0

我正在使用此代码

.wrapInner('<span title="sort this column"/>')
    .each(function () {

    var th = $(this),
        thIndex = th.index(),
        inverse = false;

    th.click(function () {

        table.find('td').filter(function () {

            return $(this).index() === thIndex;

        }).sortElements(function (a, b) {

            return $.text([a]) > $.text([b]) ? inverse ? -1 : 1 : inverse ? 1 : -1;

        }, function () {

            // parentNode is the element we want to move
            return this.parentNode;

        });

        inverse = !inverse;

    });

});

尝试订购一张桌子但是我不断收到以下错误

TypeError: table.find("td").filter(function () {return $(this).index() === thIndex;}).sortElements 不是函数

有没有人有任何线索?

4

1 回答 1

2

如前所述,您需要jquery.sortElements.js 插件

假设以下标记:

<ul>
    <li>Banana</li>
    <li>Carrot</li>
    <li>Apple</li>
</ul>

您可以像这样按字母顺序对项目进行排序:

$('li').sortElements(function(a, b){
    return $(a).text() > $(b).text() ? 1 : -1;
});

这将导致:

<ul>
    <li>Apple</li>
    <li>Banana</li>
    <li>Carrot</li>
</ul>

它还允许您指定要排序的元素。当前集合的元素将是那些在每次调用比较器时称为 a 和 b 的元素,但您可能不希望这些元素成为移动的元素。例如,您可能希望它成为父母。例如,在对表格列进行排序时,您将按<td>元素排序,但您实际想要在 DOM 中移动的元素是<tr>(每个<td>的父级):

$('td').sortElements(myComparator, function(){
    // Return a reference to the desired element:
    return this.parentNode;
});

在此处查看更多信息:http: //james.padolsey.com/javascript/sorting-elements-with-jquery/

于 2013-12-13T15:46:29.060 回答