bootstrap-table不分頁時對數值類型數據的排序

html中的代碼

<table id="table"></table>

 

sortData.json的數據以下

[
    {"name":"三洋","num":"10","pecent":"29%"},
    {"name":"松下","num":"9","pecent":"28%"},
    {"name":"美的","num":"7","pecent":"20%"},
    {"name":"小天鵝","num":"3","pecent":"8%"},
    {"name":"飛利浦","num":"12","pecent":"32%"}
]

對應的js代碼

 1 $("#table").bootstrapTable({
 2     dataType: "json",
 3     method: 'get',
 4     contentType: "application/x-www-form-urlencoded",
 5     cache: false,
 6     url:"data/sortData.json",
 7     columns:[
 8         {
 9             field: 'name',
10             title: '品牌名稱',
11             valign:"middle",
12             align:"center",
13             sortable:true
14         },
15         {
16             field: 'num',
17             title: '門店數量',
18             valign:"middle",
19             align:"center",
20             sortable:true,
21             sorter:numSort
22         },{
23             field: 'pecent',
24             title: '市場份額',
25             valign:"middle",
26             align:"center",
27             sortable:true,
28             sorter:percentSort
29         }
30     ]
31 })
32 function numSort(a, b) {
33     return b-a;
34 }
35 function percentSort(a, b) {
36     var value_a = a.substr(0, a.length-1)
37     var value_b = b.substr(0, b.length-1)
38     return value_b-value_a;
39 }

 


對於數值類型的數據使用numSort排序
對於有百分號的數據使用percentSort
bootstrap-table默認按字符串排序

默認顯示的結果

 

數值類型排序的結果

正序排序html

倒序排序json

百分比類型數據排序的結果

正序排序bootstrap

 

倒序排序app

相關文章
相關標籤/搜索