浏览代码

adds a numericOnly sort option

Duane May 10 年之前
父节点
当前提交
d99f514a54

+ 14 - 2
src/extensions/natural-sorting/README.md

@@ -8,6 +8,18 @@ Use Plugin: [bootstrap-table-natural-sorting](https://github.com/wenzhixin/boots
 <script src="extensions/natural-sorting/bootstrap-table-natural-sorting.js"></script>
 ```
 
-### Options
+add a data-sorter atribute to any th. 
+*e.g.* ```<th data-sortable="true" data-sorter="alphanum">Price</th>```
+
+## Options
+
+* *alphanum* - sort alpha or numeric content naturally.
+    This can be used in columns that contain text or numeric content. 
+    Numbers will be sorted as expected 
+
+* *numericOnly* - extract numeric content and sort numerically.  
+  This can be used in columns that contain formated numeric content. 
+  e.g. $ and , will be removed, then Numbers will be sorted as expected
+  an alpha sort crrently sorts these as ASCII so you get $1, $100, $2, $20
+  instead of $1, $2, $20, $100.
 
-* Just add data-sorter="alphanum" to any th

+ 15 - 2
src/extensions/natural-sorting/bootstrap-table-natural-sorting.js

@@ -2,10 +2,11 @@
  * @author: Brian Huisman
  * @webSite: http://www.greywyvern.com
  * @version: v1.0.0
- * JS function to allow natural sorting on bootstrap-table columns
- * just add data-sorter="alphanum" to any th
+ * JS functions to allow natural sorting on bootstrap-table columns
+ * add data-sorter="alphanum" or data-sorter="numericOnly" to any th
  *
  * @update Dennis Hernández <http://djhvscf.github.io/Blog>
+ * @update Duane May
  */
 
 function alphanum(a, b) {
@@ -44,4 +45,16 @@ function alphanum(a, b) {
     }
   }
   return aa.length - bb.length;
+}
+
+function numericOnly(a, b) {
+    function stripNonNumber(s) {
+        s = s.replace(new RegExp(/[^0-9]/g), "");
+        return parseInt(s.toLowerCase());
+    }
+
+    aa = stripNonNumber(a);
+    bb = stripNonNumber(b);
+
+    return aa - bb;
 }