Keeping a single row selected, even after multiple clicks?

Keeping a single row selected, even after multiple clicks?

faintedremix009faintedremix009 Posts: 2Questions: 1Answers: 0

I am currently working on a datatable with these parameters.

        var sampleTable= $('#sampleTable').DataTable({
            bFilter: false,
            bInfo: false,
            deferRender: true,
            ajax: {
                 url: someURL
            },
            columns: [
                {data: "field1"},
                {data: "field2"},
                {data: "field3"}
            ],
            rowId: 'field1',
            select: 'single',
            paging: false
         });

Is there a way to keep a row selected if the end user has clicked on the particular row multiple times?

Also, is there a way to check if that row was previously selected on click?
For example:

        $('#sampleTable tbody').on('click', 'tr', function() {
            var rowData = sampleTable.row( this ).data();
            
            // If the previous state of this row was selected... //

            var url = "someGeneratedURL";

            $.getJSON(url, function(data) {
                doSomething();
            });
        });

Notes: This sampleTable is dynamic and is calling sampleTable.ajax.reload() on a set interval.

Answers

  • faintedremix009faintedremix009 Posts: 2Questions: 1Answers: 0

    I have a work around for question 1.

    // Sample Table definition
    var sampleTable= $('#sampleTable').DataTable({
        ajax: {
             url: someURL
        },
        columns: [
            {data: "field1"},
            {data: "field2"},
            {data: "field3"}
        ],
        rowId: 'field1',
        select: 'single'
     });
    
    $('#sampleTable tbody').on('click', 'tr', function() { 
        sampleTable.rows(this).select();
     
        /*... Do additional logic here ...*/
    });
    

    This allows the sampleTable to keep a row selected despite multiple clicks. Happy coding!

  • xginoxxginox Posts: 1Questions: 0Answers: 0

    thanks faintedremix009!

This discussion has been closed.