feature: addItems and removeItems

This commit is contained in:
Chen Lily 2024-01-29 12:14:35 +08:00
parent b5fd20aa2c
commit 56262d6766
2 changed files with 112 additions and 5 deletions

13
lib/ui/grid/grid.d.ts vendored
View File

@ -260,12 +260,25 @@ export class Grid {
* @returns
*/
addItem(item: GridItem, index?: number): GridItem;
/**
*
* @param array
* @param index
* @returns
*/
addItems(array: Array<GridItem>, index?: number): Array<GridItem>
/**
*
* @param index
* @returns
*/
removeItem(index: number): GridItem;
/**
*
* @param indexes
* @returns
*/
removeItems(indexes: Array<number>): Array<GridItem>;
/**
*
* @param index

View File

@ -182,7 +182,7 @@ export class Grid {
throw new Error('no source');
}
const it = index >= 0 ? this._var.currentSource[index] : null;
const newIt = { values: item };
const newIt = { __index: null, values: item };
if (it != null) {
newIt.__index = it.__index;
this._var.currentSource.splice(index, 0, newIt);
@ -203,6 +203,36 @@ export class Grid {
return item;
}
addItems(array, index) {
if (this._var.currentSource == null) {
throw new Error('no source');
}
if (!Array.isArray(array) || array.length <= 0) {
throw new Error(`invalid items array: ${array}`);
}
const it = index >= 0 ? this._var.currentSource[index] : null;
if (it != null) {
const items = array.map((a, i) => ({ __index: it.__index + i, values: a }));
this._var.currentSource.splice(index, 0, ...items);
if (this._var.colAttrs.__filtered === true) {
this._var.source.splice(it.__index, 0, ...items);
}
const offset = array.length;
for (let i = it.__index + offset; i < this._var.source.length; ++i) {
this._var.source[i].__index += offset;
}
} else {
const length = this._var.source.length;
const items = array.map((a, i) => ({ __index: length + i, values: a }));
this._var.currentSource.push(...items);
if (this._var.colAttrs.__filtered === true) {
this._var.source.push(...items);
}
}
this.reload();
return array;
}
removeItem(index) {
if (this._var.currentSource == null) {
throw new Error('no source');
@ -217,10 +247,59 @@ export class Grid {
for (let i = it.__index; i < this._var.source.length; ++i) {
this._var.source[i].__index -= 1;
}
if (index < 1) {
this._var.selectedIndexes = [index - 1];
} else {
this._var.selectedIndexes = [];
}
this.reload();
return it.values;
}
removeItems(indexes) {
if (this._var.currentSource == null) {
throw new Error('no source');
}
if (!Array.isArray(indexes) || indexes.length <= 0) {
throw new Error(`invalid selected indexes: ${indexes}`);
}
indexes = indexes.slice().sort();
const array = [];
let first = 0;
for (let i = indexes.length - 1; i >= 0; --i) {
let it = this._var.currentSource.splice(indexes[i], 1)[0];
if (it == null) {
continue;
}
let next = this._var.source[it.__index];
if (next != null && next.__offset == null) {
next.__offset = i + 1;
}
if (this._var.colAttrs.__filtered === true) {
this._var.source.splice(it.__index, 1);
}
array.splice(0, 0, it.values);
first = it.__index;
}
let offset = 1;
for (let i = first; i < this._var.source.length; ++i) {
let it = this._var.source[i];
if (it.__offset > 0) {
offset = it.__offset;
delete it.__offset;
}
it.__index -= offset;
}
const index = indexes[0];
if (index < 1) {
this._var.selectedIndexes = [index - 1];
} else {
this._var.selectedIndexes = [];
}
this.reload();
return array;
}
_refreshSource(list) {
list ??= this._var.source;
if (this._var.colAttrs.__filtered === true) {
@ -1046,6 +1125,17 @@ export class Grid {
_fillRows(rows, cols, widths) {
const startIndex = this._var.startIndex;
const selectedIndexes = this._var.selectedIndexes;
const stateChanged =
this._var.oldIndex !== startIndex ||
selectedIndexes == null ||
this._var.oldSelectedIndexes?.length !== selectedIndexes.length ||
this._var.oldSelectedIndexes.find((s, i) => s !== selectedIndexes[i]) != null;
if (stateChanged) {
this._var.oldIndex = startIndex;
if (selectedIndexes != null) {
this._var.oldSelectedIndexes = selectedIndexes.slice();
}
}
[...rows].forEach((row, i) => {
const vals = this._var.currentSource[startIndex + i];
if (vals == null) {
@ -1100,10 +1190,14 @@ export class Grid {
this._onRowChanged(null, i, col, val, cell, true);
}
}
element = selected ?
type.createEdit(e => this._onRowChanged(e, i, col, type.getValue(e, col), cell), col, this._var.el, vals) :
type.create(col);
cell.replaceChildren(element);
if (stateChanged) {
element = selected ?
type.createEdit(e => this._onRowChanged(e, i, col, type.getValue(e, col), cell), col, this._var.el, vals) :
type.create(col);
cell.replaceChildren(element);
} else {
element = cell.children[0];
}
} else {
element = cell.children[0];
}