Browse Source

Using sort function

djhvscf 7 years ago
parent
commit
b2140ed146

+ 4 - 43
src/bootstrap-table.js

@@ -8,6 +8,7 @@ import Constants from './constants/index.js'
 import Utils from './utils/index.js'
 import VirtualScroll from './virtual-scroll/index.js'
 import {isNumeric, isEmptyObject} from './types.js'
+import Sort from './sort.js'
 
 class BootstrapTable {
   constructor (el, options) {
@@ -401,8 +402,8 @@ class BootstrapTable {
           if (this.header.sortNames[index]) {
             name = this.header.sortNames[index]
           }
-          let aa = Utils.getItemField(a, name, this.options.escape)
-          let bb = Utils.getItemField(b, name, this.options.escape)
+          const aa = Utils.getItemField(a, name, this.options.escape)
+          const bb = Utils.getItemField(b, name, this.options.escape)
           const value = Utils.calculateObjectValue(this.header, this.header.sorters[index], [aa, bb, a, b])
 
           if (value !== undefined) {
@@ -412,47 +413,7 @@ class BootstrapTable {
             return order * value
           }
 
-          // Fix #161: undefined or null string sort bug.
-          if (aa === undefined || aa === null) {
-            aa = ''
-          }
-          if (bb === undefined || bb === null) {
-            bb = ''
-          }
-
-          if (this.options.sortStable && aa === bb) {
-            aa = a._position
-            bb = b._position
-          }
-
-          // IF both values are numeric, do a numeric comparison
-          if (isNumeric(aa) && isNumeric(bb)) {
-            // Convert numerical values form string to float.
-            aa = parseFloat(aa)
-            bb = parseFloat(bb)
-            if (aa < bb) {
-              return order * -1
-            }
-            if (aa > bb) {
-              return order
-            }
-            return 0
-          }
-
-          if (aa === bb) {
-            return 0
-          }
-
-          // If value is not a string, convert to string
-          if (typeof aa !== 'string') {
-            aa = aa.toString()
-          }
-
-          if (aa.localeCompare(bb) === -1) {
-            return order * -1
-          }
-
-          return order
+          return Sort(aa, bb, order, this.options.sortStable)
         })
       }
 

+ 2 - 1
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -536,7 +536,8 @@ $.extend($.fn.bootstrapTable.columnDefaults, {
   filterDatepickerOptions: undefined,
   filterStrictSearch: false,
   filterStartsWithSearch: false,
-  filterControlPlaceholder: ''
+  filterControlPlaceholder: '',
+  filterOrderBy: 'asc' // asc || desc
 })
 
 $.extend($.fn.bootstrapTable.Constructor.EVENTS, {

+ 8 - 3
src/sort.js

@@ -1,6 +1,6 @@
 import {isNumeric} from './types.js'
 
-export default function Sort (a, b, order) {
+export default function Sort (a, b, order, sortStable) {
   if (a === undefined || a === null) {
     a = ''
   }
@@ -8,6 +8,11 @@ export default function Sort (a, b, order) {
     b = ''
   }
 
+  if (sortStable && a === b) {
+    a = a._position
+    b = b._position
+  }
+
   // IF both values are numeric, do a numeric comparison
   if (isNumeric(a) && isNumeric(b)) {
     // Convert numerical values form string to float.
@@ -27,11 +32,11 @@ export default function Sort (a, b, order) {
   }
 
   // If value is not a string, convert to string
-  if (typeof aa !== 'string') {
+  if (typeof a !== 'string') {
     a = a.toString()
   }
 
-  if (a.localeCompare(b) < -1) {
+  if (a.localeCompare(b) === -1) {
     return order * -1
   }