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

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

@@ -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;
}