Browse Source

Added ability for the multiple-sort extension to use a custom sorter

jbarrineau06 5 years ago
parent
commit
9d8f707723
1 changed files with 27 additions and 11 deletions
  1. 27 11
      src/extensions/multiple-sort/bootstrap-table-multiple-sort.js

+ 27 - 11
src/extensions/multiple-sort/bootstrap-table-multiple-sort.js

@@ -599,18 +599,36 @@ BootstrapTable.prototype.onMultipleSort = function () {
     const arr2 = []
 
     for (let i = 0; i < that.options.sortPriority.length; i++) {
-      const order = that.options.sortPriority[i].sortOrder === 'desc' ? -1 : 1
-      let aa = a[that.options.sortPriority[i].sortName]
-      let bb = b[that.options.sortPriority[i].sortName]
+      // get fieldName from multi sort
+      let fieldName = that.options.sortPriority[i].sortName
+
+      // get index of fieldName from fields array
+      const fieldIndex = that.header.fields.indexOf(fieldName)
+
+      // use fieldIndex to get column's custom sorter
+      const sorterName = that.header.sorters[that.header.fields.indexOf(fieldName)]
 
-      if (aa === undefined || aa === null) {
-        aa = ''
+      // check if column has a custom sort name
+      if (that.header.sortNames[fieldIndex]) {
+        // set fieldName to custom sort column
+        fieldName = that.header.sortNames[fieldIndex]
       }
 
-      if (bb === undefined || bb === null) {
-        bb = ''
+      const order = that.options.sortPriority[i].sortOrder === 'desc' ? -1 : 1
+      let aa = a[fieldName]
+      let bb = b[fieldName]
+      value1 = $.fn.bootstrapTable.utils.calculateObjectValue(that.header, sorterName, [aa, bb])
+      value2 = $.fn.bootstrapTable.utils.calculateObjectValue(that.header, sorterName, [bb, aa])
+
+      if (value1 !== undefined && value2 !== undefined) {
+        arr1.push(order * value1)
+        arr2.push(order * value2)
+        continue
       }
 
+      if (aa === undefined || aa === null) aa = ''
+      if (bb === undefined || bb === null) bb = ''
+
       if ($.isNumeric(aa) && $.isNumeric(bb)) {
         aa = parseFloat(aa)
         bb = parseFloat(bb)
@@ -624,10 +642,8 @@ BootstrapTable.prototype.onMultipleSort = function () {
         }
       }
 
-      arr1.push(
-        order * cmp(aa, bb))
-      arr2.push(
-        order * cmp(bb, aa))
+      arr1.push(order * cmp(aa, bb))
+      arr2.push(order * cmp(bb, aa))
     }
 
     return cmp(arr1, arr2)