Virajsinh

row_reorder

Nov 5th, 2019
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. RowReorder DataTable : Static (Using foreach loop Data)
  2. Ref = https://www.itsolutionstuff.com/post/php-dynamic-drag-and-drop-table-rows-using-jquery-ajaxexample.html
  3. ----------------------------------------------------------
  4. 1. Add Library
  5.     - jquery-ui.min.js
  6.     OR
  7.     - <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
  8.  
  9. 2. <tbody class="row_position">
  10.  
  11. 3. <tr id="<?php echo $user['id'] ?>">
  12.  
  13. 4. Add Script
  14. <script type="text/javascript">
  15.     $( ".row_position" ).sortable({
  16.         delay: 150,
  17.         stop: function() {
  18.             var selectedData = new Array();
  19.             $('.row_position>tr').each(function() {
  20.                 selectedData.push($(this).attr("id"));
  21.             });
  22.             updateOrder(selectedData);
  23.         }
  24.     });
  25.     function updateOrder(data) {
  26.         $.ajax({
  27.             url:"url.php",
  28.             type:'post',
  29.             data:{position:data},
  30.             success:function(){
  31.                 alert('your change successfully saved');
  32.             }
  33.         })
  34.     }
  35. </script>
  36.  
  37. 5. URL.php
  38. $position = $_POST['position'];
  39. $i=1;
  40. foreach($position as $k=>$v){
  41.     $sql = "UPDATE tbl_name SET in_order=".$i." WHERE id=".$v;
  42.     $mysqli->query($sql);
  43.     $i++;
  44. }
  45. --------------------------------------------------------------------------
  46. help : "in_order" is table column name in table.
Add Comment
Please, Sign In to add comment