Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
  2. <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
  3. <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  4. <!------ Include the above in your HEAD tag ---------->
  5.  
  6. <!-- Pulled from http://www.avtex.com/blog/2015/01/27/drag-and-drop-sorting-of-table-rows-in-priority-order/ -->
  7.  
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  9. <html xmlns="http://www.w3.org/1999/xhtml"><head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  11. <title>Steven Ray: Drag and drop sorting of table rows</title>
  12.  
  13. <link href="../demo.css" type="text/css" rel="stylesheet" />
  14.  
  15. <!-- Bootstrap CSS -->
  16. <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
  17.  
  18. <!-- jQuery -->
  19. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
  20.  
  21. <!-- jQuery UI CSS -->
  22. <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
  23.  
  24. <script type="text/javascript">
  25.  
  26. $(document).ready(function() {
  27. //Helper function to keep table row from collapsing when being sorted
  28. var fixHelperModified = function(e, tr) {
  29. var $originals = tr.children();
  30. var $helper = tr.clone();
  31. $helper.children().each(function(index)
  32. {
  33. $(this).width($originals.eq(index).width())
  34. });
  35. return $helper;
  36. };
  37.  
  38. //Make diagnosis table sortable
  39. $("#diagnosis_list tbody").sortable({
  40. helper: fixHelperModified,
  41. stop: function(event,ui) {renumber_table('#diagnosis_list')}
  42. }).disableSelection();
  43.  
  44.  
  45. //Delete button in table rows
  46. $('table').on('click','.btn-delete',function() {
  47. tableID = '#' + $(this).closest('table').attr('id');
  48. r = confirm('Delete this item?');
  49. if(r) {
  50. $(this).closest('tr').remove();
  51. renumber_table(tableID);
  52. }
  53. });
  54.  
  55. });
  56.  
  57. //Renumber table rows
  58. function renumber_table(tableID) {
  59. $(tableID + " tr").each(function() {
  60. count = $(this).parent().children().index($(this)) + 1;
  61. $(this).find('.priority').html(count);
  62. });
  63. }
  64.  
  65.  
  66. </script>
  67.  
  68. <style type="text/css">
  69. .ui-sortable tr {
  70. cursor:pointer;
  71. }
  72.  
  73. .ui-sortable tr:hover {
  74. background:rgba(244,251,17,0.45);
  75. }
  76.  
  77. </style>
  78.  
  79. </head>
  80.  
  81. <body>
  82.  
  83. <div id="content" class="container">
  84.  
  85. <h1>Sortable table</h1>
  86.  
  87. <table class="table" id="diagnosis_list">
  88. <thead>
  89. <tr><th>Priority</th><th>Name</th><th>Favorite fruit</th><th>Vegetarian?</th><th> </th></tr>
  90. </thead>
  91. <tbody>
  92. <tr><td class='priority'>1</td><td>George Washington</td><td>Apple</td><td>N</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
  93. <tr><td class='priority'>2</td><td>John Adams</td><td>Pear</td><td>Y</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
  94. <tr><td class='priority'>3</td><td>Thomas Jefferson</td><td>Banana</td><td>Y</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
  95. <tr><td class='priority'>4</td><td>Ben Franklin</td><td>Kumquat</td><td>N</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
  96. <tr><td class='priority'>5</td><td>Alexander Hamilton</td><td>Red grapes</td><td>N</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
  97. </tbody>
  98. </table>
  99.  
  100. </div>
  101.  
  102. </body>
  103. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement