EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: itay-g on August 03, 2014, 12:01:20 AM



Title: Swap rows in grid
Post by: itay-g on August 03, 2014, 12:01:20 AM
Hi,
I'm trying to swap rows in a grid. I'm doing it by using 'remove' method to remove the selected row and 'insert' method to insert the row into the new position. I noticed that when I'm doing that, the grid data doesn't update to the new order of the rows and that makes the swap look really odd because the rows move in a strange order(for example: suddenly one row goes to the bottom of the grid).
Is there a way to make the grid's data change?

Thank you very much  :)


Title: Re: Swap rows in grid
Post by: stworthy on August 03, 2014, 12:47:29 AM
No 'remove' method exists in the datagrid component, please use 'deleteRow' and 'insertRow' methods instead. It is easy to extend a new method to move a row in a datagrid component.
Code:
$.extend($.fn.datagrid.methods, {
moveRow:function(jq, param){
return jq.each(function(){
var dg = $(this);
var fromRow = dg.datagrid('getRows')[param.from];
var toRow = dg.datagrid('getRows')[param.to];
dg.datagrid('deleteRow', param.from);
var toIndex = dg.datagrid('getRowIndex', toRow);
dg.datagrid('insertRow', {
index: toIndex,
row: fromRow
});
})
}
})

The code below shows how to move a row #3 to the new position #5.
Code:
$('#dg').datagrid('moveRow', {
  from: 3,
  to: 5
});


Title: Re: Swap rows in grid
Post by: itay-g on August 03, 2014, 01:51:28 AM
Thank you.
When i wrote grid I meant to a treegrid, and with treegrid i can't work with indexes because the hierarchy.
Is there another way to make this work?

thanks