Sunday, June 25, 2023

DataTables.net table rows can be toggled (hide or show) using a checkbox based on some conditions.

If you are planning to display only certain data rows based on a condition in DataTables.net, here is the JavaScript code to do that.


function ToggleDataTableRows() {
    if (dataTableObject != null) {
        let hideSomeDataChecked = $("#hideSomeDataCheckBox").is(":checked");
        if (hideSomeDataChecked == true) {
            $.fn.dataTable.ext.search.push(
                function (settings, data, dataIndex) {
                    // If data in 6th Column does not meet some condition
// return true. Which means some rows are being hidden now.
                    if (data[5] != "SomeCondition") {
                        return true;
                    }
                }
            );
        }
        else {
            // Retrieve data
            // var data = hpDataTable.column(1, { search: 'applied' }).data();
            // Restore search criteria
            $.fn.dataTable.ext.search.pop();
        }
        displayTableObject.draw();
    }
}

All Blogs so far ...