仅对表格的几行进行排序-jQuery / JavaScript

richa_pandey

我只想排序子tr的数据,而不想移动父tr。只有孩子tr会移动到下一个父母。我有这样一张桌子:

 <table>
    <tr>
        <th id="column1">Column 1</th>
        <th id="column2">Column 2</th>
        <th>Column 3</th>
    </tr>
    <tr class="parent">
        <td>text</td>
        <td>text</td>
        <td>text</td>
    </tr>
    <tr class="child">
        <td>96</td>
        <td>102</td>
        <td>121</td>
    </tr>
    <tr class="child">
        <td>455</td>
        <td>422</td>
        <td>410</td>
    </tr>
    <tr class="child">
        <td>212</td>
        <td>430</td>
        <td>203</td>
    </tr>
    <tr class="parent">
        <td>text</td>
        <td>text</td>
        <td>text</td>
    </tr>
    <tr class="child">
        <td>363</td>
        <td>581</td>
        <td>231</td>
    </tr>
    <tr class="child">
        <td>632</td>
        <td>115</td>
        <td>212</td>
    </tr>
</table>

JavaScript代码:

$('#column1, #column2')
    .each(function(){
        var th = $(this),
        thIndex = th.index(),
        inverse = false;

        th.click(function() {
            // sorting classes don't work here b/c this function gets called repeatedly - moved to afterRequest: function

            table.find('tr.parent 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;
                //  this.parentNode
            });

            inverse = !inverse;
        });

    });

小提琴:演示

安德烈亚斯(Andreas)

将每个“部分”单独包装 <tbody />

<tbody>
    <tr class="parent"><!-- ... --></tr>
    <tr class="child"><!-- ... --></tr>
    <tr class="child"><!-- ... --></tr>
    <tr class="child"><!-- ... --></tr>
</tbody>
<tbody>
    <tr class="parent"><!-- ... --></tr>
    <tr class="child"><!-- ... --></tr>
    <tr class="child"><!-- ... --></tr>
</tbody>
<!-- ... -->

而且做每一个的分类<tbody />

$("tbody").each(function() {
    $(this).find('tr:not(.parent) td') // ignore the "parent" row
           .filter(function () {
               return $(this).index() === thIndex;
           }).sortElements(function (a, b) {
               return $(a).text() > $(b).text() ? inverse ? -1 : 1 : inverse ? 1 : -1;
           }, function () {
               return this.parentNode;
           });
});

例子

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Javascript对表格行进行排序

使用 Javascript 或 Jquery 根据高/低值对表格行进行着色

使用 jQuery 如何对表格进行排序

使用Javascript对表格进行正确排序?

jQuery按1 td的值对表行进行排序

如何使用jQuery按两列对表格进行排序?

在Javascript中对表格的列进行排序无法正常工作

使用jquery或Javascript进行自定义表格排序

如何使用javascript jquery按字母顺序对表中的默认td进行排序

如何使用jQuery JavaScript对表中的文件夹和文件进行排序

jquery根据自定义列值对表行进行排序

按“团队名称”在jquery中的特定列对表行进行排序

使用纯Javascript根据列类名称对表行进行排序

对表格中的行进行排序

如何仅对 .txt 文件的最后几行进行排序

jQuery根据'td'内'sub-div'中的内容对表格列进行排序

如何通过javascript对表格进行排序之前强制浏览器显示模式?

单击 Javascript 中的标题,按升序和降序对表格进行排序

如何在纯JavaScript的帮助下对表格进行排序并重新生成

jQuery用数字对表数据进行排序

如何在 JQuery 中对表行进行连续编号

使用JavaScript(jQuery)对<table>进行排序

用JavaScript或jQuery对HTML内容进行排序

jQuery / JavaScript 排序 div

按字母顺序对表格行进行排序

如何使用jQuery对行进行排序

如何使用 javascript/jQuery 对表列求和?

在jQuery或JavaScript中排序项目

使用拖放和jQuery对表列进行重新排序