【问题标题】:jQuery Tablesorter Uncaught TypeError: Cannot set property 'count' of undefinedjQuery Tablesorter Uncaught TypeError:无法设置未定义的属性“计数”
【发布时间】:2011-10-11 09:25:40
【问题描述】:

我正在使用 jQuery TableSorter 插件并使用此代码收到以下错误 Uncaught TypeError: Cannot set property 'count' of undefined

$(document).ready(function () {
    var copyThead = $(".uiGridContent thead").html();
    copyThead = '<table><thead>' + copyThead + '</thead></table>';
    $(".uiGridHeader").html(copyThead);
    $(".uiGridContent table").tablesorter();
    $(".uiGridContent table thead").hide();
    $(".uiGridHeader th").click(function () {
        // Get updated HTML
        var FindcopyThead = $(".uiGridContent thead").html();
        var NewcopyThead = '<table><thead>' + FindcopyThead + '</thead></table>';
        $(".uiGridHeader").html(NewcopyThead);

        var index = $(".uiGridHeader th").index(this);
        var sorting = [[index, 0]];
        $(".uiGridContent table").trigger("sorton", [sorting]);
        return false;
    });
});

HTML 是:

<div class="uiGrid">
    <div class="uiGridHeader">
        <!-- NEW HEADER GOES HERE -->
    </div>
    <div class="uiGridContent">
        <table class="list sortable">
            <thead>
                <tr>
                    <th scope="col"><input type="checkbox" id="checkall" /></th>
                    <th scope="col">Name</th>
                    <th scope="col">Post Code</th>
                    <th scope="col">SIC Code</th>
                    <th scope="col">&#8470; of Employees</th>
                    <th scope="col"></th>
                    <th scope="col"></th>
                </tr>
            </thead>
            <tbody>
<!-- ROWS -->

我复制表头的原因是我需要拆分表头才能使我的应用程序结构正常工作。 这部分很好但是因为类等被添加到原始标题中,我需要再次使用新标题更新 HTML 这很好。但我得到错误?任何想法为什么或如何解决它?谢谢

【问题讨论】:

    标签: jquery tablesorter


    【解决方案1】:

    我遇到了同样的错误。我花了一分钟才弄清楚你必须传递的值是一个三深数组:

    $('table').trigger('sorton', [[[1,1]]]);
    

    与初始排序选项相反:

    $('table').tablesorter({
        sortList: [[1,1]]
    });
    

    向表中添加新数据时,请务必:

    $('table').trigger('update').trigger('appendCache');
    

    但是,在动态添加新列后,我无法让 tablesorter 正常运行。相反,在这些情况下,我必须让 jQuery 取消绑定所有内容,以便重新初始化:

    $('table').remove().appendTo('body').tablesorter({ ...
    

    【讨论】:

    • 由于某种原因,当我链接“更新”和“追加缓存”触发器时,它无法正常工作。但是当我分别给他们打电话时,他们奏效了。 $('table').trigger('update'); $('table').trigger('appendCache');
    【解决方案2】:

    存在三个问题。索引返回 -1,每次设置 HTML 都会删除您绑定到头部的事件,并且因为您正在触发 sorton 事件,所以您需要自己跟踪方向。

    这是我的代码,希望对您有所帮助。

    var copyThead = $(".uiGridContent thead").html();
    copyThead = '<table><thead>' + copyThead + '</thead></table>';
    $(".uiGridHeader").html(copyThead);
    $(".uiGridContent table").tablesorter();
    $(".uiGridContent table thead").hide();
    
    function bindClick() {
         $(".uiGridHeader th").click(theadClick);
    }
    
    var direction = 0;
    function theadClick() {
        if(direction) {
            direction = 0;
        } else {
            direction = 1;
        }
    
        var index = $(this).index();
        var sorting = [[index, direction ]];
        $(".uiGridContent table").trigger("sorton", [sorting]);
    
        var FindcopyThead = $(".uiGridContent thead").html();
        var NewcopyThead = '<table><thead>' + FindcopyThead + '</thead></table>';
        $(".uiGridHeader").html(NewcopyThead);
        bindClick();
    }
    
    bindClick();
    

    【讨论】:

    • 排序工作,但只有一次点击...之后,进一步的点击不会在任何列上工作?有任何想法吗?谢谢
    • 它似乎在jsFiddle 上运行良好。也许做一些记录并确保事件被触发。
    • 它确实有效,但标题的类没有改变?在复制的 HTML 和原始 HTML 上。所以这与使用.click()有关
    • 我认为是因为您仍在重置表格的html?通过这样做,您将删除点击事件和 .data() 值。我已经用新副本更新了 jsFiddle,我将编辑我的帖子。
    • 仍然没有为我改变课程:/它对你有用吗?
    猜你喜欢
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多