Selecting rows by dynamically created class

Selecting rows by dynamically created class

NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
To access some table rows easily later on during some application processing, I assigned classes to rows based on some column values:

Added here:

myTable = $("#MyTable").DataTable({
    ....
    rowCallback: function (row, data, index) {
        $(row).addClass('row_' + data.SOMECODE);
        ...
    }
});

Later, in an event handler:

var someCode = data.SOMECODE;  // Let's say 31038
myTable.rows("'.row_" + someCode + "'").every(function (rowIdx, tableLoop, rowLoop) {
    ....
});

Here, I get:

Syntax error, unrecognized expression: '.row_31038'

Normally, I can access rows like:

myTable.rows('.selected').every(function (rowIdx, tableLoop, rowLoop) {

Answers

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951

    I think the problem is with the single quotes. You probably want something like this:

    myTable.rows( ".row_" + someCode ).every(
    

    Kevin

  • NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

    Thanks. I now proceed to open my eyes!

    The reason I am doing this is that I have a checkbox select column to allow users to select records to delete. Some selected records can have duplicate column value (in one column, called BINCODE. I need to be able to iterate through the selected records, group by the duplicate column, set one as "selected" and remove "selected" class from others. This is why I was adding a class called "row_" + BinCode to thos rows so I could easily find/group them.

    tblOpenBins.rows('.selected').every(function (rowIdx, tableLoop, rowLoop) {
        var currRow = $(this.node());
        var binCode = tblOpenBins.row(rowIdx).data().BINCODE;
        var binStatus = tblOpenBins.row(rowIdx).data().BINSTATUS;
        var lastModified = tblOpenBins.row(rowIdx).data().LASTMODIFIEDON;
    
        if (binStatus == '3') {  
            // get a group of rows with same bin code
            tblOpenBins.rows(".row_" + binCode).every(function (rowIdx, tableLoop, rowLoop) {
                // Unselect all
                $(this.node()).removeClass('selected');
            });
            // set the one with status 3 as selected
            currRow.addClass('selected');
        }
        else {
            // get a group of rows with same bin code
            tblOpenBins.rows(".row_" + binCode).every(function (rowIdx, tableLoop, rowLoop) {
                // Set oldest ones as unselected
                if (lastModified < $(this.node()).data().LASTMODIFIEDON) {
                    $(this.node()).removeClass('selected');
                }
            });
            currRow.addClass('selected');
    });
    

    This does not seems to be correct, probably because of the way I am selecting row.
    I need to be able to keep the selected record if status = 3 or last modified is newer and unselect others.
    I am not sure if I am doing this (i.e., selecting records) the right way.

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    edited February 2023

    If you are using the Select extension then you will probably want to use the row().select() or rows().select() and row().deselect() or rows().deselect() APIs. Maybe something like this:

    tblOpenBins.rows(".row_" + binCode).deselect();
    

    Or within the rows().every() loop use something like this:

        if (binStatus == '3') { 
            // get a group of rows with same bin code
            tblOpenBins.rows(".row_" + binCode).every(function (rowIdx, tableLoop, rowLoop) {
                // Unselect all
                this.deselect();
            });
    ....
    

    Maybe you can post a link to your page or a test case replicating your solution so we can see exactly what you have to offer suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

    Thank you Kevin; this helped a lot.

Sign In or Register to comment.