How to insert a new row in grid?
I just inserted a new row in grid but it always displayed as last row. here is the http://plnkr.co/edit/geHyRzsAzSGruVzuCi29?p=preview
Hi, is there any hack code for this problem? I tried refresh but nothing happen.
Most likely "hack" would be to shallow copy the data, then reassign it.
$scope.newData = angular.copy($scope.data);
$scope.data.slice ....... whereever you're adding the data
$scope.gridOptions.data = $scope.newData;
Thanks for your reply, Paul.
I tried it, but it only works if $scope.gridOptions.data is empty before assign $scope.newData.
Summary: [] -> Assigned['A', 'D', 'B', 'C'] -> Table ['A', 'D', 'B', 'C'] ['A', 'B', 'C'] -> Assigned ['A', 'D', 'B', 'C'] -> Table ['D', 'A', 'B', 'C']
I also tried:
$scope.gridOptions.data = [];
$timeout(function(){$scope.gridOptions.data = $scope.newData;});
It works but ... @@. Any advices?
Thanks a lot.
FYI, looking through the source, I figured that making this small change fixes this problem. Hopefully this will be included in the official release asap.
Grid.prototype.addRows = function addRows(newRawData,data) { var self = this;
var existingRowCount = self.rows.length;
for (var i=0; i < newRawData.length; i++) {
var newRow = self.processRowBuilders(new GridRow(newRawData[i], i + existingRowCount, self));
if (self.options.enableRowHashing) {
var found = self.rowHashMap.get(newRow.entity);
if (found) {
found.row = newRow;
}
}
/// BEGIN OLD CODE //self.rows.push(newRow); /// END OLD CODE
/// BEGIN NEW CODE
if (!data)
self.rows.push(newRow);
else {
var index = data.indexOf(newRow.entity);
if (index != -1) {
self.rows.splice(index, 0, newRow);
}
}
/// END NEW CODE } };
Thanks balakishman, you saved my day :)
@balakrishnan051: Can i know what variable you passed as data parameter? Is that self.options.data?
I'm passing self.options.data but it works incorrectly in case data existed but data don't contains newRow. Ex: Load page1's data -> data assigned -> load page2's data -> blank grid.
Thanks a lot.
This is how I called addRows:
Grid.prototype.modifyRows = function modifyRows(newRawData) {
......
var unfoundNew = (unfoundNewRowsToFind || newRawData);
// Search for real new rows in `unfoundNew` and concat them onto `newRows`
newRows = newRows.concat(self.newInN(self.rows, unfoundNew, 'entity'));
/// OLD CODE self.addRows(newRows); /// END
/// NEW CODE self.addRows(newRows, newRawData); /// END
I'm not completely sure, but it looks like this change would completely ignore all the row hashing setup that addRows() uses.
Have you compared before and after performance when adding large chunks of data? That's what the row hashing is intended to fix. Check this comment: https://github.com/angular-ui/ng-grid/issues/1029#issuecomment-41562620
@c0bra : it's look like, it is working fine. Where do you using the hashing to improve the performance, so that i can debug there and come up with some performance numbers.
@PaulL1 your solution worked thanks!, but in a hope to understand the angular/grid runtime better, if you have some time, can you provide any insight why your 'hack' solution:
angular.copy, unshift on the copy, then replace the whole array works
vs
gridOptions.data.unshift(row)?
Aren't there watchers on the data property that the grid would lose when I replace it with a new array in that manner?
And what watchers on the unshift operation would cause the grid to render below the rows already rendered?
The watchers are set up so that they notice a change in the overall array, I think it's doing a watch collection. So it notices that there's a new row in the data, but the way the code works it first looks to see what rows have changed (removing any that no longer exist), then adds new rows. I think replacing the whole array causes it to remove all rows and then readd them, so you end up with things in the order of your original array.
There's a PR out there to align the data to the source data, it needs a couple of updates before being applied.
Thanks Paul, so it's not the watch per say but really it's the logic in the listener that would need to be adapted to support honoring the position the item was inserted into relative to the way the grid is showing the data.. that makes sense..
@PaulL1 @JohnMorales can you clarify the hack? I don't get it. .slice....
I need to get around this issue too, and I don't wanna modify the ui-grid.js code.
Refer this plunker - I think this "hack" is no longer necessary, we merged a change that puts the rows in the order of the data. http://plnkr.co/edit/fsDVpgtpufVNhsGklXeP?p=preview
You may need to build from source, this might not be in rc16 yet.
Closing as no response for some months, and believe this is now working.
I concur, it works for me now on 3.0.0-rc15
PaulL1, I was goin through your plunker sample. If I am adding a new row and saving. Later, I want to apply an external save button, By which this newly added record is persisted to the DB. Then how about its refreshing the grid again. And how about the id Number generated.