bootstrap-table-multiple-selection-row.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define([], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory();
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory();
  11. global.bootstrapTableMultipleSelectionRow = mod.exports;
  12. }
  13. })(this, function () {
  14. "use strict";
  15. /**
  16. * @author: Dennis Hernández
  17. * @webSite: http://djhvscf.github.io/Blog
  18. * @version: v1.0.0
  19. */
  20. !function ($) {
  21. 'use strict';
  22. document.onselectstart = function () {
  23. return false;
  24. };
  25. var getTableObjectFromCurrentTarget = function getTableObjectFromCurrentTarget(currentTarget) {
  26. currentTarget = $(currentTarget);
  27. return currentTarget.is("table") ? currentTarget : currentTarget.parents().find(".table");
  28. };
  29. var getRow = function getRow(target) {
  30. target = $(target);
  31. return target.parent().parent();
  32. };
  33. var onRowClick = function onRowClick(e) {
  34. var that = getTableObjectFromCurrentTarget(e.currentTarget);
  35. if (window.event.ctrlKey) {
  36. toggleRow(e.currentTarget, that, false, false);
  37. }
  38. if (window.event.button === 0) {
  39. if (!window.event.ctrlKey && !window.event.shiftKey) {
  40. clearAll(that);
  41. toggleRow(e.currentTarget, that, false, false);
  42. }
  43. if (window.event.shiftKey) {
  44. selectRowsBetweenIndexes([that.bootstrapTable("getOptions").multipleSelectRowLastSelectedRow.rowIndex, e.currentTarget.rowIndex], that);
  45. }
  46. }
  47. };
  48. var onCheckboxChange = function onCheckboxChange(e) {
  49. var that = getTableObjectFromCurrentTarget(e.currentTarget);
  50. clearAll(that);
  51. toggleRow(getRow(e.currentTarget), that, false, false);
  52. };
  53. var toggleRow = function toggleRow(row, that, clearAll, useShift) {
  54. if (clearAll) {
  55. row = $(row);
  56. that.bootstrapTable("getOptions").multipleSelectRowLastSelectedRow = undefined;
  57. row.removeClass(that.bootstrapTable("getOptions").multipleSelectRowCssClass);
  58. that.bootstrapTable("uncheck", row.data("index"));
  59. } else {
  60. that.bootstrapTable("getOptions").multipleSelectRowLastSelectedRow = row;
  61. row = $(row);
  62. if (useShift) {
  63. row.addClass(that.bootstrapTable("getOptions").multipleSelectRowCssClass);
  64. that.bootstrapTable("check", row.data("index"));
  65. } else {
  66. if (row.hasClass(that.bootstrapTable("getOptions").multipleSelectRowCssClass)) {
  67. row.removeClass(that.bootstrapTable("getOptions").multipleSelectRowCssClass);
  68. that.bootstrapTable("uncheck", row.data("index"));
  69. } else {
  70. row.addClass(that.bootstrapTable("getOptions").multipleSelectRowCssClass);
  71. that.bootstrapTable("check", row.data("index"));
  72. }
  73. }
  74. }
  75. };
  76. var selectRowsBetweenIndexes = function selectRowsBetweenIndexes(indexes, that) {
  77. indexes.sort(function (a, b) {
  78. return a - b;
  79. });
  80. for (var i = indexes[0]; i <= indexes[1]; i++) {
  81. toggleRow(that.bootstrapTable("getOptions").multipleSelectRowRows[i - 1], that, false, true);
  82. }
  83. };
  84. var clearAll = function clearAll(that) {
  85. for (var i = 0; i < that.bootstrapTable("getOptions").multipleSelectRowRows.length; i++) {
  86. toggleRow(that.bootstrapTable("getOptions").multipleSelectRowRows[i], that, true, false);
  87. }
  88. };
  89. $.extend($.fn.bootstrapTable.defaults, {
  90. multipleSelectRow: false,
  91. multipleSelectRowCssClass: 'multiple-select-row-selected',
  92. //internal variables used by the extension
  93. multipleSelectRowLastSelectedRow: undefined,
  94. multipleSelectRowRows: []
  95. });
  96. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  97. _init = BootstrapTable.prototype.init,
  98. _initBody = BootstrapTable.prototype.initBody;
  99. BootstrapTable.prototype.init = function () {
  100. if (this.options.multipleSelectRow) {
  101. var that = this;
  102. //Make sure that the internal variables have the correct value
  103. this.options.multipleSelectRowLastSelectedRow = undefined;
  104. this.options.multipleSelectRowRows = [];
  105. this.$el.on("post-body.bs.table", function (e) {
  106. setTimeout(function () {
  107. that.options.multipleSelectRowRows = that.$body.children();
  108. that.options.multipleSelectRowRows.click(onRowClick);
  109. that.options.multipleSelectRowRows.find("input[type=checkbox]").change(onCheckboxChange);
  110. }, 1);
  111. });
  112. }
  113. _init.apply(this, Array.prototype.slice.apply(arguments));
  114. };
  115. BootstrapTable.prototype.clearAllMultipleSelectionRow = function () {
  116. clearAll(this);
  117. };
  118. $.fn.bootstrapTable.methods.push('clearAllMultipleSelectionRow');
  119. }(jQuery);
  120. });