ソースを参照

Merge pull request #518 from c-pohl/master

Natural Sort Order for bootstrap-table
文翼 11 年 前
コミット
cd424096e6
1 ファイル変更32 行追加0 行削除
  1. 32 0
      src/extensions/sorting/bootstrap-table_natural-sorting.js

+ 32 - 0
src/extensions/sorting/bootstrap-table_natural-sorting.js

@@ -0,0 +1,32 @@
+//JS function to allow natural sorting on bootstrap-table columns
+//just add data-sorter="alphanum" to any th
+//Thanks to Brian Huisman: http://www.greywyvern.com
+
+function alphanum(a, b) {
+  function chunkify(t) {
+    var tz = [], x = 0, y = -1, n = 0, i, j;
+
+    while (i = (j = t.charAt(x++)).charCodeAt(0)) {
+      var m = (i == 46 || (i >=48 && i <= 57));
+      if (m !== n) {
+        tz[++y] = "";
+        n = m;
+      }
+      tz[y] += j;
+    }
+    return tz;
+  }
+
+  var aa = chunkify(a);
+  var bb = chunkify(b);
+
+  for (x = 0; aa[x] && bb[x]; x++) {
+    if (aa[x] !== bb[x]) {
+      var c = Number(aa[x]), d = Number(bb[x]);
+      if (c == aa[x] && d == bb[x]) {
+        return c - d;
+      } else return (aa[x] > bb[x]) ? 1 : -1;
+    }
+  }
+  return aa.length - bb.length;
+}