bootstrap-table-filter-control.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /**
  2. * @author: Dennis Hernández
  3. * @webSite: http://djhvscf.github.io/Blog
  4. * @version: v2.3.0
  5. */
  6. import * as UtilsFilterControl from './utils.js'
  7. const Utils = $.fn.bootstrapTable.utils
  8. $.extend($.fn.bootstrapTable.defaults, {
  9. filterControl: false,
  10. filterControlVisible: true,
  11. onColumnSearch (field, text) {
  12. return false
  13. },
  14. onCreatedControls () {
  15. return false
  16. },
  17. alignmentSelectControlOptions: undefined,
  18. filterTemplate: {
  19. input (that, field, placeholder, value) {
  20. return Utils.sprintf(
  21. '<input type="text" class="form-control bootstrap-table-filter-control-%s search-input" style="width: 100%;" placeholder="%s" value="%s">',
  22. field,
  23. 'undefined' === typeof placeholder ? '' : placeholder,
  24. 'undefined' === typeof value ? '' : value
  25. )
  26. },
  27. select ({options}, field) {
  28. return Utils.sprintf(
  29. '<select class="form-control bootstrap-table-filter-control-%s" style="width: 100%;" dir="%s"></select>',
  30. field,
  31. UtilsFilterControl.getDirectionOfSelectOptions(
  32. options.alignmentSelectControlOptions
  33. )
  34. )
  35. },
  36. datepicker (that, field, value) {
  37. return Utils.sprintf(
  38. '<input type="text" class="form-control date-filter-control bootstrap-table-filter-control-%s" style="width: 100%;" value="%s">',
  39. field,
  40. 'undefined' === typeof value ? '' : value
  41. )
  42. }
  43. },
  44. disableControlWhenSearch: false,
  45. searchOnEnterKey: false,
  46. showFilterControlSwitch: false,
  47. // internal variables
  48. valuesFilterControl: []
  49. })
  50. $.extend($.fn.bootstrapTable.columnDefaults, {
  51. filterControl: undefined, // input, select, datepicker
  52. filterDataCollector: undefined,
  53. filterData: undefined,
  54. filterDatepickerOptions: undefined,
  55. filterStrictSearch: false,
  56. filterStartsWithSearch: false,
  57. filterControlPlaceholder: '',
  58. filterDefault: '',
  59. filterOrderBy: 'asc' // asc || desc
  60. })
  61. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  62. 'column-search.bs.table': 'onColumnSearch',
  63. 'created-controls.bs.table': 'onCreatedControls'
  64. })
  65. $.extend($.fn.bootstrapTable.defaults.icons, {
  66. clear: {
  67. bootstrap3: 'glyphicon-trash icon-clear'
  68. }[$.fn.bootstrapTable.theme] || 'fa-trash',
  69. filterControlSwitchHide: {
  70. bootstrap3: 'glyphicon-zoom-out icon-zoom-out',
  71. materialize: 'zoom_out'
  72. }[$.fn.bootstrapTable.theme] || 'fa-search-minus',
  73. filterControlSwitchShow: {
  74. bootstrap3: 'glyphicon-zoom-in icon-zoom-in',
  75. materialize: 'zoom_in'
  76. }[$.fn.bootstrapTable.theme] || 'fa-search-plus'
  77. })
  78. $.extend($.fn.bootstrapTable.locales, {
  79. formatFilterControlSwitch () {
  80. return 'Hide/Show controls'
  81. },
  82. formatFilterControlSwitchHide () {
  83. return 'Hide controls'
  84. },
  85. formatFilterControlSwitchShow () {
  86. return 'Show controls'
  87. }
  88. })
  89. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
  90. $.extend($.fn.bootstrapTable.defaults, {
  91. formatClearSearch () {
  92. return 'Clear filters'
  93. }
  94. })
  95. $.fn.bootstrapTable.methods.push('triggerSearch')
  96. $.fn.bootstrapTable.methods.push('clearFilterControl')
  97. $.fn.bootstrapTable.methods.push('toggleFilterControl')
  98. $.BootstrapTable = class extends $.BootstrapTable {
  99. init () {
  100. // Make sure that the filterControl option is set
  101. if (this.options.filterControl) {
  102. // Make sure that the internal variables are set correctly
  103. this.options.valuesFilterControl = []
  104. this.$el
  105. .on('reset-view.bs.table', () => {
  106. // Create controls on $tableHeader if the height is set
  107. if (!this.options.height) {
  108. return
  109. }
  110. // Avoid recreate the controls
  111. const $controlContainer = UtilsFilterControl.getControlContainer(this)
  112. if ($controlContainer.find('select').length > 0 || $controlContainer.find('input:not([type="checkbox"]):not([type="radio"])').length > 0) {
  113. return
  114. }
  115. UtilsFilterControl.createControls(this, $controlContainer)
  116. })
  117. .on('post-header.bs.table', () => {
  118. UtilsFilterControl.setValues(this)
  119. })
  120. .on('post-body.bs.table', () => {
  121. if (this.options.height && !this.options.filterControlContainer) {
  122. UtilsFilterControl.fixHeaderCSS(this)
  123. }
  124. this.$tableLoading.css('top', this.$header.outerHeight() + 1)
  125. })
  126. .on('column-switch.bs.table', () => {
  127. UtilsFilterControl.setValues(this)
  128. })
  129. .on('load-success.bs.table', () => {
  130. this.enableControls(true)
  131. })
  132. .on('load-error.bs.table', () => {
  133. this.enableControls(true)
  134. })
  135. }
  136. super.init()
  137. }
  138. initHeader () {
  139. super.initHeader()
  140. if (!this.options.filterControl || this.options.height) {
  141. return
  142. }
  143. UtilsFilterControl.createControls(this, UtilsFilterControl.getControlContainer(this))
  144. }
  145. initBody () {
  146. super.initBody()
  147. UtilsFilterControl.syncControls(this)
  148. UtilsFilterControl.initFilterSelectControls(this)
  149. }
  150. initSearch () {
  151. const that = this
  152. const fp = $.isEmptyObject(that.filterColumnsPartial) ? null : that.filterColumnsPartial
  153. super.initSearch()
  154. if (this.options.sidePagination === 'server' || fp === null) {
  155. return
  156. }
  157. // Check partial column filter
  158. that.data = fp
  159. ? that.data.filter((item, i) => {
  160. const itemIsExpected = []
  161. const keys1 = Object.keys(item)
  162. const keys2 = Object.keys(fp)
  163. const keys = keys1.concat(keys2.filter(item => !keys1.includes(item)))
  164. keys.forEach(key => {
  165. const thisColumn = that.columns[that.fieldsColumnsIndex[key]]
  166. const fval = (fp[key] || '').toLowerCase()
  167. let value = Utils.getItemField(item, key, false)
  168. let tmpItemIsExpected
  169. if (fval === '') {
  170. tmpItemIsExpected = true
  171. } else {
  172. // Fix #142: search use formatted data
  173. if (thisColumn && thisColumn.searchFormatter) {
  174. value = $.fn.bootstrapTable.utils.calculateObjectValue(
  175. that.header,
  176. that.header.formatters[$.inArray(key, that.header.fields)],
  177. [value, item, i],
  178. value
  179. )
  180. }
  181. if ($.inArray(key, that.header.fields) !== -1) {
  182. if (value === undefined || value === null) {
  183. tmpItemIsExpected = false
  184. } else if (typeof value === 'object') {
  185. value.forEach((objectValue) => {
  186. if (tmpItemIsExpected) {
  187. return
  188. }
  189. tmpItemIsExpected = that.isValueExpected(fval, objectValue, thisColumn, key)
  190. })
  191. } else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
  192. tmpItemIsExpected = that.isValueExpected(fval, value, thisColumn, key)
  193. }
  194. }
  195. }
  196. itemIsExpected.push(tmpItemIsExpected)
  197. })
  198. return !itemIsExpected.includes(false)
  199. })
  200. : that.data
  201. that.unsortedData = [...that.data]
  202. }
  203. isValueExpected (searchValue, value, column, key) {
  204. let tmpItemIsExpected = false
  205. if (column.filterStrictSearch) {
  206. tmpItemIsExpected = value.toString().toLowerCase() === searchValue.toString().toLowerCase()
  207. } else if (column.filterStartsWithSearch) {
  208. tmpItemIsExpected = (`${value}`).toLowerCase().indexOf(searchValue) === 0
  209. } else {
  210. tmpItemIsExpected = (`${value}`).toLowerCase().includes(searchValue)
  211. }
  212. const largerSmallerEqualsRegex = /(?:(<=|=>|=<|>=|>|<)(?:\s+)?(\d+)?|(\d+)?(\s+)?(<=|=>|=<|>=|>|<))/gm
  213. const matches = largerSmallerEqualsRegex.exec(searchValue)
  214. if (matches) {
  215. const operator = matches[1] || `${matches[5]}l`
  216. const comparisonValue = matches[2] || matches[3]
  217. const int = parseInt(value, 10)
  218. const comparisonInt = parseInt(comparisonValue, 10)
  219. switch (operator) {
  220. case '>':
  221. case '<l':
  222. tmpItemIsExpected = int > comparisonInt
  223. break
  224. case '<':
  225. case '>l':
  226. tmpItemIsExpected = int < comparisonInt
  227. break
  228. case '<=':
  229. case '=<':
  230. case '>=l':
  231. case '=>l':
  232. tmpItemIsExpected = int <= comparisonInt
  233. break
  234. case '>=':
  235. case '=>':
  236. case '<=l':
  237. case '=<l':
  238. tmpItemIsExpected = int >= comparisonInt
  239. break
  240. default:
  241. break
  242. }
  243. }
  244. if (column.filterCustomSearch) {
  245. const customSearchResult = Utils.calculateObjectValue(this, column.filterCustomSearch, [searchValue, value, key, this.options.data], true)
  246. if (customSearchResult !== null) {
  247. tmpItemIsExpected = customSearchResult
  248. }
  249. }
  250. return tmpItemIsExpected
  251. }
  252. initColumnSearch (filterColumnsDefaults) {
  253. UtilsFilterControl.copyValues(this)
  254. if (filterColumnsDefaults) {
  255. this.filterColumnsPartial = filterColumnsDefaults
  256. this.updatePagination()
  257. for (const filter in filterColumnsDefaults) {
  258. this.trigger('column-search', filter, filterColumnsDefaults[filter])
  259. }
  260. }
  261. }
  262. onColumnSearch ({currentTarget, keyCode}) {
  263. if ($.inArray(keyCode, [37, 38, 39, 40]) > -1) {
  264. return
  265. }
  266. UtilsFilterControl.copyValues(this)
  267. const text = $.trim($(currentTarget).val())
  268. const $field = $(currentTarget).closest('[data-field]').data('field')
  269. this.trigger('column-search', $field, text)
  270. if ($.isEmptyObject(this.filterColumnsPartial)) {
  271. this.filterColumnsPartial = {}
  272. }
  273. if (text) {
  274. this.filterColumnsPartial[$field] = text
  275. } else {
  276. delete this.filterColumnsPartial[$field]
  277. }
  278. this.options.pageNumber = 1
  279. this.enableControls(false)
  280. this.onSearch({currentTarget}, false)
  281. }
  282. initToolbar () {
  283. this.showToolbar = this.showToolbar || this.options.showFilterControlSwitch
  284. this.showSearchClearButton = this.options.filterControl && this.options.showSearchClearButton
  285. super.initToolbar()
  286. if (this.options.showFilterControlSwitch) {
  287. const $btnGroup = this.$toolbar.find('>.columns')
  288. let $btnFilterControlSwitch = $btnGroup.find('.filter-control-switch')
  289. if (!$btnFilterControlSwitch.length) {
  290. $btnFilterControlSwitch = $(`
  291. <button class="filter-control-switch ${this.constants.buttonsClass}"
  292. type="button" title="${this.options.formatFilterControlSwitch()}">
  293. ${this.options.showButtonIcons ? Utils.sprintf(this.constants.html.icon, this.options.iconsPrefix, this.options.filterControlVisible ? this.options.icons.filterControlSwitchHide : this.options.icons.filterControlSwitchShow) : ''}
  294. ${this.options.showButtonText ? this.options.filterControlVisible ? this.options.formatFilterControlSwitchHide() : this.options.formatFilterControlSwitchShow() : ''}
  295. </button>
  296. `).appendTo($btnGroup)
  297. $btnFilterControlSwitch.on('click', $.proxy(this.toggleFilterControl, this))
  298. }
  299. }
  300. }
  301. resetSearch (text) {
  302. if (this.options.filterControl && this.options.showSearchClearButton) {
  303. this.clearFilterControl()
  304. }
  305. super.resetSearch(text)
  306. }
  307. clearFilterControl () {
  308. if (this.options.filterControl) {
  309. const that = this
  310. const cookies = UtilsFilterControl.collectBootstrapCookies()
  311. const header = UtilsFilterControl.getCurrentHeader(that)
  312. const table = header.closest('table')
  313. const controls = header.find(UtilsFilterControl.getCurrentSearchControls(that))
  314. const search = that.$toolbar.find('.search input')
  315. let hasValues = false
  316. let timeoutId = 0
  317. $.each(that.options.valuesFilterControl, (i, item) => {
  318. hasValues = hasValues ? true : item.value !== ''
  319. item.value = ''
  320. })
  321. $.each(that.options.filterControls, (i, item) => {
  322. item.text = ''
  323. })
  324. UtilsFilterControl.setValues(that)
  325. // clear cookies once the filters are clean
  326. clearTimeout(timeoutId)
  327. timeoutId = setTimeout(() => {
  328. if (cookies && cookies.length > 0) {
  329. $.each(cookies, (i, item) => {
  330. if (that.deleteCookie !== undefined) {
  331. that.deleteCookie(item)
  332. }
  333. })
  334. }
  335. }, that.options.searchTimeOut)
  336. // If there is not any value in the controls exit this method
  337. if (!hasValues) {
  338. return
  339. }
  340. // Clear each type of filter if it exists.
  341. // Requires the body to reload each time a type of filter is found because we never know
  342. // which ones are going to be present.
  343. if (controls.length > 0) {
  344. this.filterColumnsPartial = {}
  345. $(controls[0]).trigger(
  346. controls[0].tagName === 'INPUT' ? 'keyup' : 'change', {keyCode: 13}
  347. )
  348. } else {
  349. return
  350. }
  351. if (search.length > 0) {
  352. that.resetSearch()
  353. }
  354. // use the default sort order if it exists. do nothing if it does not
  355. if (that.options.sortName !== table.data('sortName') || that.options.sortOrder !== table.data('sortOrder')) {
  356. const sorter = header.find(Utils.sprintf('[data-field="%s"]', $(controls[0]).closest('table').data('sortName')))
  357. if (sorter.length > 0) {
  358. that.onSort({type: 'keypress', currentTarget: sorter})
  359. $(sorter).find('.sortable').trigger('click')
  360. }
  361. }
  362. }
  363. }
  364. triggerSearch () {
  365. const searchControls = UtilsFilterControl.getSearchControls(this)
  366. searchControls.each(function () {
  367. const el = $(this)
  368. if (el.is('select')) {
  369. el.change()
  370. } else {
  371. el.keyup()
  372. }
  373. })
  374. }
  375. enableControls (enable) {
  376. if (this.options.disableControlWhenSearch && this.options.sidePagination === 'server') {
  377. const searchControls = UtilsFilterControl.getSearchControls(this)
  378. if (!enable) {
  379. searchControls.prop('disabled', 'disabled')
  380. } else {
  381. searchControls.removeProp('disabled')
  382. }
  383. }
  384. }
  385. toggleFilterControl () {
  386. this.options.filterControlVisible = !this.options.filterControlVisible
  387. const $filterControls = UtilsFilterControl.getControlContainer(this).find('.filter-control, .no-filter-control')
  388. if (this.options.filterControlVisible) {
  389. $filterControls.show()
  390. } else {
  391. $filterControls.hide()
  392. this.clearFilterControl()
  393. }
  394. const icon = this.options.showButtonIcons ? this.options.filterControlVisible ? this.options.icons.filterControlSwitchHide : this.options.icons.filterControlSwitchShow : ''
  395. const text = this.options.showButtonText ? this.options.filterControlVisible ? this.options.formatFilterControlSwitchHide() : this.options.formatFilterControlSwitchShow() : ''
  396. this.$toolbar.find('>.columns').find('.filter-control-switch')
  397. .html(Utils.sprintf(this.constants.html.icon, this.options.iconsPrefix, icon) + ' ' + text)
  398. }
  399. }