EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: officecode on December 05, 2018, 04:14:32 AM



Title: How to undo the modification of a single data row in the datagrid?
Post by: officecode on December 05, 2018, 04:14:32 AM
When I submit the modified data to the server in onAfterEdit, if the submission fails, how can I cancel the modification of the current row? Even if I add the cancelEdit method to it, I can't get back to the original data. I can't use rejectChanges here because it will undo all the changes in the table.
Please help, thank you.

Code:
      onAfterEdit:function(index,row,changes){
            var inserted = $(this).datagrid('getChanges','inserted');
            var updated = $(this).datagrid('getChanges','updated');
            $.post('crud.php',{
                type: (inserted.length > 0) ? 'add' : 'update',
                row:row
            },function(data){
if(data){
            row.editing = false;
            $('#test').datagrid('refreshRow', index);
                }else{
            $('#test').datagrid('cancelEdit', index);
            $('#test').datagrid('beginEdit', index);
}
            })
      },


Title: Re: How to undo the modification of a single data row in the datagrid?
Post by: officecode on December 05, 2018, 07:55:55 PM
I tried to add a data property to the row in the onBeforeEdit event to save the pre-edit value, then get the data in onAfterEdit and set the value to the row.
However, it is invalid!

Code:
onBeforeEdit:function(index,row){
            row.editing = true;
            $(this).datagrid('refreshRow', index);
      row.data = row;
},

onAfterEdit:function(index,row,changes){
            var oldrow = row.data;
            delete row.data;
            var inserted = $(this).datagrid('getChanges','inserted');
            var updated = $(this).datagrid('getChanges','updated');
            $.post('crud.php',{
                type: (inserted.length > 0) ? 'add' : 'update',
                row:row
            },function(data){
if(data){
            row.editing = false;
            $('#test').datagrid('refreshRow', index);
                }else{
                            row = oldrow;
            $('#test').datagrid('cancelEdit', index);
            $('#test').datagrid('beginEdit', index);
}
            })
},


Title: Re: How to undo the modification of a single data row in the datagrid?
Post by: stworthy on December 05, 2018, 08:35:19 PM
Please look at this code. It does no any changes although editing it.
Code:
$('#dg').datagrid({
  onBeforeEdit: function(index,row){
    row.originalData = $.extend({},row);
  },
  onAfterEdit: function(index,row){
    if (row.originalData){
      $(this).datagrid('updateRow', {index:index,row:row.originalData});
      row.originalData = null;
    }
  }
})


Title: Re: How to undo the modification of a single data row in the datagrid?
Post by: officecode on December 05, 2018, 09:06:21 PM
Oh, it's my object reference problem.
Thank you for your reply and help!

After testing, if all the columns have content before editing, this processing method is no problem. If it is a newly added row, the row in the onBeforeEdit event is {}, and then it is useless when updating with the updateRow method in the onAfterEdit event.
How can I get the full value of the row in the onBeforeEdit event?


Title: Re: How to undo the modification of a single data row in the datagrid?
Post by: officecode on December 05, 2018, 09:48:26 PM
Moreover, even if data is restored using updateRow, this data row will still exist when using ‘getChanges’.
Is there any way to deal with it more perfect?


Title: Re: How to undo the modification of a single data row in the datagrid?
Post by: stworthy on December 06, 2018, 12:04:32 AM
For the newly added row, you should delete it if you want to cancel the row.