Browse Source

Merge pull request #4855 from jbarrineau06/add-custom-sorter-functionality-to-multiple-sort

Add custom sorter functionality to multiple sort
Dustin Utecht 5 years ago
parent
commit
a1ad7c8162
1 changed files with 20 additions and 11 deletions
  1. 20 11
      src/extensions/multiple-sort/bootstrap-table-multiple-sort.js

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

@@ -599,18 +599,29 @@ 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]
+      let fieldName = that.options.sortPriority[i].sortName
+      const fieldIndex = that.header.fields.indexOf(fieldName)
+      const sorterName = that.header.sorters[that.header.fields.indexOf(fieldName)]
 
-      if (aa === undefined || aa === null) {
-        aa = ''
+      if (that.header.sortNames[fieldIndex]) {
+        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]
+      const value1 = $.fn.bootstrapTable.utils.calculateObjectValue(that.header, sorterName, [aa, bb])
+      const 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 +635,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)