Jquery 表排序器
我正在使用 Jquery tablesorter,我需要对包含带有连字符标记的数字的 Gridview 列进行排序。数据采用以下格式,无法按 ASC 顺序onLoad排序:17-143,17-162,12-144,17-45, 18-12,17-65,18-2 。有些值为 NULL。 sql 中的空白值。 上述数据默认排序为:12-144,17-45,17-65,17-143,17-162,18-2,18-12
请帮忙。这是我尝试使用的片段:
<script src="https://github.com/christianbach/tablesorter/blob/master/jquery.tablesorter.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#main_gvResource").tablesorter.addParser({
// set a unique id
id: 'lblIDSort',
is: function(s) {
return false;
},
format: function(s) {
return s.replace('$','').replace(/-/g,'');
},
type: 'numeric'
});
$(function() {
$("#main_gvResource").tablesorter({
widgets: ['zebra'],
headers: {
1: {//zero-based column index
sorter:'gdMobileID'
}
}
});
});
});
</script>
最佳答案
为什么有这么多空白?您是否害怕,您可能会一次看到太多代码? ;)
$(function() {
$.tablesorter.addParser({
_regex: /^\d+(?:-\d+)+$/,
_formatNumber: function(nr){
//return "0".repeat(5-nr.length) + nr;
while(nr.length < 5) nr = "0" + nr;
return nr;
},
// set a unique id
id: 'gdMobileID',
is: function(s) {
//to auto-recognize the column-type
return this._regex.test(s);
//to apply only on columns you defined
return false;
},
format: function(s){
return s.replace(/\d+/g, this._formatNumber);
},
type: 'text',
});
$("#main-gvResource").tablesorter({
widgets: ['zebra'],
//no need, if you let the parser auto-recognize the columns to apply
/*headers: {
1: {//zero-based column index
sorter:'gdMobileID'
}
}*/
});
});
关于javascript - 带连字符的 Jquery 表排序器数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37079329/