[ui-grid 3.0.0-rc.12] Adding multiple new rows to ui-grid results in rows of duplicate content that always appear at the bottom of the grid
I'm implementing a ui-grid that doesn't attempt to persist the rows until you hit a "Save Rows" button. When you press it, various $resource REST calls are made to call a REST service on the backend to persist additions, deletions, updates to a database.
As such, here are my grid options:
$scope.gridOptions = {
columnDefs: [
{field: 'id', displayName: 'Identifier', enableCellEdit: true},
// other colDefs
],
data: $scope.myData,
enableCellSelection: true,
enableColumnResizing: true,
enableFiltering: true,
enableGridMenu: true,
enableRowSelection: true,
minRowsToShow: 50,
multiSelect: true,
rowHeight: 20,
rowEditWaitInterval: -1,
showFooterRow: false
};
Following the examples, I try to add a new row of when I press "Add rows" button, this is invoked when I press the controller (I do unshift() so the new row appears as the new first row of the grid for easy access):
// add a new pattern override row. we don't make actually save it or mark it as "dirty" here.
$scope.addOverride = function() {
var emptyData = {
id: "",
idType: "myType",
// other cols
_newRow: true
};
$scope.gridOptions.data.unshift(emptyData);
$scope.refreshGrid();
};
I have registered these event handlers ():
$scope.gridOptions.onRegisterApi = function(gridApi) {
$scope.gridApi = gridApi;
gridApi.selection.on.rowSelectionChanged($scope, function(row) {...});
gridApi.edit.on.afterCellEdit( $scope, function( rowEntity, colDef ) {...});
gridApi.rowEdit.on.saveRow($scope, function(rowEntity){...});
};
Eventually, I call the following when I hit the Save button:
$scope.gridApi.rowEdit.flushDirtyRows($scope.gridApi.grid);
which, in turn, triggers the saveRow() callback for every row that was added/modified (any dirty rows). This resolves all the promises on the rows.
Back to the problem: I don't hit "Save" until I'm done adding rows. Again, the issue is that if I add a 2nd new row (before hitting Save), the 2nd new row appears will the same data as the first. When I try to modify the 2nd new row, it also changes the 1st new row! Can you help me figure out the right way to do this:
- and 2..N new rows before hitting Save
- make the new rows appear as the first row in the grid for easy access.
Thanks!
I'm using ui-grid 3.0.0-rc.12
I created a quick plunker, based on the rowEdit tutorial. It seems to work for me, I wonder whether somewhere in your code you're actually using the same instance of emptyData in both rows - so both rows are actually pointing to the same entity?
http://plnkr.co/edit/fsDVpgtpufVNhsGklXeP?p=preview
You may also need to upgrade to the latest - the commit that has the row order follow the data array was quite recent.
oh ok I think I need to upgrade then.
how do I do that. bower install ui-grid right?
Either bower install angular-ui-grid or you may need to build from source - so you need to clone and then build following the instructions in https://github.com/angular-ui/ng-grid/blob/master/FIRST_TIMER.md
@PaulL1 Thanks for your help, I was able to get the new row showing on the top after the upgrade.
The remaining issue is that the 2nd...nth now row is a duplicate of the first new row. As you mentioned, somehow we may be pointing to the same instance...but I don't see how. Here is my code that is invoked when I call click the Add Rows button:
$scope.addRows = function () {
var newLength = $scope.gridOptions.data.unshift({
id: "",
myName: "",
myDate: "",
_newRow: true
});
$scope.refreshGrid();
};
However, I forgot to mention that I have also overriden getRowIdentity() and rowIdentity() on gridOptions as the following (as per the API):
$scope.gridOptions.getRowIdentity = function(row) {
return row.id + '-' + row.myName;
};
since the underlying database row has a composite key based on these 2 columns.
Could this be the cause of my "duplicate" rows? considering that the new "blank" row technically has the same "rowIdentity" of the previously entered new row (which has not yet been committed)?
Any way around this if this is the case?
I modified your plunkr @PaulL1 and have confirmed that defining a rowIdentity is what causes the duplicate row issue.
Besides not defining a rowIdenity, are there any other ways to avoid this issue when trying to add more than one new row? Do you think this is a bug, or working as designed, or something that can be improved (i.e. ignore the rowIdentity, if one exists, when adding (but not yet saving) a new row)?
Yes, I would expect this would cause it - the grid now has two rows with the same identity, and so it will probably use the first of them for both. There are a handful of ways to deal with this:
- Use the native hashkey instead of overriding
- Allocate a default myName or default id when you create the row so that it is unique
- Change your rowIdentity to have something else in it that is unique - so for example you could add a property to all newly created rows of "newId", and add that into your rowIdentity. All rows that aren't new won't have it, so it won't impact any other rows, but it would fix the new rows.
I think I'd go with 3, the code would be something like:
$scope.gridOptions.getRowIdentity = function(row) {
return row.id + '-' + row.myName + ( row._newId ? row._newId : '' );
};
And you'd modify your addRows like:
var lastNewId = 1;
$scope.addRows = function () {
var newLength = $scope.gridOptions.data.unshift({
id: "",
myName: "",
myDate: "",
_newId: lastNewId++,
_newRow: true
});
$scope.refreshGrid();
};
I'm assuming here that your server is ignoring any attribute prefixed with _.
@PaulL1 Once again thanks for your insightful help.
I'll try 3 tomorrow once I've gotten some shut-eye!
@PaulL1 Thanks. This works for me. To avoid the server choking on the unrecognized value, I just do a delete rowEntity.newId before sending it over in a REST call with $resource.add() (POST call).
I'll close this. Hopefully, this tip will help others as well. Thanks!