按价格列排序表 [英] sort table by price column

查看:81
本文介绍了按价格列排序表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是帐单清单:

Service   Price
---------------
S1        13 CHF
S2        Free
S3        Free
S4        40 CHF

我想按价格使用jQuery或纯JavaScript进行排序. (不是服务器端)

I want to sort it by price using jQuery or pure JavaScript. (not server-side)

我尝试了 jQuery Tablesorter ,但是失败了.因为有些价格不是数字. (Free)

I tried jQuery Tablesorter, but it failed. Because some prices aren't number. (Free)

我该怎么办? Tablesorter可以支持吗?或者我应该使用其他插件...

What can I do? Can Tablesorter support it? Or I should use other plugins...

推荐答案

您可以编写自己的解析器.请参阅以下文档: http://tablesorter.com/docs/example-parsers.html

You can write your own parser. See the documentation at: http://tablesorter.com/docs/example-parsers.html

工作示例:

$.tablesorter.addParser({ 
    id: 'price', 
    is: function(s) { 
        return false; 
    }, 
    format: function(s) { 
        return s.replace(/Free/,0).replace(/ CHF/,""); 
    }, 
    type: 'numeric' 
}); 

$(function() { 
    $("table").tablesorter({ 
        headers: { 
            1: { 
                sorter:'price' 
            } 
        } 
    }); 
});

http://jsfiddle.net/8BCHR/

这篇关于按价格列排序表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆