add: Grid.readonly

This commit is contained in:
Chen Lily 2024-02-05 16:18:27 +08:00
parent 8712554f12
commit 09bdef19d5

View File

@ -515,6 +515,12 @@ export class Grid {
* @private
*/
currentSource: null,
/**
* Grid 是否只读
* @type {boolean}
* @private
*/
readonly: false,
/**
* 当前选中的列索引
* @type {number}
@ -976,6 +982,16 @@ export class Grid {
*/
get allSource() { return this._var.source?.map(s => s.values) }
/**
* 获取或设置 Grid 是否为只读
* @type {boolean}
*/
get readonly() { return this._var.readonly }
set readonly(flag) {
this._var.readonly = flag;
this.refresh();
}
/**
* 获取已过滤的数据数组或者设置数据并刷新列表
* @type {GridItem[]}
@ -2094,6 +2110,7 @@ export class Grid {
const exists = content.querySelectorAll('&>.ui-grid-row').length;
count -= exists;
if (count > 0) {
const readonly = this._var.readonly;
for (let i = 0; i < count; ++i) {
const row = createElement('tr', 'ui-grid-row');
let left = this.expandable ? ExpandableWidth : 0;
@ -2129,7 +2146,7 @@ export class Grid {
if (style !== '') {
cell.style.cssText = style;
}
if (GridColumnTypeEnum.isCheckbox(col.type)) {
if (!readonly && GridColumnTypeEnum.isCheckbox(col.type)) {
cell.appendChild(GridCheckboxColumn.createEdit(e => this._onRowChanged(e, exists + i, col, e.target.checked, cell)));
// this._var.colTypes[col.key] = GridCheckboxColumn;
} else {
@ -2182,6 +2199,7 @@ export class Grid {
}
}
const offset = this.expandable ? 1 : 0;
const readonly = this._var.readonly;
[...rows].forEach((row, i) => {
const vals = this._var.currentSource[startIndex + i];
if (vals == null) {
@ -2280,7 +2298,7 @@ export class Grid {
const isCheckbox = GridColumnTypeEnum.isCheckbox(col.type);
const type = isCheckbox ? GridCheckboxColumn : this._var.colTypes[col.key] ?? GridColumn;
let element;
if (!isCheckbox && typeof type.createEdit === 'function') {
if (!readonly && !isCheckbox && typeof type.createEdit === 'function') {
if (vals.__editing?.[col.key]) {
if (typeof type.leaveEdit === 'function') {
type.leaveEdit(cell.children[0], this._var.el);