Browse Source

Moving the functions

djhvscf 5 years ago
parent
commit
f06f7919bd

+ 10 - 28
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -13,7 +13,7 @@ $.extend($.fn.bootstrapTable.defaults, {
   onColumnSearch (field, text) {
     return false
   },
-  onInitFilterSelectControls () {
+  onCreatedControls () {
     return false
   },
   alignmentSelectControlOptions: undefined,
@@ -52,7 +52,7 @@ $.extend($.fn.bootstrapTable.defaults, {
 })
 
 $.extend($.fn.bootstrapTable.columnDefaults, {
-  filterControl: undefined,
+  filterControl: undefined, // input, select, datepicker
   filterDataCollector: undefined,
   filterData: undefined,
   filterDatepickerOptions: undefined,
@@ -64,7 +64,8 @@ $.extend($.fn.bootstrapTable.columnDefaults, {
 })
 
 $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
-  'column-search.bs.table': 'onColumnSearch'
+  'column-search.bs.table': 'onColumnSearch',
+  'created-controls.bs.table': 'onCreatedControls'
 })
 
 $.extend($.fn.bootstrapTable.defaults.icons, {
@@ -167,9 +168,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
   initSearch () {
     const that = this
-    const fp = $.isEmptyObject(that.filterColumnsPartial)
-      ? null
-      : that.filterColumnsPartial
+    const fp = $.isEmptyObject(that.filterColumnsPartial) ? null : that.filterColumnsPartial
 
     super.initSearch()
 
@@ -289,9 +288,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
     UtilsFilterControl.copyValues(this)
     const text = $.trim($(currentTarget).val())
-    const $field = $(currentTarget)
-      .closest('[data-field]')
-      .data('field')
+    const $field = $(currentTarget).closest('[data-field]').data('field')
 
     this.trigger('column-search', $field, text)
 
@@ -395,23 +392,11 @@ $.BootstrapTable = class extends $.BootstrapTable {
       }
 
       // use the default sort order if it exists. do nothing if it does not
-      if (
-        that.options.sortName !== table.data('sortName') ||
-        that.options.sortOrder !== table.data('sortOrder')
-      ) {
-        const sorter = header.find(
-          Utils.sprintf(
-            '[data-field="%s"]',
-            $(controls[0])
-              .closest('table')
-              .data('sortName')
-          )
-        )
+      if (that.options.sortName !== table.data('sortName') || that.options.sortOrder !== table.data('sortOrder')) {
+        const sorter = header.find(Utils.sprintf('[data-field="%s"]', $(controls[0]).closest('table').data('sortName')))
         if (sorter.length > 0) {
           that.onSort({type: 'keypress', currentTarget: sorter})
-          $(sorter)
-            .find('.sortable')
-            .trigger('click')
+          $(sorter).find('.sortable').trigger('click')
         }
       }
     }
@@ -431,10 +416,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
   }
 
   enableControls (enable) {
-    if (
-      this.options.disableControlWhenSearch &&
-      this.options.sidePagination === 'server'
-    ) {
+    if (this.options.disableControlWhenSearch && this.options.sidePagination === 'server') {
       const searchControls = UtilsFilterControl.getSearchControls(this)
 
       if (!enable) {

+ 32 - 36
src/extensions/filter-control/utils.js

@@ -1,6 +1,31 @@
 const Utils = $.fn.bootstrapTable.utils
 const searchControls = 'select, input:not([type="checkbox"]):not([type="radio"])'
 
+function getElementClass ($element) {
+  return $element.attr('class').replace('form-control', '').replace('focus-temp', '').replace('search-input', '').trim()
+}
+
+function getCursorPosition (el) {
+  if (Utils.isIEBrowser()) {
+    if ($(el).is('input[type=text]')) {
+      let pos = 0
+      if ('selectionStart' in el) {
+        pos = el.selectionStart
+      } else if ('selection' in document) {
+        el.focus()
+        const Sel = document.selection.createRange()
+        const SelLength = document.selection.createRange().text.length
+        Sel.moveStart('character', -el.value.length)
+        pos = Sel.text.length - SelLength
+      }
+      return pos
+    }
+    return -1
+
+  }
+  return -1
+}
+
 export function getOptionsFromSelectControl (selectControl) {
   return selectControl.get(selectControl.length - 1).options
 }
@@ -180,9 +205,7 @@ export function escapeID (id) {
 }
 
 export function isColumnSearchableViaSelect ({ filterControl, searchable }) {
-  return filterControl &&
-        filterControl.toLowerCase() === 'select' &&
-        searchable
+  return filterControl && (filterControl.toLowerCase() === 'select' || filterControl.toLowerCase() === 'multipleselect') && searchable
 }
 
 export function isFilterDataNotGiven ({ filterData }) {
@@ -204,12 +227,8 @@ export function initFilterSelectControls (that) {
 
   $.each(that.header.fields, (j, field) => {
     const column = that.columns[that.fieldsColumnsIndex[field]]
-    const selectControl = getControlContainer(that).find(`.bootstrap-table-filter-control-${escapeID(column.field)}`)
-    if (
-      isColumnSearchableViaSelect(column) &&
-            isFilterDataNotGiven(column) &&
-            hasSelectControlElement(selectControl)
-    ) {
+    const selectControl = getControlContainer(that).find(`select.bootstrap-table-filter-control-${escapeID(column.field)}`)
+    if (isColumnSearchableViaSelect(column) && isFilterDataNotGiven(column) && hasSelectControlElement(selectControl)) {
       if (selectControl.get(selectControl.length - 1).options.length === 0) {
         // Added the default option
         addOptionToSelectControl(selectControl, '', column.filterControlPlaceholder, column.filterDefault)
@@ -376,11 +395,11 @@ export function createControls (that, header) {
       }, that.options.searchTimeOut)
     })
 
-    header.off('change', 'select').on('change', 'select', ({ currentTarget, keyCode }) => {
+    header.off('change', 'select:not(".ms-offscreen")').on('change', 'select:not(".ms-offscreen")', ({ currentTarget, keyCode }) => {
       syncControls(that)
       const $select = $(currentTarget)
       const value = $select.val()
-      if (value.trim()) {
+      if (value && value.length > 0 && value.trim()) {
         $select.find('option[selected]').removeAttr('selected')
         $select.find('option[value="' + value + '"]').attr('selected', true)
       } else {
@@ -448,6 +467,8 @@ export function createControls (that, header) {
   } else {
     header.find('.filter-control, .no-filter-control').hide()
   }
+
+  that.trigger('created-controls')
 }
 
 export function getDirectionOfSelectOptions (_alignment) {
@@ -490,31 +511,6 @@ export function syncControls (that) {
   }
 }
 
-function getElementClass ($element) {
-  return $element.attr('class').replace('form-control', '').replace('focus-temp', '').replace('search-input', '').trim()
-}
-
-function getCursorPosition (el) {
-  if (Utils.isIEBrowser()) {
-    if ($(el).is('input[type=text]')) {
-      let pos = 0
-      if ('selectionStart' in el) {
-        pos = el.selectionStart
-      } else if ('selection' in document) {
-        el.focus()
-        const Sel = document.selection.createRange()
-        const SelLength = document.selection.createRange().text.length
-        Sel.moveStart('character', -el.value.length)
-        pos = Sel.text.length - SelLength
-      }
-      return pos
-    }
-    return -1
-
-  }
-  return -1
-}
-
 const filterDataMethods = {
   func (filterDataSource, selectControl, filterOrderBy, selected) {
     const variableValues = window[filterDataSource].apply()