bootstrap-table-reorder-rows.js 2.8 KB

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