fix patchData

This commit is contained in:
samunohito 2024-02-03 21:47:07 +09:00
parent 3cb3c3a148
commit b37a27e154
1 changed files with 44 additions and 26 deletions

View File

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