Skip to content

Commit a9d81f7

Browse files
committed
Fix(rowEdit): fix #3401 provide setRowsClean method
1 parent 755f18d commit a9d81f7

File tree

2 files changed

+125
-44
lines changed

2 files changed

+125
-44
lines changed

src/features/row-edit/js/gridRowEdit.js

Lines changed: 92 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
flushDirtyRows: function () {
148148
return service.flushDirtyRows(grid);
149149
},
150-
150+
151151
/**
152152
* @ngdoc method
153153
* @methodOf ui.grid.rowEdit.api:PublicApi
@@ -159,7 +159,7 @@
159159
* call in a $interval or $timeout
160160
* <pre>
161161
* $interval( function() {
162-
* gridApi.rowEdit.setRowsDirty(grid, myDataRows);
162+
* gridApi.rowEdit.setRowsDirty(myDataRows);
163163
* }, 0, 1);
164164
* </pre>
165165
* @param {array} dataRows the data entities for which the gridRows
@@ -168,22 +168,42 @@
168168
*/
169169
setRowsDirty: function ( dataRows) {
170170
service.setRowsDirty(grid, dataRows);
171+
},
172+
173+
/**
174+
* @ngdoc method
175+
* @methodOf ui.grid.rowEdit.api:PublicApi
176+
* @name setRowsClean
177+
* @description Sets each of the rows passed in dataRows
178+
* to be clean, removing them from the dirty cache and the error cache,
179+
* and clearing the error flag and the dirty flag
180+
* <pre>
181+
* var gridRows = $scope.gridApi.rowEdit.getDirtyRows();
182+
* var dataRows = gridRows.map( function( gridRow ) { return gridRow.entity; });
183+
* $scope.gridApi.rowEdit.setRowsClean( dataRows );
184+
* </pre>
185+
* @param {array} dataRows the data entities for which the gridRows
186+
* should be set clean.
187+
*
188+
*/
189+
setRowsClean: function ( dataRows) {
190+
service.setRowsClean(grid, dataRows);
171191
}
172192
}
173193
}
174194
};
175195

176196
grid.api.registerEventsFromObject(publicApi.events);
177197
grid.api.registerMethodsFromObject(publicApi.methods);
178-
198+
179199
grid.api.core.on.renderingComplete( scope, function ( gridApi ) {
180200
grid.api.edit.on.afterCellEdit( scope, service.endEditCell );
181201
grid.api.edit.on.beginCellEdit( scope, service.beginEditCell );
182202
grid.api.edit.on.cancelCellEdit( scope, service.cancelEditCell );
183-
203+
184204
if ( grid.api.cellNav ) {
185205
grid.api.cellNav.on.navigate( scope, service.navigate );
186-
}
206+
}
187207
});
188208

189209
},
@@ -222,9 +242,9 @@
222242
// don't save the row again if it's already saving - that causes stale object exceptions
223243
return gridRow.rowEditSavePromise;
224244
}
225-
245+
226246
var promise = grid.api.rowEdit.raise.saveRow( gridRow.entity );
227-
247+
228248
if ( gridRow.rowEditSavePromise ){
229249
gridRow.rowEditSavePromise.then( self.processSuccessPromise( grid, gridRow ), self.processErrorPromise( grid, gridRow ));
230250
} else {
@@ -233,7 +253,7 @@
233253
return promise;
234254
};
235255
},
236-
256+
237257

238258
/**
239259
* @ngdoc method
@@ -269,7 +289,7 @@
269289
*/
270290
processSuccessPromise: function ( grid, gridRow ) {
271291
var self = this;
272-
292+
273293
return function() {
274294
delete gridRow.isSaving;
275295
delete gridRow.isDirty;
@@ -280,7 +300,7 @@
280300
self.removeRow( grid.rowEdit.dirtyRows, gridRow );
281301
};
282302
},
283-
303+
284304

285305
/**
286306
* @ngdoc method
@@ -299,7 +319,7 @@
299319
delete gridRow.rowEditSavePromise;
300320

301321
gridRow.isError = true;
302-
322+
303323
if (!grid.rowEdit.errorRows){
304324
grid.rowEdit.errorRows = [];
305325
}
@@ -308,8 +328,8 @@
308328
}
309329
};
310330
},
311-
312-
331+
332+
313333
/**
314334
* @ngdoc method
315335
* @methodOf ui.grid.rowEdit.service:uiGridRowEditService
@@ -324,15 +344,15 @@
324344
if (typeof(rowArray) === 'undefined' || rowArray === null){
325345
return;
326346
}
327-
347+
328348
rowArray.forEach( function( gridRow, index ){
329349
if ( gridRow.uid === removeGridRow.uid ){
330350
rowArray.splice( index, 1);
331351
}
332352
});
333353
},
334-
335-
354+
355+
336356
/**
337357
* @ngdoc method
338358
* @methodOf ui.grid.rowEdit.service:uiGridRowEditService
@@ -352,7 +372,7 @@
352372
return present;
353373
},
354374

355-
375+
356376
/**
357377
* @ngdoc method
358378
* @methodOf ui.grid.rowEdit.service:uiGridRowEditService
@@ -374,11 +394,11 @@
374394
service.saveRow( grid, gridRow )();
375395
promises.push( gridRow.rowEditSavePromise );
376396
});
377-
397+
378398
return $q.all( promises );
379399
},
380-
381-
400+
401+
382402
/**
383403
* @ngdoc method
384404
* @methodOf ui.grid.rowEdit.service:uiGridRowEditService
@@ -389,7 +409,7 @@
389409
* is automatically provided by the gridApi.
390410
* @param {object} rowEntity the data entity for which the cell
391411
* was edited
392-
*/
412+
*/
393413
endEditCell: function( rowEntity, colDef, newValue, previousValue ){
394414
var grid = this.grid;
395415
var gridRow = grid.getRow( rowEntity );
@@ -399,19 +419,19 @@
399419
if ( !grid.rowEdit.dirtyRows ){
400420
grid.rowEdit.dirtyRows = [];
401421
}
402-
422+
403423
if ( !gridRow.isDirty ){
404424
gridRow.isDirty = true;
405425
grid.rowEdit.dirtyRows.push( gridRow );
406426
}
407-
427+
408428
delete gridRow.isError;
409-
429+
410430
service.considerSetTimer( grid, gridRow );
411431
}
412432
},
413-
414-
433+
434+
415435
/**
416436
* @ngdoc method
417437
* @methodOf ui.grid.rowEdit.service:uiGridRowEditService
@@ -428,7 +448,7 @@
428448
var grid = this.grid;
429449
var gridRow = grid.getRow( rowEntity );
430450
if ( !gridRow ){ gridUtil.logError( 'Unable to find rowEntity in grid data, timer cannot be cancelled' ); return; }
431-
451+
432452
service.cancelTimer( grid, gridRow );
433453
},
434454

@@ -440,23 +460,23 @@
440460
* @description Receives a cancelCellEdit event from the edit function,
441461
* and if the row was already dirty, restarts the save timer. If the row
442462
* was not already dirty, then it's not dirty now either and does nothing.
443-
*
463+
*
444464
* Only the rowEntity parameter
445465
* is processed, although other params are available. Grid
446466
* is automatically provided by the gridApi.
447-
*
467+
*
448468
* @param {object} rowEntity the data entity for which the cell
449469
* editing was cancelled
450-
*/
470+
*/
451471
cancelEditCell: function( rowEntity, colDef ){
452472
var grid = this.grid;
453473
var gridRow = grid.getRow( rowEntity );
454474
if ( !gridRow ){ gridUtil.logError( 'Unable to find rowEntity in grid data, timer cannot be set' ); return; }
455-
475+
456476
service.considerSetTimer( grid, gridRow );
457477
},
458-
459-
478+
479+
460480
/**
461481
* @ngdoc method
462482
* @methodOf ui.grid.rowEdit.service:uiGridRowEditService
@@ -479,8 +499,8 @@
479499
service.considerSetTimer( grid, oldRowCol.row );
480500
}
481501
},
482-
483-
502+
503+
484504
/**
485505
* @ngdoc property
486506
* @propertyOf ui.grid.rowEdit.api:GridOptions
@@ -510,15 +530,15 @@
510530
*/
511531
considerSetTimer: function( grid, gridRow ){
512532
service.cancelTimer( grid, gridRow );
513-
533+
514534
if ( gridRow.isDirty && !gridRow.isSaving ){
515535
if ( grid.options.rowEditWaitInterval !== -1 ){
516536
var waitTime = grid.options.rowEditWaitInterval ? grid.options.rowEditWaitInterval : 2000;
517537
gridRow.rowEditSaveTimer = $interval( service.saveRow( grid, gridRow ), waitTime, 1);
518538
}
519539
}
520540
},
521-
541+
522542

523543
/**
524544
* @ngdoc method
@@ -565,22 +585,52 @@
565585
if ( !grid.rowEdit.dirtyRows ){
566586
grid.rowEdit.dirtyRows = [];
567587
}
568-
588+
569589
if ( !gridRow.isDirty ){
570590
gridRow.isDirty = true;
571591
grid.rowEdit.dirtyRows.push( gridRow );
572592
}
573-
593+
574594
delete gridRow.isError;
575-
595+
576596
service.considerSetTimer( grid, gridRow );
577597
} else {
578598
gridUtil.logError( "requested row not found in rowEdit.setRowsDirty, row was: " + value );
579599
}
580600
});
601+
},
602+
603+
604+
/**
605+
* @ngdoc method
606+
* @methodOf ui.grid.rowEdit.service:uiGridRowEditService
607+
* @name setRowsClean
608+
* @description Sets each of the rows passed in dataRows
609+
* to be clean, clearing the dirty flag and the error flag, and removing
610+
* the rows from the dirty and error caches.
611+
* @param {object} grid the grid for which rows should be set clean
612+
* @param {array} dataRows the data entities for which the gridRows
613+
* should be set clean.
614+
*
615+
*/
616+
setRowsClean: function( grid, myDataRows ) {
617+
var gridRow;
618+
619+
myDataRows.forEach( function( value, index ){
620+
gridRow = grid.getRow( value );
621+
if ( gridRow ){
622+
delete gridRow.isDirty;
623+
service.removeRow( grid.rowEdit.dirtyRows, gridRow );
624+
service.cancelTimer( grid, gridRow );
625+
626+
delete gridRow.isError;
627+
service.removeRow( grid.rowEdit.errorRows, gridRow );
628+
} else {
629+
gridUtil.logError( "requested row not found in rowEdit.setRowsClean, row was: " + value );
630+
}
631+
});
581632
}
582-
583-
633+
584634
};
585635

586636
return service;

src/features/row-edit/test/uiGridRowEditService.spec.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('ui.grid.edit uiGridRowEditService', function () {
6060
expect( grid.api.rowEdit.getErrorRows ).toEqual( jasmine.any(Function) );
6161
expect( grid.api.rowEdit.flushDirtyRows ).toEqual( jasmine.any(Function) );
6262
expect( grid.api.rowEdit.setRowsDirty ).toEqual( jasmine.any(Function) );
63+
expect( grid.api.rowEdit.setRowsClean ).toEqual( jasmine.any(Function) );
6364

6465
});
6566

@@ -528,7 +529,7 @@ describe('ui.grid.edit uiGridRowEditService', function () {
528529
expect( grid.rowEdit.errorRows.length ).toEqual(1);
529530
expect( success ).toEqual(false);
530531
expect( failure ).toEqual(true);
531-
});
532+
});
532533
});
533534

534535

@@ -544,5 +545,35 @@ describe('ui.grid.edit uiGridRowEditService', function () {
544545
expect( grid.rows[1].isDirty ).toEqual( true );
545546
expect( grid.rows[1].rowEditSaveTimer ).not.toEqual( undefined );
546547
});
547-
});
548+
});
549+
550+
551+
describe( 'setRowsClean', function() {
552+
it( 'rows are set clean', function() {
553+
uiGridRowEditService.initializeGrid( $scope, grid );
554+
uiGridEditService.initializeGrid( grid );
555+
grid.renderingComplete();
556+
557+
grid.api.rowEdit.setRowsDirty( [ grid.options.data[0], grid.options.data[1] ]);
558+
expect( grid.rows[0].isDirty ).toEqual( true );
559+
expect( grid.rows[0].rowEditSaveTimer ).not.toEqual( undefined );
560+
expect( grid.rows[1].isDirty ).toEqual( true );
561+
expect( grid.rows[1].rowEditSaveTimer ).not.toEqual( undefined );
562+
expect( grid.rowEdit.dirtyRows.length).toEqual(2);
563+
564+
grid.rows[0].isError = true;
565+
grid.rowEdit.errorRows = [];
566+
grid.rowEdit.errorRows.push( grid.rows[0] );
567+
expect( grid.rowEdit.errorRows.length).toEqual(1);
568+
569+
grid.api.rowEdit.setRowsClean( [ grid.options.data[0], grid.options.data[1] ]);
570+
571+
expect( grid.rows[0].isDirty ).toEqual( undefined );
572+
expect( grid.rows[0].rowEditSaveTimer ).toEqual( undefined );
573+
expect( grid.rows[1].isDirty ).toEqual( undefined );
574+
expect( grid.rows[1].rowEditSaveTimer ).toEqual( undefined );
575+
expect( grid.rowEdit.dirtyRows.length).toEqual(0);
576+
expect( grid.rowEdit.errorRows.length).toEqual(0);
577+
});
578+
});
548579
});

0 commit comments

Comments
 (0)