jQuery Tablesorter未捕获TypeError:无法设置未定义的属性'count'

Cam*_*ron 7 jquery tablesorter

我正在使用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;
    });
});
Run Code Online (Sandbox Code Playgroud)

而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 -->
Run Code Online (Sandbox Code Playgroud)

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

Joh*_*Kim 2

存在三个问题。索引返回 -1,每次设置 HTML 都会删除绑定到 head 的事件,并且因为您正在触发 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();
Run Code Online (Sandbox Code Playgroud)