bootstrap-table-reorder-rows.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @author: Dennis Hernández
  3. * @webSite: http://djhvscf.github.io/Blog
  4. * @version: v1.0.0
  5. */
  6. !function ($) {
  7. 'use strict';
  8. var isSearch = false;
  9. var rowAttr = function (row, index) {
  10. return {
  11. id: 'customId_' + index
  12. };
  13. };
  14. $.extend($.fn.bootstrapTable.defaults, {
  15. reorderableRows: false,
  16. onDragStyle: null,
  17. onDropStyle: null,
  18. onDragClass: "reorder_rows_onDragClass",
  19. dragHandle: null,
  20. useRowAttrFunc: false,
  21. onReorderRowsDrag: function (table, row) {
  22. return false;
  23. },
  24. onReorderRowsDrop: function (table, row) {
  25. return false;
  26. },
  27. onReorderRow: function (newData) {
  28. return false;
  29. }
  30. });
  31. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  32. 'reorder-row.bs.table': 'onReorderRow'
  33. });
  34. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  35. _init = BootstrapTable.prototype.init,
  36. _initSearch = BootstrapTable.prototype.initSearch;
  37. BootstrapTable.prototype.init = function () {
  38. if (!this.options.reorderableRows) {
  39. return;
  40. }
  41. var that = this;
  42. if (this.options.useRowAttrFunc) {
  43. this.options.rowAttributes = rowAttr;
  44. }
  45. var onPostBody = this.options.onPostBody;
  46. this.options.onPostBody = function () {
  47. setTimeout(function () {
  48. that.makeRowsReorderable();
  49. onPostBody.apply();
  50. }, 1);
  51. };
  52. _init.apply(this, Array.prototype.slice.apply(arguments));
  53. };
  54. BootstrapTable.prototype.initSearch = function () {
  55. _initSearch.apply(this, Array.prototype.slice.apply(arguments));
  56. if (!this.options.reorderableRows) {
  57. return;
  58. }
  59. //Known issue after search if you reorder the rows the data is not display properly
  60. //isSearch = true;
  61. };
  62. BootstrapTable.prototype.makeRowsReorderable = function () {
  63. if (this.options.cardView) {
  64. return;
  65. }
  66. var that = this;
  67. this.$el.tableDnD({
  68. onDragStyle: that.options.onDragStyle,
  69. onDropStyle: that.options.onDropStyle,
  70. onDragClass: that.options.onDragClass,
  71. onDrop: that.onDrop,
  72. onDragStart: that.options.onReorderRowsDrag,
  73. dragHandle: that.options.dragHandle
  74. });
  75. };
  76. BootstrapTable.prototype.onDrop = function (table, droppedRow) {
  77. var tableBs = $(table),
  78. tableBsData = tableBs.data('bootstrap.table'),
  79. tableBsOptions = tableBs.data('bootstrap.table').options,
  80. row = null,
  81. newData = [];
  82. for (var i = 0; i < table.tBodies[0].rows.length; i++) {
  83. row = $(table.tBodies[0].rows[i]);
  84. newData.push(tableBsOptions.data[row.data('index')]);
  85. row.data('index', i).attr('data-index', i);
  86. }
  87. tableBsOptions.data = newData;
  88. //Call the user defined function
  89. tableBsOptions.onReorderRowsDrop.apply(table, [table, droppedRow]);
  90. //Call the event reorder-row
  91. tableBsData.trigger('reorder-row', newData);
  92. };
  93. }(jQuery);