ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

How to save all ui-grid data in database at once instead of single row at a time.

Open p7103934 opened this issue 11 years ago • 7 comments

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.

p7103934 avatar Aug 18 '15 04:08 p7103934

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

florian-kittel avatar Aug 18 '15 10:08 florian-kittel

Nice job, I have try it out and works! Thanks!

vigohe avatar Sep 27 '15 06:09 vigohe

I am new in this ,can you give the complete code for the save and update of all grid data to database in plunkr

prasannapethe avatar Dec 10 '15 15:12 prasannapethe

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 :(

[email protected]

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 ?

HassanIsCoding avatar May 16 '16 14:05 HassanIsCoding

@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>

?

shatting avatar Jun 06 '16 17:06 shatting

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

atodicebear avatar Jul 06 '16 12:07 atodicebear

@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 :)

HassanIsCoding avatar Jul 06 '16 12:07 HassanIsCoding