From 6dc9df8b1750dfadd143bccb0eaec209a6c09d71 Mon Sep 17 00:00:00 2001 From: Tsanie Date: Mon, 5 Feb 2024 16:24:54 +0800 Subject: [PATCH] optimized --- lib/ui/grid/grid.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/ui/grid/grid.js b/lib/ui/grid/grid.js index f97454d..33ea02c 100644 --- a/lib/ui/grid/grid.js +++ b/lib/ui/grid/grid.js @@ -986,7 +986,7 @@ export class Grid { * 获取或设置 Grid 是否为只读 * @type {boolean} */ - get readonly() { return this._var.readonly } + get readonly() { return this._var.readonly === true } set readonly(flag) { this._var.readonly = flag; this.refresh(); @@ -1283,9 +1283,7 @@ export class Grid { set selectedIndexes(indexes) { const startIndex = this._var.startIndex; this._var.selectedIndexes.splice(0, this._var.selectedIndexes.length, ...indexes); - if (this.readonly !== true) { - this.refresh(); - } else { + if (this.readonly) { this._tableRows.forEach((row, i) => { if (indexes.includes(startIndex + i)) { row.classList.add('selected'); @@ -1293,6 +1291,8 @@ export class Grid { row.classList.remove('selected'); } }); + } else { + this.refresh(); } if (typeof this.onSelectedRowChanged === 'function') { this.onSelectedRowChanged(); @@ -1393,12 +1393,12 @@ export class Grid { return; } selectedIndexes.splice(0); - if (this.readonly !== true) { - this.refresh(); - } else { + if (this.readonly) { this._tableRows.forEach(row => { row.classList.remove('selected'); }); + } else { + this.refresh(); } if (typeof this.onSelectedRowChanged === 'function') { this.onSelectedRowChanged(-1); @@ -1929,6 +1929,7 @@ export class Grid { thead.appendChild(header); const sizer = this._var.refs.sizer; let left = this.expandable ? ExpandableWidth : 0; + const readonly = this.readonly; if (this.expandable) { header.appendChild(createElement('th', th => { th.className = 'ui-expandable sticky'; @@ -1971,7 +1972,7 @@ export class Grid { this._var.needResize = true; sizer.innerText = col.caption ?? ''; let width = sizer.offsetWidth + 22; - if (!this.readonly && col.enabled !== false && col.allcheck && isCheckbox) { + if (!readonly && col.enabled !== false && col.allcheck && isCheckbox) { width += 32; } if (col.allowFilter === true) { @@ -2024,7 +2025,7 @@ export class Grid { wrapper.style.justifyContent = 'center'; } th.appendChild(wrapper); - if (!this.readonly && col.enabled !== false && col.allcheck && isCheckbox) { + if (!readonly && col.enabled !== false && col.allcheck && isCheckbox) { const check = createCheckbox({ onchange: e => this._onColumnAllChecked(col, e.target.checked) }); @@ -2110,7 +2111,7 @@ export class Grid { const exists = content.querySelectorAll('&>.ui-grid-row').length; count -= exists; if (count > 0) { - const readonly = this._var.readonly; + const readonly = this.readonly; for (let i = 0; i < count; ++i) { const row = createElement('tr', 'ui-grid-row'); let left = this.expandable ? ExpandableWidth : 0; @@ -2199,7 +2200,7 @@ export class Grid { } } const offset = this.expandable ? 1 : 0; - const readonly = this._var.readonly; + const readonly = this.readonly; [...rows].forEach((row, i) => { const vals = this._var.currentSource[startIndex + i]; if (vals == null) { @@ -2323,7 +2324,7 @@ export class Grid { element = cell.children[0]; } let enabled; - if (this.readonly) { + if (readonly) { enabled = false; } else { enabled = col.enabled; @@ -3216,9 +3217,7 @@ export class Grid { } // apply style if (flag) { - if (this.readonly !== true) { - this.refresh(); - } else { + if (this.readonly) { this._tableRows.forEach((row, i) => { if (selectedIndexes.includes(startIndex + i)) { row.classList.add('selected'); @@ -3226,6 +3225,8 @@ export class Grid { row.classList.remove('selected'); } }); + } else { + this.refresh(); } if (typeof this.onSelectedRowChanged === 'function') { this.onSelectedRowChanged(selectedIndex);