Skip to content

Commit

Permalink
fix: Unable to delete selected rows in some input tables (#1678)
Browse files Browse the repository at this point in the history
- Erroneously assumed that the key columns were the first columns in the
table
- Removed unused delete function from `IrisGridTableModelTemplate`
- It should only be implemented for regular JsTables as JsTreeTables do
not have input tables
  - Was duplicated code
- Fixes #1677
- Tested using the code in the ticket
  • Loading branch information
mofojed authored Dec 8, 2023
1 parent 1b06675 commit 1e71550
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 98 deletions.
29 changes: 20 additions & 9 deletions packages/iris-grid/src/IrisGridTableModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,29 @@ class IrisGridTableModel extends IrisGridTableModelTemplate<Table, UIRow> {
if (tableRanges.length <= 0) {
return;
}

const [data, deleteTable] = await Promise.all([
// Need to get the key values of each row
this.snapshot(
tableRanges.map(range => {
assertNotNull(range);
return new GridRange(
0,
range.startRow,
keyColumns.length - 1,
range.endRow
);
})
tableRanges
.map(range => {
assertNotNull(range);
// Need to map each key column to it's range so we can pass that into the snapshot function
return keyColumns.map(keyColumn => {
const keyIndex = this.getColumnIndexByName(keyColumn.name);
if (keyIndex == null) {
throw new Error(`Key column ${keyColumn.name} not found`);
}

return new GridRange(
keyIndex,
range.startRow,
keyIndex,
range.endRow
);
});
})
.flat()
),
this.table.copy(),
]);
Expand Down
90 changes: 1 addition & 89 deletions packages/iris-grid/src/IrisGridTableModelTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2086,95 +2086,7 @@ class IrisGridTableModelTemplate<
}

async delete(ranges: GridRange[]): Promise<void> {
if (!this.isDeletableRanges(ranges)) {
throw new Error(`Undeletable ranges ${ranges}`);
}

assertNotNull(this.inputTable);
const { keyColumns } = this.inputTable;
if (keyColumns.length === 0) {
throw new Error('No key columns to allow deletion');
}

const pendingAreaRange = this.getPendingAreaRange();
const pendingRanges = ranges
.map(range => GridRange.intersection(pendingAreaRange, range))
.filter(range => range != null)
.map(range => {
assertNotNull(range);

return GridRange.offset(
range,
0,
-(this.floatingTopRowCount + this.table.size)
);
});

if (pendingRanges.length > 0) {
const newDataMap = new Map(this.pendingNewDataMap);
for (let i = 0; i < pendingRanges.length; i += 1) {
const pendingRange = pendingRanges[i];
assertNotNull(pendingRange.startRow);
assertNotNull(pendingRange.endRow);
for (let r = pendingRange.startRow; r <= pendingRange.endRow; r += 1) {
newDataMap.delete(r);
}
}
this.pendingNewDataMap = newDataMap;

this.formattedStringData = [];

this.dispatchEvent(
new EventShimCustomEvent(IrisGridModel.EVENT.PENDING_DATA_UPDATED)
);

this.dispatchEvent(new EventShimCustomEvent(IrisGridModel.EVENT.UPDATED));
}

const tableAreaRange = this.getTableAreaRange();
const tableRanges = ranges
.map(range => GridRange.intersection(tableAreaRange, range))
.filter(range => range != null);
if (tableRanges.length <= 0) {
return;
}
const [data, deleteTable] = await Promise.all([
// Need to get the key values of each row
this.snapshot(
tableRanges.map(range => {
assertNotNull(range);
return new GridRange(
0,
range.startRow,
keyColumns.length - 1,
range.endRow
);
})
),
this.table.copy(),
]);

// Now copy the existing table and filter it on the values in the snapshot for the key columns in the input table
const filters = data.map(row => {
const columnFilters = [];
for (let c = 0; c < keyColumns.length; c += 1) {
const column = keyColumns[c];
const value = row[c];
const filterValue = this.tableUtils.makeFilterRawValue(
column.type,
value
);
const filter = column.filter().eq(filterValue);
columnFilters.push(filter);
}
return columnFilters.reduce((agg, curr) => agg?.and(curr) ?? curr);
});

const filter = filters.reduce((agg, curr) => agg?.or(curr) ?? curr);
deleteTable.applyFilter([filter]);

// await this.inputTable?.deleteTable(deleteTable);
deleteTable.close();
throw new Error('Delete not implemented');
}

isValidForCell(x: ModelIndex, y: ModelIndex, value: string): boolean {
Expand Down

0 comments on commit 1e71550

Please sign in to comment.