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

View File

@ -143,10 +143,12 @@ export class Grid {
if (source == null) {
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) {
if (this._var.el == null) {
throw new Error('grid has not been initialized.')
@ -154,40 +156,66 @@ export class Grid {
if (!Array.isArray(list)) {
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._refreshSource(list);
}
get sourceFiltered() { return this._var.currentSource?.map(s => s.values) ?? this.source }
setItem(index, item) {
if (this._var.source == null) {
if (this._var.currentSource == null) {
throw new Error('no source');
}
const it = this._var.source[index];
const it = this._var.currentSource[index];
// clear dropdown source cache
delete it.source;
it.values = item;
this.refresh();
}
addItem(item, index) {
if (this._var.source == null) {
if (this._var.currentSource == null) {
throw new Error('no source');
}
if (index >= 0) {
this._var.source.splice(index, 0, { values: item });
const it = index >= 0 ? this._var.currentSource[index] : null;
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 {
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();
}
removeItem(index) {
if (this._var.source == null) {
if (this._var.currentSource == null) {
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();
return item;
}