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 返回已添加的行数据
|
* @returns 返回已添加的行数据
|
||||||
*/
|
*/
|
||||||
addItem(item: GridItem, index?: number): GridItem;
|
addItem(item: GridItem, index?: number): GridItem;
|
||||||
|
/**
|
||||||
|
* 批量添加行数据
|
||||||
|
* @param array 待添加的行数据数组
|
||||||
|
* @param index 待添加的行索引
|
||||||
|
* @returns 返回已添加的行数据数组
|
||||||
|
*/
|
||||||
|
addItems(array: Array<GridItem>, index?: number): Array<GridItem>
|
||||||
/**
|
/**
|
||||||
* 删除行数据
|
* 删除行数据
|
||||||
* @param index 待删除的行索引
|
* @param index 待删除的行索引
|
||||||
* @returns 返回已删除的行数据
|
* @returns 返回已删除的行数据
|
||||||
*/
|
*/
|
||||||
removeItem(index: number): GridItem;
|
removeItem(index: number): GridItem;
|
||||||
|
/**
|
||||||
|
* 批量删除行数据
|
||||||
|
* @param indexes 待删除的行索引数组
|
||||||
|
* @returns 返回已删除的行数据数组
|
||||||
|
*/
|
||||||
|
removeItems(indexes: Array<number>): Array<GridItem>;
|
||||||
/**
|
/**
|
||||||
* 滚动到指定行的位置
|
* 滚动到指定行的位置
|
||||||
* @param index 待滚动至的行索引
|
* @param index 待滚动至的行索引
|
||||||
|
@ -182,7 +182,7 @@ export class Grid {
|
|||||||
throw new Error('no source');
|
throw new Error('no source');
|
||||||
}
|
}
|
||||||
const it = index >= 0 ? this._var.currentSource[index] : null;
|
const it = index >= 0 ? this._var.currentSource[index] : null;
|
||||||
const newIt = { values: item };
|
const newIt = { __index: null, values: item };
|
||||||
if (it != null) {
|
if (it != null) {
|
||||||
newIt.__index = it.__index;
|
newIt.__index = it.__index;
|
||||||
this._var.currentSource.splice(index, 0, newIt);
|
this._var.currentSource.splice(index, 0, newIt);
|
||||||
@ -203,6 +203,36 @@ export class Grid {
|
|||||||
return item;
|
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) {
|
removeItem(index) {
|
||||||
if (this._var.currentSource == null) {
|
if (this._var.currentSource == null) {
|
||||||
throw new Error('no source');
|
throw new Error('no source');
|
||||||
@ -217,10 +247,59 @@ export class Grid {
|
|||||||
for (let i = it.__index; i < this._var.source.length; ++i) {
|
for (let i = it.__index; i < this._var.source.length; ++i) {
|
||||||
this._var.source[i].__index -= 1;
|
this._var.source[i].__index -= 1;
|
||||||
}
|
}
|
||||||
|
if (index < 1) {
|
||||||
|
this._var.selectedIndexes = [index - 1];
|
||||||
|
} else {
|
||||||
|
this._var.selectedIndexes = [];
|
||||||
|
}
|
||||||
this.reload();
|
this.reload();
|
||||||
return it.values;
|
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) {
|
_refreshSource(list) {
|
||||||
list ??= this._var.source;
|
list ??= this._var.source;
|
||||||
if (this._var.colAttrs.__filtered === true) {
|
if (this._var.colAttrs.__filtered === true) {
|
||||||
@ -1046,6 +1125,17 @@ export class Grid {
|
|||||||
_fillRows(rows, cols, widths) {
|
_fillRows(rows, cols, widths) {
|
||||||
const startIndex = this._var.startIndex;
|
const startIndex = this._var.startIndex;
|
||||||
const selectedIndexes = this._var.selectedIndexes;
|
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) => {
|
[...rows].forEach((row, i) => {
|
||||||
const vals = this._var.currentSource[startIndex + i];
|
const vals = this._var.currentSource[startIndex + i];
|
||||||
if (vals == null) {
|
if (vals == null) {
|
||||||
@ -1100,10 +1190,14 @@ export class Grid {
|
|||||||
this._onRowChanged(null, i, col, val, cell, true);
|
this._onRowChanged(null, i, col, val, cell, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
element = selected ?
|
if (stateChanged) {
|
||||||
type.createEdit(e => this._onRowChanged(e, i, col, type.getValue(e, col), cell), col, this._var.el, vals) :
|
element = selected ?
|
||||||
type.create(col);
|
type.createEdit(e => this._onRowChanged(e, i, col, type.getValue(e, col), cell), col, this._var.el, vals) :
|
||||||
cell.replaceChildren(element);
|
type.create(col);
|
||||||
|
cell.replaceChildren(element);
|
||||||
|
} else {
|
||||||
|
element = cell.children[0];
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
element = cell.children[0];
|
element = cell.children[0];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user