Hi,
On my view, I have a grid with editing enabled and set to Batch edit mode. I have also enabled the search bar in the grid Header
When I edit one of the rows and I enter a value to search for, I get a warning "Unsaved changes will be lost. Are you sure you want to continue?". When I click OK, the search results are displayed, but the values I entered were reset to the original value based on the grid data source.
I understand that this is normal behavior for the grid when in batch editing mode, but is there a way to override this?
I am using batch edit mode as I am doing a mass save to database using all changed records. The changed records are pushed to an array on button click and then passed as JSON data to my controller. I would like to keep this functionality, but avoid losing any saved data when searching the grid.
|
<script>
var SearchInEdit = false;
function actionBegin(args) {
if (args.requestType == 'searching') {
var gridIns = document.getElementById('Grid').ej2_instances[0];
var batchChanges = gridIns.getBatchChanges();
if ( batchChanges.addedRecords.length > 0 || batchChanges.deletedRecords.length > 0 || batchChanges.changedRecords.length > 0) {
SearchInEdit = true;
args.cancel = true; // prevent the search action
// execute the batchSave method to save the edited data to Grid’s dataSource
setTimeout(() => {
gridIns.editModule.batchSave(); // save the changes
}, 200);
}
}
}
function actionComplete(args) {
if (args.requestType == 'batchsave' && SearchInEdit) {
var gridIns = document.getElementById('Grid').ej2_instances[0];
SearchInEdit = false;
// search the Grid once the save action done
gridIns.search(gridIns.element.querySelector('#' + gridIns.element.id + '_searchbar').value.toLowerCase());
}
}
</script>
|
Thank you.
This works as expected, but I have one more requirement:
I need to post all changed records to my controller. Once the batchSave method is triggered, there will no longer be any changed records.
How do I go about capturing the changed records to be sent to my controller upon button click?
I currently use the changedRecords to send to my controller.
Hi.
Apologies for only getting back to you now. I've managed to work around this issue by using BeforeBatchSave method on the grid. Here I use a javascript function to build an array of unique grid rows to pass through to my controller or to my secondary view.