bootstrap-table-toolbar.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @author: aperez <aperez@datadec.es>
  3. * @version: v2.0.0
  4. *
  5. * @update Dennis Hernández <http://djhvscf.github.io/Blog>
  6. * @update zhixin wen <wenzhixin2010@gmail.com>
  7. */
  8. ($ => {
  9. const Utils = $.fn.bootstrapTable.utils
  10. const bootstrap = {
  11. 3: {
  12. icons: {
  13. advancedSearchIcon: 'glyphicon-chevron-down'
  14. },
  15. html: {
  16. modalHeader: `
  17. <div class="modal-header">
  18. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  19. <span aria-hidden="true">&times;</span>
  20. </button>
  21. <h4 class="modal-title">%s</h4>
  22. </div>
  23. `
  24. }
  25. },
  26. 4: {
  27. icons: {
  28. advancedSearchIcon: 'fa-chevron-down'
  29. },
  30. html: {
  31. modalHeader: `
  32. <div class="modal-header">
  33. <h4 class="modal-title">%s</h4>
  34. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  35. <span aria-hidden="true">&times;</span>
  36. </button>
  37. </div>
  38. `
  39. }
  40. }
  41. }[Utils.bootstrapVersion]
  42. $.extend($.fn.bootstrapTable.defaults, {
  43. advancedSearch: false,
  44. idForm: 'advancedSearch',
  45. actionForm: '',
  46. idTable: undefined,
  47. onColumnAdvancedSearch (field, text) {
  48. return false
  49. }
  50. })
  51. $.extend($.fn.bootstrapTable.defaults.icons, {
  52. advancedSearchIcon: bootstrap.icons.advancedSearchIcon
  53. })
  54. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  55. 'column-advanced-search.bs.table': 'onColumnAdvancedSearch'
  56. })
  57. $.extend($.fn.bootstrapTable.locales, {
  58. formatAdvancedSearch () {
  59. return 'Advanced search'
  60. },
  61. formatAdvancedCloseButton () {
  62. return 'Close'
  63. }
  64. })
  65. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
  66. $.BootstrapTable = class extends $.BootstrapTable {
  67. initToolbar () {
  68. const o = this.options
  69. this.showToolbar = this.showToolbar ||
  70. (o.search &&
  71. o.advancedSearch &&
  72. o.idTable)
  73. super.initToolbar()
  74. if (!o.search || !o.advancedSearch || !o.idTable) {
  75. return
  76. }
  77. this.$toolbar.find('>.btn-group').append(`
  78. <button class="btn btn-default${Utils.sprintf(' btn-%s', o.buttonsClass)}${Utils.sprintf(' btn-%s', o.iconSize)}"
  79. type="button"
  80. name="advancedSearch"
  81. aria-label="advanced search"
  82. title="${o.formatAdvancedSearch()}">
  83. <i class="${o.iconsPrefix} ${o.icons.advancedSearchIcon}"></i>
  84. </button>
  85. `)
  86. this.$toolbar.find('button[name="advancedSearch"]').off('click').on('click', () => this.showAvdSearch())
  87. }
  88. showAvdSearch () {
  89. const o = this.options
  90. if (!$(`#avdSearchModal_${o.idTable}`).hasClass('modal')) {
  91. $('body').append(`
  92. <div id="avdSearchModal_${o.idTable}" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  93. <div class="modal-dialog modal-xs">
  94. <div class="modal-content">
  95. ${Utils.sprintf(bootstrap.html.modalHeader, o.formatAdvancedSearch())}
  96. <div class="modal-body modal-body-custom">
  97. <div class="container-fluid" id="avdSearchModalContent_${o.idTable}"
  98. style="padding-right: 0px; padding-left: 0px;" >
  99. </div>
  100. </div>
  101. <div class="modal-footer">
  102. <button type="button" id="btnCloseAvd_${o.idTable}" class="btn btn-${o.buttonsClass}">
  103. ${o.formatAdvancedCloseButton()}
  104. </button>
  105. </div>
  106. </div>
  107. </div>
  108. </div>
  109. `)
  110. let timeoutId = 0
  111. $(`#avdSearchModalContent_${o.idTable}`).append(this.createFormAvd().join(''))
  112. $(`#${o.idForm}`).off('keyup blur', 'input').on('keyup blur', 'input', e => {
  113. if (o.sidePagination === 'server') {
  114. this.onColumnAdvancedSearch(e)
  115. } else {
  116. clearTimeout(timeoutId)
  117. timeoutId = setTimeout(() => {
  118. this.onColumnAdvancedSearch(e)
  119. }, o.searchTimeOut)
  120. }
  121. })
  122. $(`#btnCloseAvd_${o.idTable}`).click(() => {
  123. $(`#avdSearchModal_${o.idTable}`).modal('hide')
  124. if (o.sidePagination === 'server') {
  125. this.options.pageNumber = 1
  126. this.updatePagination()
  127. this.trigger('column-advanced-search', this.filterColumnsPartial)
  128. }
  129. })
  130. $(`#avdSearchModal_${o.idTable}`).modal()
  131. } else {
  132. $(`#avdSearchModal_${o.idTable}`).modal()
  133. }
  134. }
  135. createFormAvd () {
  136. const o = this.options
  137. const html = [`<form class="form-horizontal" id="${o.idForm}" action="${o.actionForm}">`]
  138. for (let column of this.columns) {
  139. if (!column.checkbox && column.visible && column.searchable) {
  140. html.push(`
  141. <div class="form-group row">
  142. <label class="col-sm-4 control-label">${column.title}</label>
  143. <div class="col-sm-6">
  144. <input type="text" class="form-control input-md" name="${column.field}" placeholder="${column.title}" id="${column.field}">
  145. </div>
  146. </div>
  147. `)
  148. }
  149. }
  150. html.push('</form>')
  151. return html
  152. }
  153. initSearch () {
  154. super.initSearch()
  155. if (!this.options.advancedSearch || this.options.sidePagination === 'server') {
  156. return
  157. }
  158. const fp = $.isEmptyObject(this.filterColumnsPartial) ? null : this.filterColumnsPartial
  159. this.data = fp ? $.grep(this.data, (item, i) => {
  160. for (const key in fp) {
  161. const fval = fp[key].toLowerCase()
  162. let value = item[key]
  163. const index = this.header.fields.indexOf(key)
  164. value = Utils.calculateObjectValue(this.header, this.header.formatters[index], [value, item, i], value)
  165. if (
  166. !(index !== -1 &&
  167. (typeof value === 'string' || typeof value === 'number') &&
  168. (`${value}`).toLowerCase().includes(fval))
  169. ) {
  170. return false
  171. }
  172. }
  173. return true
  174. }) : this.data
  175. }
  176. onColumnAdvancedSearch (e) {
  177. const text = $.trim($(e.currentTarget).val())
  178. const $field = $(e.currentTarget)[0].id
  179. if ($.isEmptyObject(this.filterColumnsPartial)) {
  180. this.filterColumnsPartial = {}
  181. }
  182. if (text) {
  183. this.filterColumnsPartial[$field] = text
  184. } else {
  185. delete this.filterColumnsPartial[$field]
  186. }
  187. if (this.options.sidePagination !== 'server') {
  188. this.options.pageNumber = 1
  189. this.onSearch(e)
  190. this.updatePagination()
  191. this.trigger('column-advanced-search', $field, text)
  192. }
  193. }
  194. }
  195. })(jQuery)