bootstrap-table-select2-filter.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * @author: Jewway
  3. * @version: v1.1.1
  4. */
  5. function getCurrentHeader (that) {
  6. let header = that.$header
  7. if (that.options.height) {
  8. header = that.$tableHeader
  9. }
  10. return header
  11. }
  12. function initFilterValues (that) {
  13. if (!$.isEmptyObject(that.filterColumnsPartial)) {
  14. const $header = getCurrentHeader(that)
  15. $.each(that.columns, (idx, column) => {
  16. const value = that.filterColumnsPartial[column.field]
  17. if (column.filter) {
  18. if (column.filter.setFilterValue) {
  19. const $filter = $header.find(`[data-field=${column.field}] .filter`)
  20. column.filter.setFilterValue($filter, column.field, value)
  21. } else {
  22. const $ele = $header.find(`[data-filter-field=${column.field}]`)
  23. switch (column.filter.type) {
  24. case 'input':
  25. $ele.val(value)
  26. break
  27. case 'select':
  28. $ele.val(value).trigger('change')
  29. break
  30. default:
  31. break
  32. }
  33. }
  34. }
  35. })
  36. }
  37. }
  38. function createFilter (that, header) {
  39. let enableFilter = false
  40. let isVisible
  41. let html
  42. let timeoutId = 0
  43. $.each(that.columns, (i, column) => {
  44. isVisible = 'hidden'
  45. html = null
  46. if (!column.visible) {
  47. return
  48. }
  49. if (!column.filter) {
  50. html = $('<div class="no-filter"></div>')
  51. } else {
  52. const filterClass = column.filter.class ? ` ${column.filter.class}` : ''
  53. html = $(`<div style="margin: 0px 2px 2px 2px;" class="filter${filterClass}">`)
  54. if (column.searchable) {
  55. enableFilter = true
  56. isVisible = 'visible'
  57. }
  58. if (column.filter.template) {
  59. html.append(column.filter.template(that, column, isVisible))
  60. } else {
  61. const $filter = $(that.options.filterTemplate[column.filter.type.toLowerCase()](that, column, isVisible))
  62. switch (column.filter.type) {
  63. case 'input':
  64. let cpLock = true
  65. $filter.off('compositionstart').on('compositionstart', event => {
  66. cpLock = false
  67. })
  68. $filter.off('compositionend').on('compositionend', function (event) {
  69. cpLock = true
  70. const $input = $(this)
  71. clearTimeout(timeoutId)
  72. timeoutId = setTimeout(() => {
  73. that.onColumnSearch(event, column.field, $input.val())
  74. }, that.options.searchTimeOut)
  75. })
  76. $filter.off('keyup').on('keyup', function (event) {
  77. if (cpLock) {
  78. const $input = $(this)
  79. clearTimeout(timeoutId)
  80. timeoutId = setTimeout(() => {
  81. that.onColumnSearch(event, column.field, $input.val())
  82. }, that.options.searchTimeOut)
  83. }
  84. })
  85. $filter.off('mouseup').on('mouseup', function (event) {
  86. const $input = $(this)
  87. const oldValue = $input.val()
  88. if (oldValue === '') {
  89. return
  90. }
  91. setTimeout(() => {
  92. const newValue = $input.val()
  93. if (newValue === '') {
  94. clearTimeout(timeoutId)
  95. timeoutId = setTimeout(() => {
  96. that.onColumnSearch(event, column.field, newValue)
  97. }, that.options.searchTimeOut)
  98. }
  99. }, 1)
  100. })
  101. break
  102. case 'select':
  103. $filter.on('select2:select', function (event) {
  104. that.onColumnSearch(event, column.field, $(this).val())
  105. })
  106. $filter.on('select2:unselecting', function (event) {
  107. const $select2 = $(this)
  108. event.preventDefault()
  109. $select2.val(null).trigger('change')
  110. that.searchText = undefined
  111. that.onColumnSearch(event, column.field, $select2.val())
  112. })
  113. break
  114. default:
  115. break
  116. }
  117. html.append($filter)
  118. }
  119. }
  120. $.each(header.children().children(), (i, tr) => {
  121. tr = $(tr)
  122. if (tr.data('field') === column.field) {
  123. tr.find('.fht-cell').append(html)
  124. return false
  125. }
  126. })
  127. })
  128. if (!enableFilter) {
  129. header.find('.filter').hide()
  130. }
  131. }
  132. function initSelect2 (that) {
  133. const $header = getCurrentHeader(that)
  134. $.each(that.columns, (idx, column) => {
  135. if (column.filter && column.filter.type === 'select') {
  136. const $selectEle = $header.find(`select[data-filter-field="${column.field}"]`)
  137. if ($selectEle.length > 0 && !$selectEle.data().select2) {
  138. const select2Opts = {
  139. placeholder: '',
  140. allowClear: true,
  141. data: column.filter.data,
  142. dropdownParent: that.$el.closest('.bootstrap-table')
  143. }
  144. $selectEle.select2(select2Opts)
  145. }
  146. }
  147. })
  148. }
  149. $.extend($.fn.bootstrapTable.defaults, {
  150. filter: false,
  151. filterValues: {},
  152. filterTemplate: {
  153. input (instance, column, isVisible) {
  154. return `<input type="text" class="form-control" data-filter-field="${column.field}" style="width: 100%; visibility:${isVisible}">`
  155. },
  156. select (instance, column, isVisible) {
  157. return `<select data-filter-field="${column.field}" style="width: 100%; visibility:${isVisible}"></select>`
  158. }
  159. },
  160. onColumnSearch (field, text) {
  161. return false
  162. }
  163. })
  164. $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
  165. filter: undefined
  166. })
  167. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  168. 'column-search.bs.table': 'onColumnSearch'
  169. })
  170. const BootstrapTable = $.fn.bootstrapTable.Constructor
  171. const _init = BootstrapTable.prototype.init
  172. const _initHeader = BootstrapTable.prototype.initHeader
  173. const _initSearch = BootstrapTable.prototype.initSearch
  174. BootstrapTable.prototype.init = function (...args) {
  175. // Make sure that the filtercontrol option is set
  176. if (this.options.filter) {
  177. const that = this
  178. if (that.options.filterTemplate) {
  179. that.options.filterTemplate = $.extend({}, $.fn.bootstrapTable.defaults.filterTemplate, that.options.filterTemplate)
  180. }
  181. if (!$.isEmptyObject(that.options.filterValues)) {
  182. that.filterColumnsPartial = that.options.filterValues
  183. that.options.filterValues = {}
  184. }
  185. this.$el.on('reset-view.bs.table', () => {
  186. // Create controls on $tableHeader if the height is set
  187. if (!that.options.height) {
  188. return
  189. }
  190. // Avoid recreate the controls
  191. if (that.$tableHeader.find('select').length > 0 || that.$tableHeader.find('input').length > 0) {
  192. return
  193. }
  194. createFilter(that, that.$tableHeader)
  195. }).on('post-header.bs.table', () => {
  196. let timeoutId = 0
  197. initSelect2(that)
  198. clearTimeout(timeoutId)
  199. timeoutId = setTimeout(() => {
  200. initFilterValues(that)
  201. }, that.options.searchTimeOut - 1000)
  202. }).on('column-switch.bs.table', (field, checked) => {
  203. initFilterValues(that)
  204. })
  205. }
  206. _init.apply(this, Array.prototype.slice.apply(args))
  207. }
  208. BootstrapTable.prototype.initHeader = function (...args) {
  209. _initHeader.apply(this, Array.prototype.slice.apply(args))
  210. if (this.options.filter) {
  211. createFilter(this, this.$header)
  212. }
  213. }
  214. BootstrapTable.prototype.initSearch = function (...args) {
  215. const that = this
  216. const filterValues = that.filterColumnsPartial
  217. // Filter for client
  218. if (that.options.sidePagination === 'client') {
  219. this.data = this.data.filter((row, idx) => {
  220. for (const field in filterValues) {
  221. if (Object.prototype.hasOwnProperty(field, filterValues)) {
  222. const column = that.columns[that.fieldsColumnsIndex[field]]
  223. const filterValue = filterValues[field].toLowerCase()
  224. let rowValue = row[field]
  225. rowValue = $.fn.bootstrapTable.utils.calculateObjectValue(
  226. that.header,
  227. that.header.formatters[$.inArray(field, that.header.fields)], [rowValue, row, idx], rowValue)
  228. if (column.filterStrictSearch) {
  229. if (!($.inArray(field, that.header.fields) !== -1 &&
  230. (typeof rowValue === 'string' || typeof rowValue === 'number') &&
  231. rowValue.toString().toLowerCase() === filterValue.toString().toLowerCase())) {
  232. return false
  233. }
  234. } else {
  235. if (!($.inArray(field, that.header.fields) !== -1 &&
  236. (typeof rowValue === 'string' || typeof rowValue === 'number') &&
  237. (`${rowValue}`).toLowerCase().includes(filterValue))) {
  238. return false
  239. }
  240. }
  241. }
  242. }
  243. return true
  244. })
  245. }
  246. _initSearch.apply(this, Array.prototype.slice.apply(args))
  247. }
  248. BootstrapTable.prototype.onColumnSearch = function (event, field, value) {
  249. if ($.isEmptyObject(this.filterColumnsPartial)) {
  250. this.filterColumnsPartial = {}
  251. }
  252. if (value) {
  253. this.filterColumnsPartial[field] = value
  254. } else {
  255. delete this.filterColumnsPartial[field]
  256. }
  257. this.options.pageNumber = 1
  258. this.onSearch(event)
  259. this.trigger('column-search', field, value)
  260. }
  261. BootstrapTable.prototype.setSelect2Data = function (field, data) {
  262. const that = this
  263. const $header = getCurrentHeader(that)
  264. const $selectEle = $header.find(`select[data-filter-field="${field}"]`)
  265. $selectEle.empty()
  266. $selectEle.select2({
  267. data,
  268. placeholder: '',
  269. allowClear: true,
  270. dropdownParent: that.$el.closest('.bootstrap-table')
  271. })
  272. $.each(this.columns, (idx, column) => {
  273. if (column.field === field) {
  274. column.filter.data = data
  275. return false
  276. }
  277. })
  278. }
  279. BootstrapTable.prototype.setFilterValues = function (values) {
  280. this.filterColumnsPartial = values
  281. }
  282. $.fn.bootstrapTable.methods.push('setSelect2Data')
  283. $.fn.bootstrapTable.methods.push('setFilterValues')