Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. $(document).ready(function(){
  2. //put all the table rows in a variable after page load to pass in to RowClick
  3. var trs = $('#main-table tr')
  4. //bind the click handler to all the table rows
  5. $('tr').on('click', function(){
  6. //call the RowClick function on click event
  7. RowClick($(this),false,trs)
  8. })
  9. })
  10.  
  11. //declare variable to store the most recently clicked row
  12. var lastSelectedRow;
  13.  
  14. // disable text selection
  15. document.onselectstart = function() {
  16. return false;
  17. }
  18.  
  19. function RowClick(currentrow, lock, rows) {
  20. //if control is held down, toggle the row
  21. if (window.event.ctrlKey) {
  22. toggleRow(currentrow);
  23. }
  24.  
  25. //if there are no buttons held down...
  26. if (window.event.button === 0) {
  27.  
  28. //if neither control or shift are held down...
  29. if (!window.event.ctrlKey && !window.event.shiftKey) {
  30. //clear selection
  31. clearAll(rows);
  32. //toggle clicked row
  33. toggleRow(currentrow);
  34. }
  35.  
  36. //if shift is held down...
  37. if (window.event.shiftKey) {
  38. //pass the indexes of the last selected row and currently selected row along with all rows
  39. selectRowsBetweenIndexes([lastSelectedRow.index(), currentrow.index()], rows)
  40. }
  41. }
  42. }
  43.  
  44. function toggleRow(row) {
  45. //if the row is not the header row...
  46. if (!row.hasClass('header-row')){
  47. //if the row is selected...
  48. if (row.hasClass('selected')){
  49. //deselect it
  50. row.removeClass('selected')
  51. }
  52. else{
  53. //otherwise, select it
  54. row.addClass('selected')
  55. }
  56. //reassign the most recently selected row
  57. lastSelectedRow = row;
  58. }
  59. }
  60.  
  61. function selectRowsBetweenIndexes(indexes,rows) {
  62. //sort the indexes in ascending order
  63. indexes.sort(function(a, b) {
  64. return a - b;
  65. });
  66.  
  67. //for every row starting at the first index, until the second index...
  68. for (var i = indexes[0]; i <= indexes[1]; i++) {
  69. //select the row
  70. $(rows[i+1]).addClass('selected');
  71. }
  72. }
  73.  
  74. function clearAll(rows) {
  75. //for all rows...
  76. for (var i = 0; i < rows.length; i++) {
  77. //deselect each row
  78. $(rows[i]).removeClass("selected");
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement