ソースを参照

Merge pull request #1615 from duanemay/master

adds a numericOnly sort option
文翼 10 年 前
コミット
a3c1d44dc0

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

@@ -8,6 +8,20 @@ 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 and not in ASCII order 
+
+### 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

+ 12 - 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,13 @@ 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, 10);
+    }
+
+    return stripNonNumber(a) - stripNonNumber(b);
 }