3

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!

1 Answer 1

2

I hope it will works, I am not sure about it. because I have not tested from my side as you have mentioned other variables also. Still I have tried my best

var $tr = $tbody.find('tr');

var sorted_tr= $tr.sort(function (a, b) {
    var tda = checkboxSort ? $(a).find('td:eq(' + columnNumber + ') checkbox').is(':checked') : $(a).find('td:eq(' + columnNumber + ')').text().toLowerCase();
    var tdb = checkboxSort ? $(b).find('td:eq(' + columnNumber + ') checkbox').is(':checked') : $(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;
    }
});

$tbody.html(sorted_tr);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.