feature: addItems
and removeItems
This commit is contained in:
parent
b5fd20aa2c
commit
56262d6766
13
lib/ui/grid/grid.d.ts
vendored
13
lib/ui/grid/grid.d.ts
vendored
@ -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 待滚动至的行索引
|
||||
|
@ -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];
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user