Find parent of selected within button action

Find parent of selected within button action

glimpsed_chaosglimpsed_chaos Posts: 140Questions: 30Answers: 4

I'm using the Select extension with Buttons on my table. I have a button with an action function which will get the selected row. That's all fine and I get the values as needed from this selected row.

I am trying to get the parent of this row, the '<tr>' , so I can check if it contains a specific class.

I've searched and seen some solutions but I have not been able to apply that in this scenario.

buttons: [
    {
        text: 'MyButton',
        action:  function (e, dt, node, config) {
            var selectedData = table.row({ selected: true }).data();
        }
    }
]

Replies

  • glimpsed_chaosglimpsed_chaos Posts: 140Questions: 30Answers: 4

    I've tried evaluating this within the action but it comes back as false.

    if ($(table.row({ selected: true })).hasClass('.flagclass'))
    

    I would have that the table.row({ selected: true }) would be the <tr> I am looking to check.

  • kthorngrenkthorngren Posts: 21,337Questions: 26Answers: 4,954

    Use row().node() to get the tr. See if this works:

    $( table.row({ selected: true }).node() ).hasClass('.flagclass'))
    

    Kevin

  • glimpsed_chaosglimpsed_chaos Posts: 140Questions: 30Answers: 4
    edited June 2023

    @kthorngren

    I tried it and no luck.

    Here is a demo to show - https://live.datatables.net/caqutoru/2/

    If you select 'Cedric Kelly' row it should log to the console "This row is flagged". And not for that any when other row is selected.

    No matter what row selected I cannot get it to properly determine the class on the row.

  • glimpsed_chaosglimpsed_chaos Posts: 140Questions: 30Answers: 4

    @kthorngren

    I found out what it was... ugh! That little period should not be present when looking for the class.

    $(table.row({ selected: true }).node()).hasClass('flagclass')
    

    Fixed example -
    https://live.datatables.net/caqutoru/3/edit

    Thanks for pointing me to the "node" solution.

Sign In or Register to comment.