bootstrap-table-reorder-rows.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.bootstrapTableReorderRows = 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.1
  19. */
  20. (function ($) {
  21. 'use strict';
  22. var isSearch = false;
  23. var rowAttr = function rowAttr(row, index) {
  24. return {
  25. id: 'customId_' + index
  26. };
  27. };
  28. $.extend($.fn.bootstrapTable.defaults, {
  29. reorderableRows: false,
  30. onDragStyle: null,
  31. onDropStyle: null,
  32. onDragClass: "reorder_rows_onDragClass",
  33. dragHandle: null,
  34. useRowAttrFunc: false,
  35. onReorderRowsDrag: function onReorderRowsDrag(table, row) {
  36. return false;
  37. },
  38. onReorderRowsDrop: function onReorderRowsDrop(table, row) {
  39. return false;
  40. },
  41. onReorderRow: function onReorderRow(newData) {
  42. return false;
  43. }
  44. });
  45. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  46. 'reorder-row.bs.table': 'onReorderRow'
  47. });
  48. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  49. _init = BootstrapTable.prototype.init,
  50. _initSearch = BootstrapTable.prototype.initSearch;
  51. BootstrapTable.prototype.init = function () {
  52. if (!this.options.reorderableRows) {
  53. _init.apply(this, Array.prototype.slice.apply(arguments));
  54. return;
  55. }
  56. var that = this;
  57. if (this.options.useRowAttrFunc) {
  58. this.options.rowAttributes = rowAttr;
  59. }
  60. var onPostBody = this.options.onPostBody;
  61. this.options.onPostBody = function () {
  62. setTimeout(function () {
  63. that.makeRowsReorderable();
  64. onPostBody.apply();
  65. }, 1);
  66. };
  67. _init.apply(this, Array.prototype.slice.apply(arguments));
  68. };
  69. BootstrapTable.prototype.initSearch = function () {
  70. _initSearch.apply(this, Array.prototype.slice.apply(arguments));
  71. if (!this.options.reorderableRows) {
  72. return;
  73. }
  74. //Known issue after search if you reorder the rows the data is not display properly
  75. //isSearch = true;
  76. };
  77. BootstrapTable.prototype.makeRowsReorderable = function () {
  78. if (this.options.cardView) {
  79. return;
  80. }
  81. var that = this;
  82. this.$el.tableDnD({
  83. onDragStyle: that.options.onDragStyle,
  84. onDropStyle: that.options.onDropStyle,
  85. onDragClass: that.options.onDragClass,
  86. onDrop: that.onDrop,
  87. onDragStart: that.options.onReorderRowsDrag,
  88. dragHandle: that.options.dragHandle
  89. });
  90. };
  91. BootstrapTable.prototype.onDrop = function (table, droppedRow) {
  92. var tableBs = $(table),
  93. tableBsData = tableBs.data('bootstrap.table'),
  94. tableBsOptions = tableBs.data('bootstrap.table').options,
  95. row = null,
  96. newData = [];
  97. for (var i = 0; i < table.tBodies[0].rows.length; i++) {
  98. row = $(table.tBodies[0].rows[i]);
  99. newData.push(tableBsOptions.data[row.data('index')]);
  100. row.data('index', i).attr('data-index', i);
  101. }
  102. tableBsOptions.data = tableBsOptions.data.slice(0, tableBsData.pageFrom - 1).concat(newData).concat(tableBsOptions.data.slice(tableBsData.pageTo));
  103. //Call the user defined function
  104. tableBsOptions.onReorderRowsDrop.apply(table, [table, droppedRow]);
  105. //Call the event reorder-row
  106. tableBsData.trigger('reorder-row', newData);
  107. };
  108. })(jQuery);
  109. });