Skip to content

Commit

Permalink
fix(update): fix update an item id using DataView, closes #353
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Mar 15, 2019
1 parent 116addc commit e06fe06
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions slick.dataview.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 79,7 @@
// events
var onRowCountChanged = new Slick.Event();
var onRowsChanged = new Slick.Event();
var onRowsOrCountChanged = new Slick.Event();
var onRowsOrCountChanged = new Slick.Event();
var onPagingInfoChanged = new Slick.Event();

options = $.extend(true, {}, defaults, options);
Expand Down Expand Up @@ -219,7 219,7 @@
function getFilter(){
return filter;
}

function setFilter(filterFn) {
filter = filterFn;
if (options.inlineFilters) {
Expand Down Expand Up @@ -356,10 356,38 @@
}

function updateItem(id, item) {
if (idxById[id] === undefined || id !== item[idProperty]) {
throw new Error("Invalid or non-matching id");
// see also https://github.com/mleibman/SlickGrid/issues/1082
if (idxById[id] === undefined) {
throw new Error("Invalid id");
}

// What if the specified item also has an updated idProperty?
// Then we'll have to update the index as well, and possibly the `updated` cache too.
if (id !== item[idProperty]) {
// make sure the new id is unique:
var newId = item[idProperty];
if (newId == null) {
throw new Error("Cannot update item to associate with a null id");
}
if (idxById[newId] !== undefined) {
throw new Error("Cannot update item to associate with a non-unique id");
}
idxById[newId] = idxById[id];
delete idxById[id];

// Also update the `updated` hashtable/markercache? Yes, `recalc()` inside `refresh()` needs that one!
if (updated && updated[id]) {
delete updated[id];
}

// Also update the row indexes? no need since the `refresh()`, further down, blows away the `rowsById[]` cache!

id = newId;
}
items[idxById[id]] = item;

// Also update the rows? no need since the `refresh()`, further down, blows away the `rows[]` cache and recalculates it via `recalc()`!

if (!updated) {
updated = {};
}
Expand Down Expand Up @@ -429,7 457,7 @@
}
return low;
}

function getLength() {
return rows.length;
}
Expand Down Expand Up @@ -583,7 611,7 @@
group = groups[i];
group.groups = extractGroups(group.rows, group);
}
}
}

groups.sort(groupingInfos[level].comparer);

Expand Down Expand Up @@ -633,7 661,7 @@
level = level || 0;
var gi = groupingInfos[level];
var groupCollapsed = gi.collapsed;
var toggledGroups = toggledGroupsByLevel[level];
var toggledGroups = toggledGroupsByLevel[level];
var idx = groups.length, g;
while (idx--) {
g = groups[idx];
Expand All @@ -655,7 683,7 @@
g.collapsed = groupCollapsed ^ toggledGroups[g.groupingKey];
g.title = gi.formatter ? gi.formatter(g) : g.value;
}
}
}

function flattenGroupedRows(groups, level) {
level = level || 0;
Expand Down Expand Up @@ -941,7 969,7 @@
onRowsChanged.notify({rows: diff, dataView: self, calledOnRowCountChanged: (countBefore !== rows.length)}, null, self);
}
if (countBefore !== rows.length || diff.length > 0) {
onRowsOrCountChanged.notify({rowsDiff: diff, previousRowCount: countBefore, currentRowCount: rows.length,
onRowsOrCountChanged.notify({rowsDiff: diff, previousRowCount: countBefore, currentRowCount: rows.length,
rowCountChanged: countBefore !== rows.length, rowsChanged: diff.length > 0, dataView: self}, null, self);
}
}
Expand Down Expand Up @@ -990,7 1018,7 @@
inHandler = true;
var selectedRows = self.mapIdsToRows(selectedRowIds);
if (!preserveHidden) {
setSelectedRowIds(self.mapRowsToIds(selectedRows));
setSelectedRowIds(self.mapRowsToIds(selectedRows));
}
grid.setSelectedRows(selectedRows);
inHandler = false;
Expand Down

0 comments on commit e06fe06

Please sign in to comment.