Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

Drag & Drop Rows Between Grids

5.00/5 (2 votes)
30 Mar 2017CPOL 11.6K  
How to drag and drop rows between grids

dnd-phpgrid

You can drag and drop rows between two or more grids using a mouse.

In the demo, we have both orders and employees datagrids on the same page.

PHP
$dg = new C_DataGrid("select * from employees", "employeeNumber", "employees");
$dg->enable_edit("FORM", "CRUD");
$dg->display();

We add the following JavaScript to enable drag and drop between the two grids.

The Ondrop event posts the row data to another URL “save_dropped_row.php”. You must implement this server side script to handle how you would like the row data to be saved.

JavaScript
<script>
$(function(){
  jQuery("#orders").jqGrid("sortableRows", {});
  jQuery("#employees").jqGrid("sortableRows", {});

  jQuery("#orders").jqGrid('gridDnD',{
    connectWith:'#employees',
    drop_opts:{
      hoverClass: "ui-state-active",
    },
    ondrop: function(evt, obj, data){
      $.ajax({
          method: "POST",
          url: "save_dropped_row.php",
          data: data,
        }).done(function( msg ) {
            console.log( "Data Saved: " + msg );
        })
    },
  });  
})
</script>

The complete drag and drog API documentation can be found on jQuery UI draggable and droppable widgets.

Run live demo!

The post Drag & Drop Rows Between Grids appeared first on phpGrid.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)