JQuery Tablesorter文本提取未按预期工作

JQuery Tablesorter文本提取未按预期工作,jquery,tablesorter,Jquery,Tablesorter,请在下面找到我的HTML和javascript。我试图根据选择的单选按钮对表中的列进行排序 <table id="test"> <thead><tr><th>data</th></tr></thead> <tbody> <tr><td><span>1.00 %</span>&nbsp;<span>$5,000</span

请在下面找到我的HTML和javascript。我试图根据选择的单选按钮对表中的列进行排序

<table id="test">
 <thead><tr><th>data</th></tr></thead>
 <tbody>
  <tr><td><span>1.00 %</span>&nbsp;<span>$5,000</span></td></tr>
  <tr><td><span>0.50 %</span>&nbsp;<span>$300.00</span></td></tr>
  <tr><td><span>1.40 %</span>&nbsp;<span>$0</span></td></tr>
  <tr><td><span>1.20 %</span>&nbsp;<span>$500</span></td></tr>
 </tbody>
</table>


$("#test").tablesorter({
            textExtractionCustom: {
                0: function (node) {
                    if ($('#radio-choice-h-2a').is(':checked')) {
                        return $(node).find("span").first().text().replace('%', '').replace(' ', '');
                    }
                    else if ($('#radio-choice-h-2b').is(':checked')) {
                        return $(node).find("span").last().text().replace('$', '').replace(',', '');
                    }
                    else {
                        return $(node).find("span").first().text().replace('%', '').replace(' ', '');
                    }
                }
            },
            headers: { 0: { sorter: "digit" }
            }
        });

数据
1.00 % $5,000
0.50 % $300.00
1.40 % $0
1.20 % $500
$(“#测试”).tablesorter({
textExtractionCustom:{
0:函数(节点){
如果($('#radio-choice-h-2a')。是(':checked')){
返回$(node).find(“span”).first().text().replace('%,'').replace('');
}
else如果($('#radio-choice-h-2b')。是(':checked')){
返回$(node.find(“span”).last().text().replace(“$”,”).replace(“,”,”);
}
否则{
返回$(node).find(“span”).first().text().replace('%,'').replace('');
}
}
},
标题:{0:{sorter:“digit”}
}
});
如果选中单选按钮单选-h-2a,则按每个单元格中第一个跨距中的内容进行排序;如果选中单选按钮单选-h-2b,则按第二个跨距元素中的内容进行排序


这似乎没有达到预期效果。感谢您的帮助。谢谢。

只有在初始化或更新tablesorter时才会调用
textextextraction
函数。因此,如果您有一个单元格,其中的数据需要根据某些指示符进行不同的排序,则需要使用
textextextextraction
textSorter
功能的组合。你可以在的答案中找到两种不同的方法

如何做到这一点

排序依据:百分比
或
费用:
达布拉
1.00%$5000ABC
0.50%$300.00xyz
1.40%$0def
1.20%$500万
剧本

$(function () {

    var $sortby = $('input[name="group"]');

    $('#test').tablesorter({
        theme: 'blue',
        textExtraction: {
            0: function (node, table, cellIndex) {
                var $n = $(node).find('span');
                // remove percent, commas and dollor signs
                // add semi-colon between values
                return $.trim( $n.eq(0).text().replace(/%/g,'') ) + ';' +
                    $.trim( $n.eq(1).text().replace(/[$,]/g,'') );
            }
        },
        textSorter: function (a, b) {
            // only use special sort if there is a semi-colon
            if (/;/.test(a)) {
                var x = a.split(';'),
                    y = b.split(';'),
                    i = $sortby.index( $sortby.filter(':checked') );
                return $.tablesorter.sortNatural(x[i], y[i]);
            } else {
                return $.tablesorter.sortNatural(a, b);
            }
        }
    });

});

感谢您花时间回复。你提供的演示正是我需要的。我想知道您是否知道Jquery mobile和table sorter有任何不兼容之处。我试图在移动设备中与Jquery移动设备一起使用它,但它不像在您的演示中那样工作。无论选择哪个单选按钮,它都只会对百分比值进行排序。此外,我在tablesorter.min.js文件中找不到函数“textsorter”。当我将我的tablesorter.min.js替换为演示中附带的一个时,我的tablesorter完全停止工作。你有什么建议吗?事实上,没关系。我已经从URL下载了最新版本(2.17.7),它运行得很好。我下载的文件似乎不是最新的。谢谢你的帮助。嗨,莫蒂,我正试图在单击单选按钮时触发排序。我有这个代码来触发更新$('input:radio[name=“group”]”).change(函数(){$('test').trigger('update');});但是这段代码没有指定哪一列。您知道如何在单击单选按钮时触发X列的排序吗?谢谢。我在上一条评论中发布的代码似乎有效,但只有在至少单击一次表的标题时才有效。单击表格标题后,当我单击任何单选按钮时,表格已正确排序。有没有一种方法可以在不首先单击表格标题的情况下实现这一点?还有,当单击单选按钮时,是否有一种方法始终按升序排序?谢谢。您可以使用强制排序;此外,请确保在执行此操作时,将重新排序标志设置为
false
;下面是两个组合:
$(“表”).trigger(“更新”,“错误”).trigger(“排序”,“0,0]])