简体   繁体   English

jQuery-对HTML表格进行排序

[英]jQuery - sort HTML table

I've seen many many posts regarding sorting, but none that answers what would seem to be a typical scenario. 我见过很多有关排序的文章,但没有一篇能回答似乎是典型情况的文章。

I am trying to sort and HTML table using jQuery where the links I click to sort the columns don't exist within that table that is being sorted. 我正在尝试使用jQuery排序和HTML表格,在该表格中,我单击以对列进行排序的链接在正在排序的表格中不存在。 I "fake" a table header to accommodate a scrolling table requirement. 我“伪造”一个表头以适应滚动表的要求。

What I do now is - 我现在要做的是-

  • Load an array with the values within the column that need to be selected along with the row id's 加载一个数组,其中包含列中需要选择的值以及行ID
  • Sort that array based on those value 根据这些值对数组进行排序
  • Insert the rows into the HTML table based on index from that sorted array. 根据该排序数组中的索引将行插入HTML表中。

As expected, this takes close to 10 seconds to run locally. 如预期的那样,在本地运行大约需要10秒钟。

Code sample - 代码示例-

var sortArr = new Array();

$('tr td.' + name).each(function () {
    ResidentID = $(this).parent().attr("ResidentID");
    BID = $(this).parent().attr("BRecordID");

    if ($(this).find('select').length > 0) { //ddls
        columnText = $(this).find('select option:selected').text();
        sortArr.push({ residentId: ResidentID, bID: BID, text: columnText });
    }
    else if ($(this).find('input').length > 0) {//Textbox or checkboxes
        columnText = $(this).find('input').val();
        sortArr.push({ residentId: ResidentID, bID: BID, text: columnText });
    }
});

if (sortDirection == 'ascending') {
    colHeader.attr('sortorder', 'descending')
    sortArr = sortArr.sort(function (v, t) {
        return v.text.localeCompare(t.text);
    });
}
else {
    colHeader.attr('sortorder', 'ascending')
    sortArr = sortArr.sort(function (v, t) {
        return t.text.localeCompare(v.text);
    });
}

for (var i = 0; i < sortArr.length; i++) {
    $("[id$=fixedDataTable] > tbody > tr").eq(i).after($("[id$=trFixedDataRow_" + sortArr[i].residentId + "]"));

    $("[id$=residentDetailDataTable] > tbody > tr").eq(i).after($("[id$=trResScrollDataRow_" + sortArr[i].residentId + "]"));

    $("[id$=bDetailDataTable] > tbody > tr").eq(i).after($("[id$=trBScrollDataRow_" + sortArr[i].bmrID + "]"));
}

Is there a faster way to accomplish this? 有没有更快的方法可以做到这一点?

Your approach of finding column to be sorted, sort it, and repopulate table is correct. 您找到要排序的列,对其进行排序并重新填充表的方法是正确的。 But how you are doing that looks to be inefficient. 但是您的操作方式似乎效率低下。 Few things you can do to speed up 您可以做一些加快工作的事情

  1. Avoid reconstructing jquery object. 避免重建jquery对象。 For example you have $(this) in numerous places. 例如,您在许多地方都有$(this) Cache it in a local variable. 将其缓存在本地变量中。

  2. Instead of directly manipulating the dom. 而不是直接操纵dom。 Consider having the data in a 2D array, which directly maps out to the dom table. 考虑将数据存储在2D数组中,该数组直接映射到dom表。 I think this will be the biggest improvement. 我认为这将是最大的改进。 You can sort the 2D array based on however you want, then just repopulate the dom table. 您可以根据需要对2D数组进行排序,然后只需重新填充dom表即可。

  3. Considering giving each table row a unique-id based on its cell/row. 考虑为每个表行基于其单元格/行分配一个唯一ID。 That way the 2D array can map out more easily 这样一来,二维数组就可以更轻松地绘制出来

    Going back to your code, for this part of the snippet 对于代码段的这一部分,请回到您的代码

     for (var i = 0; i < sortArr.length; i++) { $("[id$=fixedDataTable] > tbody > tr").eq(i).after($("[id$=trFixedDataRow_" + sortArr[i].residentId + "]")); $("[id$=residentDetailDataTable] > tbody > tr").eq(i).after($(" [id$=trResScrollDataRow_" + sortArr[i].residentId + "]")); $("[id$=bDetailDataTable] > tbody > tr").eq(i).after($("[id$=trBScrollDataRow_" + sortArr[i].bmrID + "]")); } 

can you just save the result of $("[id$=...tr") into a variable before the for loop so that way jquery doesn't have to look for it in every iteration? 您可以将$("[id$=...tr")的结果保存到for循环之前的变量中,以便jquery不必在每次迭代中都查找它吗? And you can probably just index the cached data yourself? 您可能只是自己索引缓存的数据?

坚持这种排序方式似乎是唯一的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM