Categories: Dynamics 365

Row Reorder by Drag and move rows in DataTable plug-in.

Introduction

In my last blog I explain how to display a grid using DataTable plugin. In this blog I’m going to explain how we can add feature of Row-Reorder in DataTable. Row Reorder adds the ability for rows in a DataTable to be reordered through click and drag / touch and drag.

Step by Step

  1. We are using jQuery plugin DataTable and for that we must add following CDN links under the <head> tag.
    1. jQuery : https://code.jquery.com/jquery-3.3.1.js
    2. CSS link: https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css
    3. JS link: https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js
  2. For reorder :
    1. JS link: https://cdn.datatables.net/rowreorder/1.2.6/js/dataTables.rowReorder.min.js
    2. CSS link: https://cdn.datatables.net/rowreorder/1.2.6/css/rowReorder.dataTables.min.css
  3. In <body> tag, add table using <table> tag and give appropriate id table as following:
    1. <table id=”ReorderTable” class=”cell-border”>
             <thead>
                 <tr>
                      <th>Sr. No.</th>
                     <th>Full Name</th>
                     <th>Phone Number</th>
                 </tr>
             </thead>
      <tbody>
                 <tr>
                     <td>1</td>
                     <td>Karan Sharma</td>
                     <td>7878787878</td>
                 </tr>
                 <tr>
      <td>2</td>
                     <td>Raju Rastogi</td>
      <td>32323232323</td>
                 </tr>
                 <tr>
      <td>3</td>
                     <td>Anil Rao</td>
                     <td>23232323232</td>
                 </tr>
      </tbody>
      </table>
  4. In < head > tag, add script using <script> tag as following:
    1. <script type=”text/javascript”>
           $(document).ready(function () {
                var table = $(‘#ReorderTable’).DataTable({
                     rowReorder: true
                 });
      });
      </script>
  5. To make GUI more relatable then change cursor symbol when we hover to column while moving.
    1. #ReorderTable tbody td:first-child {
                 cursor: move;
      }

Above example is for implementing DataTable plugin with reorder from scratch. If you already have DataTable Plugin implementer and you want to add Row Reorder feature, then just add reorder related JS and CSS link as above and add following property to DataTable :

rowReorder: {
      dataSrc: ‘<data source that you want to implement reorder>
      }