EasyUI Forum
June 28, 2024, 07:09:08 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: How to undo the modification of a single data row in the datagrid?  (Read 5690 times)
officecode
Jr. Member
**
Posts: 69


View Profile Email
« 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);
}
            })
      },
Logged
officecode
Jr. Member
**
Posts: 69


View Profile Email
« Reply #1 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);
}
            })
},
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #2 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;
    }
  }
})
Logged
officecode
Jr. Member
**
Posts: 69


View Profile Email
« Reply #3 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?
« Last Edit: December 05, 2018, 09:36:09 PM by officecode » Logged
officecode
Jr. Member
**
Posts: 69


View Profile Email
« Reply #4 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?
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #5 on: December 06, 2018, 12:04:32 AM »

For the newly added row, you should delete it if you want to cancel the row.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!