sorting problem: improvement over extracting td value
sorting problem: improvement over extracting td value
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.
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.
This discussion has been closed.
Replies
See also the FAQs: http://datatables.net/faqs#append .
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));