sorting problem: improvement over extracting td value

sorting problem: improvement over extracting td value

SimondiSimondi Posts: 6Questions: 0Answers: 0
edited April 2014 in General
I praise your jquery datasort library a lot. I use the features in my applications. However for some tables, because my columns values are assigned differently, the sorting / search did not work on those columns. The tables are initialized roughly first. Then I have to locate the td of the row first and then assign the text value of the other columns of the same row (a feature I need in websocket communication):

var thisTD = $("#tb_consist_Status td.trainId").filter(function () {
return $(this).text() == id;
});
thisTD.prev().text(siteName);
thisTD.next().text(siteFeature);

Somehow your library does not like that. So this column of siteName (or siteFeature) is not sortable and not searchable. I think the problem can be easily solved with a modification of your library. It shall work if you extract the td text value like this (here index is the column number):

function getCellValue(row, index) {
return $(row).children('td').eq(index).text() }

Could you make changes on your library? I tested with the following sorting utility, it works great:

$(document).ready(function () {
$("#locationSort, #trainSort, #faultsSort").click(function () {
var table = $(this).parents('table').eq(0)
var rows = table.find('tr:gt(0)').toArray().sort(comparer($(this).parents('th').index()))
this.asc = !this.asc
if (!this.asc){rows = rows.reverse()}
for (var i = 0; i < rows.length; i++){table.append(rows[i])}
})
});

function comparer(index) {
return function (a, b) {
var valA = getCellValue(a, index), valB = getCellValue(b, index)
return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.localeCompare(valB)
}
}

function getCellValue(row, index) {
return $(row).children('td').eq(index).text()
}

Here #locationSort, #trainSort, #faultsSort are ids of image button to be clicked to sort beside the TH header. I am eager to hear from you.

Replies

  • allanallan Posts: 63,531Questions: 1Answers: 10,474 Site admin
    It looks like the issue is that you are modifying the cell values without telling DataTables. You need to use the API to update cell values so DataTables is aware that they have changed and so it can take the new values into account when sorting and filtering, etc.

    See also the FAQs: http://datatables.net/faqs#append .

    Allan
  • allanallan Posts: 63,531Questions: 1Answers: 10,474 Site admin
    p.s. Thank you for your duplicate message by the web form. Rather than duplicating my reply, I'll just reply once here.
  • SimondiSimondi Posts: 6Questions: 0Answers: 0
    Hi Allan,

    Thanks for the explanation. This is what I did to update cells, what do you think I can do with fnUpdate for achieving the result you described?

    var thisTD = $("#tb_consist_Status td.trainId").filter(function () {
    return $(this).text() == id;
    });

    thisTD.prev().text(siteName);
    thisTD.next().next().text(Math.floor(this.health / 10000));
This discussion has been closed.