How to save all ui-grid data in database at once instead of single row at a time.
We have requirement, where we want to save all updated data(parent and child grid) to database, when user will hit save button. I am analyzing UI-Grid, but not finding any feature/techniques to do the same. I am aware about single row update, but that is not the requirement.
Can any one please help me out for the same.
When you use rowEdit (http://ui-grid.info/docs/#/tutorial/205_row_editable), you can specify the option with a wait intervall of -1. This will do the trick:
$scope.gridOptions = {
rowEditWaitInterval: -1
};
$scope.gridOptions.onRegisterApi = function(gridApi){
$scope.gridApi = gridApi;
};
Then the save will not automaticly execute. You can see the "dirty" rows, that have changes. But the changes are not stored. I use an external button for "save all changes".
Here are a styles that makes it clear what row is dirty:
.ui-grid-row-dirty .ui-grid-cell {
color: #94c11c;
background-color: #ccc;
}
You can get the dirty-row with:
$scope.gridDirtyRows = $scope.gridApi.rowEdit.getDirtyRows($scope.gridApi.grid);
and then loop the row to save or what ever your CRUD Api will do with the entries.
For example:
// map the dirty rows with all the data inside
var dataDirtyRows = $scope.gridDirtyRows.map(function (gridRow) {
return gridRow.entity;
});
Then send it to the server.
After that (on success) you have to clean the state of the dirty-rows with:
$scope.gridApi.rowEdit.setRowsClean(dataDirtyRows);
Let me know if it works also for you. Best regards
Nice job, I have try it out and works! Thanks!
I am new in this ,can you give the complete code for the save and update of all grid data to database in plunkr
I have tried to follow this above and alot others to save the edited grid back to db but got no luck so far, spent whole week on it :(
dont wana Give up on AngularJs Ui-Grid
$scope.gridDirtyRows = $scope.gridApi.rowEdit.getDirtyRows($scope.gridApi.grid); throws error : angular.js: TypeError: Cannot read property 'rowEdit' of undefined
Where can I call this getDirtyRows? inside the a- gridApi.edit.on.afterCellEdit b- $scope.gridOptions.onRegisterApi
any suggestions plz ?
@Hassan121Cs did you include the module
app = angular.module('appName', [...,'ui.grid.rowEdit',..])
and the directive
<div ui-grid="gridOptions" ui-grid-row-edit></div>
?
i have the same issue but i inclueded 'ui....' here my code http://plnkr.co/edit/HIiD50kGMWNArizrWgC8?p=info its allways throws error not nkown aftercell edit or row edit or something else. not even simple $http.delete... which is workin in code further below. only console.log works
@shatting
Yes, I was missing "ui.grid.rowEdit" as you suggested, It helped me to activate one of the feature but still I am unable to only get the dirty rows,
so, for dirty rows , I have creating a DirtyFlag inside the Angular and sending the whole GridOptions.Data to Server and there handling the DirtyFlag Rows, I know this is poor but got no other option.
would share more IF I succeed in getting the dirty rows.
one other thing I tried was to push the row in a stack when User is Editing it but again if User was editting the same row three times, I was ended up with 3 same rows in the Stack.
Hope to find a solution One Day ;)
Thank you @shatting for the correction :)