diff --git a/lib/ui/grid/grid.js b/lib/ui/grid/grid.js index 6fff42a..f97454d 100644 --- a/lib/ui/grid/grid.js +++ b/lib/ui/grid/grid.js @@ -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);