mardi 26 juillet 2016

JS table sort by checkbox

I have a table with multiple data types on columns (date, string, int and checkbox). I want to sort it by the checked property of the checkbox.

I have done most of the function but I am stuck in getting the checkbox value from the td.

$tbody.find('tr').sort(function (a, b) {
    var tda = checkboxSort ? $(a).find('td:eq(' + columnNumber + ')').find('checkbox').val() : $(a).find('td:eq(' + columnNumber + ')').text().toLowerCase();
    var tdb = checkboxSort ? $(b).find('td:eq(' + columnNumber + ')').find('checkbox').val() : $(b).find('td:eq(' + columnNumber + ')').text().toLowerCase();
    //...other sortings based on type

    if (ascending) {
        return tda < tdb ? -1 : tda > tdb ? 1 : 0;
    } else {
        return tdb < tda ? -1 : tdb > tda ? 1 : 0;
    }
}).appendTo($tbody);

Here "checkboxSort ? $(a).find('td:eq(' + columnNumber + ')').find('checkbox').val()" i cant manage to sort it out.

Can someone help me? Thanks!




Aucun commentaire:

Enregistrer un commentaire