fix patchData
This commit is contained in:
parent
3cb3c3a148
commit
b37a27e154
|
@ -967,53 +967,70 @@ function refreshData() {
|
||||||
* そこで、新しい値とセルが持つ値を突き合わせ、変更があった場合のみ値を更新し、セルそのものは使いまわしつつ値を最新化する。
|
* そこで、新しい値とセルが持つ値を突き合わせ、変更があった場合のみ値を更新し、セルそのものは使いまわしつつ値を最新化する。
|
||||||
*/
|
*/
|
||||||
function patchData(newItems: DataSource[]) {
|
function patchData(newItems: DataSource[]) {
|
||||||
const gridRows = cells.value;
|
const gridRows = [...cells.value];
|
||||||
if (gridRows.length > newItems.length) {
|
if (gridRows.length > newItems.length) {
|
||||||
|
// 行数が減った場合
|
||||||
|
|
||||||
// 状態が壊れるかもしれないので選択を全解除
|
// 状態が壊れるかもしれないので選択を全解除
|
||||||
unSelectionRangeAll();
|
unSelectionRangeAll();
|
||||||
|
|
||||||
// 行数が減った場合
|
|
||||||
const diff = gridRows
|
const diff = gridRows
|
||||||
.map((it, idx) => ({ origin: it.origin, idx }))
|
.map((it, idx) => ({ origin: it.origin, idx }))
|
||||||
.filter(it => !newItems.includes(it.origin));
|
.filter(it => !newItems.includes(it.origin))
|
||||||
for (const { idx } of diff) {
|
.map(it => it.idx);
|
||||||
rows.value.splice(idx, 1);
|
const newRows = rows.value.filter((_, idx) => !diff.includes(idx));
|
||||||
gridRows.splice(idx, 1);
|
const newCells = gridRows.filter((_, idx) => !diff.includes(idx));
|
||||||
}
|
|
||||||
|
|
||||||
// 行数が減ったので再度採番
|
// 行数が減ったので再度採番
|
||||||
for (let rowIdx = 0; rowIdx < rows.value.length; rowIdx++) {
|
for (let rowIdx = 0; rowIdx < newRows.length; rowIdx++) {
|
||||||
rows.value[rowIdx].index = rowIdx;
|
newRows[rowIdx].index = rowIdx;
|
||||||
for (const cell of gridRows[rowIdx].cells) {
|
for (const cell of newCells[rowIdx].cells) {
|
||||||
cell.address.row = rowIdx;
|
cell.address.row = rowIdx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rows.value = newRows;
|
||||||
|
cells.value = newCells;
|
||||||
} else if (gridRows.length < newItems.length) {
|
} else if (gridRows.length < newItems.length) {
|
||||||
|
// 行数が増えた場合
|
||||||
|
|
||||||
// 状態が壊れるかもしれないので選択を全解除
|
// 状態が壊れるかもしれないので選択を全解除
|
||||||
unSelectionRangeAll();
|
unSelectionRangeAll();
|
||||||
|
|
||||||
// 行数が増えた場合
|
|
||||||
const oldOrigins = gridRows.map(it => it.origin);
|
const oldOrigins = gridRows.map(it => it.origin);
|
||||||
const diff = newItems
|
const newRows = Array.of<GridRow>();
|
||||||
.map((it, idx) => ({ origin: it, idx }))
|
const newCells = Array.of<RowHolder>();
|
||||||
.filter(it => oldOrigins.indexOf(it.origin) === -1);
|
for (const it of newItems) {
|
||||||
|
const idx = oldOrigins.indexOf(it);
|
||||||
const _cols = columns.value;
|
if (idx >= 0) {
|
||||||
for (const { origin, idx } of diff) {
|
// 既存の行
|
||||||
const newRow = createRow(idx);
|
newRows.push(rows.value[idx]);
|
||||||
const newCells = _cols.map(col => createCell(col, newRow, origin[col.setting.bindTo]));
|
newCells.push(gridRows[idx]);
|
||||||
|
} else {
|
||||||
rows.value.splice(idx, 0, newRow);
|
// 新規の行
|
||||||
gridRows.splice(idx, 0, { cells: newCells, origin });
|
const newRow = createRow(newRows.length);
|
||||||
|
newRows.push(newRow);
|
||||||
|
newCells.push({
|
||||||
|
cells: columns.value.map(col => {
|
||||||
|
const cell = createCell(col, newRow, it[col.setting.bindTo]);
|
||||||
|
cell.violation = cellValidation(cell, cell.value);
|
||||||
|
return cell;
|
||||||
|
}),
|
||||||
|
origin: it,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 行数が増えたので再度採番
|
// 行数が増えたので再度採番
|
||||||
for (let rowIdx = 0; rowIdx < rows.value.length; rowIdx++) {
|
for (let rowIdx = 0; rowIdx < newCells.length; rowIdx++) {
|
||||||
rows.value[rowIdx].index = rowIdx;
|
newRows[rowIdx].index = rowIdx;
|
||||||
for (const cell of gridRows[rowIdx].cells) {
|
for (const cell of newCells[rowIdx].cells) {
|
||||||
cell.address.row = rowIdx;
|
cell.address.row = rowIdx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rows.value = newRows;
|
||||||
|
cells.value = newCells;
|
||||||
} else {
|
} else {
|
||||||
// 行数が変わらない場合
|
// 行数が変わらない場合
|
||||||
const _cols = columns.value;
|
const _cols = columns.value;
|
||||||
|
@ -1026,7 +1043,8 @@ function patchData(newItems: DataSource[]) {
|
||||||
const oldCell = oldCells[colIdx];
|
const oldCell = oldCells[colIdx];
|
||||||
const newValue = newItem[_col.setting.bindTo];
|
const newValue = newItem[_col.setting.bindTo];
|
||||||
if (oldCell.value !== newValue) {
|
if (oldCell.value !== newValue) {
|
||||||
emitCellValue(oldCell, newValue);
|
oldCell.violation = cellValidation(oldCell, newValue);
|
||||||
|
oldCell.value = newValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue