resolve issue of addItem, removeItem, update definition of get source()

This commit is contained in:
Chen Lily 2024-01-27 15:47:15 +08:00
parent ac605895c5
commit fc61731c7d
2 changed files with 44 additions and 16 deletions

View File

@ -207,7 +207,9 @@ export class Grid {
*/ */
onSorted?: (array?: Array<GridColumnSortDefinition>) => void; onSorted?: (array?: Array<GridColumnSortDefinition>) => void;
/** 获取数据数组 */ /** 返回所有数据的数据(未过滤) */
get allSource(): Array<GridItem>;
/** 获取数据数组(已过滤) */
get source(): Array<GridItem>; get source(): Array<GridItem>;
/** 设置数据,并刷新列表 */ /** 设置数据,并刷新列表 */
set source(list: Array<GridItem>); set source(list: Array<GridItem>);
@ -224,8 +226,6 @@ export class Grid {
/** 设置 Grid 滚动偏移量 */ /** 设置 Grid 滚动偏移量 */
set scrollTop(top: number); set scrollTop(top: number);
/** 获取已过滤后的当前列表中的数据数组 */
get sourceFiltered(): Array<GridItem>;
/** 获取 Grid 的页面元素 */ /** 获取 Grid 的页面元素 */
get element(): HTMLElement; get element(): HTMLElement;
/** 获取当前 Grid 是否已发生改变 */ /** 获取当前 Grid 是否已发生改变 */

View File

@ -143,10 +143,12 @@ export class Grid {
if (source == null) { if (source == null) {
return false; return false;
} }
return source.filter(r => r.__changed).length > 0; return source.find(r => r.__changed) != null;
} }
get source() { return this._var.source?.map(s => s.values) } get allSource() { return this._var.source?.map(s => s.values) }
get source() { return this._var.currentSource?.map(s => s.values) }
set source(list) { set source(list) {
if (this._var.el == null) { if (this._var.el == null) {
throw new Error('grid has not been initialized.') throw new Error('grid has not been initialized.')
@ -154,40 +156,66 @@ export class Grid {
if (!Array.isArray(list)) { if (!Array.isArray(list)) {
throw new Error('source is not an Array.') throw new Error('source is not an Array.')
} }
list = list.map(i => { return { values: i } }); list = list.map((it, index) => {
return {
__index: index,
values: it
}
});
this._var.source = list; this._var.source = list;
this._refreshSource(list); this._refreshSource(list);
} }
get sourceFiltered() { return this._var.currentSource?.map(s => s.values) ?? this.source }
setItem(index, item) { setItem(index, item) {
if (this._var.source == null) { if (this._var.currentSource == null) {
throw new Error('no source'); throw new Error('no source');
} }
const it = this._var.source[index]; const it = this._var.currentSource[index];
// clear dropdown source cache
delete it.source; delete it.source;
it.values = item; it.values = item;
this.refresh(); this.refresh();
} }
addItem(item, index) { addItem(item, index) {
if (this._var.source == null) { if (this._var.currentSource == null) {
throw new Error('no source'); throw new Error('no source');
} }
if (index >= 0) { const it = index >= 0 ? this._var.currentSource[index] : null;
this._var.source.splice(index, 0, { values: item }); const newIt = { values: item };
if (it != null) {
newIt.__index = it.__index;
this._var.currentSource.splice(index, 0, newIt);
if (this._var.colAttrs.__filtered === true) {
this._var.source.splice(it.__index, 0, newIt);
}
for (let i = it.__index + 1; i < this._var.source.length; ++i) {
this._var.source[i].__index += 1;
}
} else { } else {
this._var.source.push({ values: item }); newIt.__index = this._var.source.length;
this._var.currentSource.push(newIt);
if (this._var.colAttrs.__filtered === true) {
this._var.source.push(newIt);
}
} }
this.reload(); this.reload();
} }
removeItem(index) { removeItem(index) {
if (this._var.source == null) { if (this._var.currentSource == null) {
throw new Error('no source'); throw new Error('no source');
} }
const item = this._var.source.splice(index, 1)[0]; const it = this._var.currentSource.splice(index, 1)[0];
if (it == null) {
return null;
}
if (this._var.colAttrs.__filtered === true) {
this._var.source.splice(it.__index, 1);
}
for (let i = it.__index + 1; i < this._var.source.length; --i) {
this._var.source[i].__index -= 1;
}
this.reload(); this.reload();
return item; return item;
} }