【问题标题】:jQuery TableSorter - textExtraction based on external variablejQuery TableSorter - 基于外部变量的文本提取
【发布时间】:2014-02-26 00:50:45
【问题描述】:

我有一个 TableSorted 表,每个 TD 元素中有五个 SPAN 元素。四个总是隐藏的,但是在点击表格外的一个 div 时,某些 span 会被隐藏,具体取决于点击的是哪个 div。

我的表格排序很好,但我需要的是 textExtraction 抓取不同的 SPAN,具体取决于已选择的 div 的值。

我尝试了以下方法无济于事:

textExtraction:function(node){
        var filter=$("div.career a.sel").text();
        if(filter=="a"){var theindex=0;}
        if(filter=="b"){var theindex=1;}
        if(filter=="c"){var theindex=2;}
        if(filter=="d"){var theindex=3;}
        if(filter=="e"){var theindex=4;}
        return $(node).find("span").eq(theindex).text();
    }

实现这一目标的最佳方法是什么?

【问题讨论】:

    标签: javascript jquery tablesorter


    【解决方案1】:

    textExtraction 函数仅在 tablesorter 初始化或更新时调用。尝试在选择更改后触发更新。

    var indexes = 'abcde'.split(''),
        // Is this a select box?
        $sel = $("div.career a.sel").on('change', function(){
            $('table').trigger('update');
        });
    
    $('table').tablesorter({
        textExtraction: function(node){
            // if this is a select, get val(), not text()
            var filter = $sel.val(),
                theindex = $.inArray( filter, indexes );
            return $(node).find("span").eq(theindex).text();
        }
    });
    

    更新:获取链接,试试这个:

    var indexes = 'abcde'.split(''),
        $careers = $("div.career a").on('click',function(){
            var searcher = $(this).attr("rel");
            $("div.career a").removeClass("sel");
            $(this).addClass("sel");
            $("td.stat span").hide();
            $("td.stat span.career_" + searcher).show();
        });
    
    $('table').tablesorter({
        textExtraction: function(node){
            // find selected
            var filter = $careers.filter('.sel').text(),
            theindex = $.inArray( filter, indexes );
            return $(node).find("span").eq(theindex).text();
        }
    });
    

    注意:不要在 jQuery 1.9+ 版本中使用 live(),它已被删除。

    【讨论】:

    • 嗨,经过一番玩弄后,我得出结论,这是因为该函数没有检测到页面的任何更改,因为它只是在您所说的开始时调用。 a.sel 只是一个链接,当它被“选中”(实际上只是单击)时,它添加了一个类 (.sel)。鉴于它不是选择框,您能否向我展示如何最好地做到这一点?这是控制 div.career 部分的代码:
    • $("div.career a").live('click',function(){ var searcher=$(this).attr("rel"); $("div.career a").removeClass("sel"); $(this).addClass("sel"); $("td.stat span").hide(); $("td.stat span.career_"+searcher).css("display","block"); });
    • 看来我已经解决了。感谢@Mottie,因为这是您触发更新的线索,这很有帮助。我添加了 $("table.formguide").trigger("update");在 a.sel 链接的 onclick 函数中!
    猜你喜欢
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多