select and deselect Event

select and deselect Event

david.j.meyer2@boeing.comdavid.j.meyer2@boeing.com Posts: 54Questions: 15Answers: 0

Description of problem:
My "select" event it populates multiple tables related to the select row using the ID of the selected record. I'm using server side processing and I only want to fire the deselect event to fire on an actual deselect. Is there a way to only fire the 'deselect' when the user has actually deselected a row not when the user selects a new record.

Here is my 'Select' event:

table.on('select', function (e) {
    $('#sPrg').DataTable().draw();
    $('#sPr').DataTable().draw();
    $('#sTech').DataTable().draw();
    $('#sTRp').DataTable().draw();
});

Here is my DeSelect event:

table.on('deselect', function () {
    $('#sPrg').DataTable().draw();
    $('#sPr').DataTable().draw();
    $('#sTech').DataTable().draw();
    $('#sTRp').DataTable().draw();
});

The problem is for every select event its making 2 ajax calls for each table because it executes the deselect then the select event. Ideally, i only want the deselect event to execute when the user is deselecting the selected record.

Hopefully that makes since.

Thanks

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,735

    See if the user-select event works for you. I believe it will fire for both user select and deselect events. It won't fire when the API is used which I believe is what you are seeing.

    Kevin

  • david.j.meyer2@boeing.comdavid.j.meyer2@boeing.com Posts: 54Questions: 15Answers: 0

    Good suggestion, I had looked at the event but it wasn't obvious on how i would determine if the selected row was changing or the user was deselecting. After thinking about it, it doesn't matter I just need to call the draw once regardless of it its a select or deselect.

    What user actions would cause this event?

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,735
    edited September 2020

    The rows can be selected using the API or by the user clicking the row. The use-select event is called only when the user clicks the row. See this example:
    http://live.datatables.net/denigobi/1/edit

    Clicking the button to use the API won't cause the user-select event to trigger.

    Kevin

  • david.j.meyer2@boeing.comdavid.j.meyer2@boeing.com Posts: 54Questions: 15Answers: 0

    That works great, how would i default the first record to be selected after the initial table load?

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,735
    edited September 2020 Answer ✓

    first record to be selected after the initial table load

    Use initComplete for this. Do you want to select the first record in the data set - depending on sorting this might not be the first record displayed. Or the first record displayed?

    This example shows both:
    http://live.datatables.net/denigobi/4/edit

    The row-selector documents the options for choosing rows.

    Kevin

  • david.j.meyer2@boeing.comdavid.j.meyer2@boeing.com Posts: 54Questions: 15Answers: 0

    the initComplete option worked but that event only fires once. I have a number of tables that load and filter off the selected table. the dawCallback seems to resolve the problem. Combined with how you suggested to select the row in the DOM.

        drawCallback: function (settings) {
            var api = this.api();
             api.row(':eq(0)').select();
        }
    

    This seems to do the job.

    thanks.

This discussion has been closed.