ソースを参照

fix #1629, #1678: add sortStable option

zhixin 9 年 前
コミット
44cb312df0
1 ファイル変更12 行追加0 行削除
  1. 12 0
      src/bootstrap-table.js

+ 12 - 0
src/bootstrap-table.js

@@ -292,6 +292,7 @@
         undefinedText: '-',
         undefinedText: '-',
         sortName: undefined,
         sortName: undefined,
         sortOrder: 'asc',
         sortOrder: 'asc',
+        sortStable: false,
         striped: false,
         striped: false,
         columns: [[]],
         columns: [[]],
         data: [],
         data: [],
@@ -934,6 +935,12 @@
         }
         }
 
 
         if (index !== -1) {
         if (index !== -1) {
+            if (this.options.sortStable) {
+                $.each(this.data, function (i, row) {
+                    if (!row.hasOwnProperty('_position')) row._position = i;
+                });
+            }
+
             this.data.sort(function (a, b) {
             this.data.sort(function (a, b) {
                 if (that.header.sortNames[index]) {
                 if (that.header.sortNames[index]) {
                     name = that.header.sortNames[index];
                     name = that.header.sortNames[index];
@@ -954,6 +961,11 @@
                     bb = '';
                     bb = '';
                 }
                 }
 
 
+                if (that.options.sortStable && aa === bb) {
+                    aa = a._position;
+                    bb = b._position;
+                }
+
                 // IF both values are numeric, do a numeric comparison
                 // IF both values are numeric, do a numeric comparison
                 if ($.isNumeric(aa) && $.isNumeric(bb)) {
                 if ($.isNumeric(aa) && $.isNumeric(bb)) {
                     // Convert numerical values form string to float.
                     // Convert numerical values form string to float.