|
Based on your query, we would like to inform you that by default in EJ2 Grid, while Drag and Drop the rows in the Grid, the changes are made only in the Grid UI and it will not affect the database. |
I know. I want to reorder visually the rows, and ONLY THEN, mark some cells of eordered rows as edited by user (if it was him as edited some values), so the user can submit the changes to server.
|
If you have to preform the Sorting after the row reordering, It will change the row order because the sorting actions need to be performed based on the Grid database, in this case we cannot maintain that row reorder. This is our default behavior. |
I though i should had a sorted column to have the rows ordered. if i have to give up the sorted column to achieve my intentions, it's ok.
|
In your provided information, we
could see that you have used a UpdateCell method, By default in EJ2
Grid editing feature requires a primary key column(unique column) for CRUD
operations. Because all the CRUD actions in the grid component are done based
on the primaryKey. So please ensure this in your sample and use a
unique column in your dataSource as primaryKey column to perform the CRUD
operations. Refer the below document for more information. |
|
function
DetailDataBoundAmostrasOperacoes(e) { |
| As a Database constrain, we cannot able to change the primaryKey column(unique column) value dynamically. The same behavior in Grid also. By default, all the CRUD actions in the grid component are done based on the primaryKey value. So, we are unable to change the primaryKey value. |
I dont want to change primaryKey. I only want a way to the user reorder some rows, by using Drag and Drop and ONLY THEN change some values in those rows, so i can mark them as edit by user, so the user can submit them to server as if was him who made the changes.
Like this, but automatically:
| var grid = new ej.grids.Grid({ editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, mode: 'Batch', newRowPosition: 'Bottom' }, //allowPaging: true, allowSorting: true, //pageSettings: { pageCount: 5 }, allowRowDragAndDrop: true, allowSorting: true, sortSettings: { columns: [{ field: 'Ordem', direction: 'Ascending' }] //For drag and drop to work correctly, only one column should be used }, beforeBatchAdd: function (args) { if (this.currentViewData.length > 0 && args.defaultData.Ordem == 1) args.defaultData.Ordem = Math.max(...this.currentViewData.map(x => x.Ordem)) + args.defaultData.Ordem; }, rowDrop: function (args) { let minIndex = Math.min(args.fromIndex, args.dropIndex); let maxIndex = Math.max(args.fromIndex, args.dropIndex); //Get the index list between the moved rows. let list = this.currentViewData.slice(minIndex, maxIndex + 1).map(x => x.Ordem); //Because the moved row does not change position until the end of this event, we have to know if we are moving up or down. //Because if the row is going up, the row its still in last position, but will receive the smallest or first index cause it will go to the top. //If the row is going down, the moved row it's in the first position but should receive the last or biggest index in the list. if (args.dropIndex == args.fromIndex) //If the row didn't change index, do nothing. return; else if (args.fromIndex > args.dropIndex) list.push(list.shift()); //Shift removes the first index and push puts it at end (moved row is at the top and is going to the last) else list.unshift(list.pop()); //Pop removes the last index and unshift puts it at start (moved row is in the bottom and is going to be at the top) for (var i = minIndex; i <= maxIndex; i++) this.updateCell(i, "Ordem", list.shift()); //Just set the index rows as we are eliminating the indexs elements. }, batchCancel: function (args) { //Makes the order of the rows back to original. Because normal 'batchCancel' only reset the row values leaving the rows in wrong order. this.refresh(); }, toolbar: ['Add', 'Edit', 'Delete', 'Update', 'Cancel'], load: function () { let data = new ej.data.DataManager({ url: "/Home/UrlDatasource", batchUrl: "/Home/BatchUpdate", adaptor: new ej.data.UrlAdaptor }); grid.dataSource = data; }, columns: [ { field: 'Ordem', width: 140, headerText: 'Ordem', allowSorting: false, defaultValue: 1 }, { field: 'OrderID', headerText: 'Order ID', textAlign: 'Right', width: 120, isPrimaryKey: true, allowSorting: false }, { field: 'ShipCity', headerText: 'Ship Country', width: 140, allowSorting: false } ] }); grid.appendTo('#Grid'); }); |
I can move the rows, and the 'Ordem' column is changed to the correct values and the changes are marked as edited by user to be submitted. It's almost perfect.
I only have one problem, that if i try to make a second drag and drop the grid shows the message "Unsaved changes will be lost. Are you sure you want to continue?" and i dont want that. Is it possible to avoid this?