how to select row on right click?

how to select row on right click?

johnny83johnny83 Posts: 3Questions: 2Answers: 0

Hello! The select plugin is great. when I use select: true it selects the row when click on it. I need same action on right click. Also need to get the row id on right/left click? Could you please help me to do this?

Replies

  • onurkulekcionurkulekci Posts: 1Questions: 0Answers: 0

    you can do with the mousedown event

    Table = $("#exampletable ").DataTable({});

    $('#exampletable tbody').on('mousedown', 'tr', function () {

    exampletable .$('tr.selected').removeClass('selected');
    $(this).addClass('selected');

    });

  • MarianLMarianL Posts: 4Questions: 0Answers: 0

    But this does not work well in conjunction with multiselect and SHIFT selections.

    For example, if you select one row with right-cllick (manually by setting the selected attribute), the next LMB-SHIFT will be taking the last left-clicked row and not the last right clicked as one would expect.

    side note: RMB and SHIFT/CTRL does not work either.

    live.datatables.net/toqugago/3/

    I do not need RMB + CTRL/SHIFT, but it would be nice if it were possible to set the selected attribute **and ** the basis for the next LML+SHIFT. Is that possible?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    This thread here might help, it discusses some right-click behaviour.

    Colin

  • MarianLMarianL Posts: 4Questions: 0Answers: 0
    edited May 2020

    I know this thread, but I do not think it answers the problem. My comment is not addressing the interception of the right-click. This works fine as shown in the live example. My point is that the setting of the select property when intercepting the right-click does confuse the next left-click+SHIFT selection.

    Or am I missing something there?

    Coincidentally, I'm using the same contextmenu plugin as the other case opener, so maybe I'm overlooking something.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I suspect that's beyond the scope of this forum, it's likely to be a question better asked on StackOverflow. If you have a working demo of that, and can give steps on how to reproduce, we're happy to take a look,

    Colin

  • MarianLMarianL Posts: 4Questions: 0Answers: 0

    Since the SHIFT/CTRL-Select is a DataTables feature i thought this is the correct place to ask. From my perspective, the last selected (selection by capturing RMB and setting the select attribute in the event handler) needs to be stored for the next SHIFT+mouse event.

    User experience with the live example I provided at live.datatables.net/toqugago/3/ and LMB (as expected):
    1. LMB on row 2 (row 2 is selected)
    2. LMB on row 3 (row 3 is selected)
    3. LMB + SHIFT on row 8 (rows 3..8 are selected)

    Now when we add the right click as the selection (as is done in the example):
    1. RMB on row 2 (row 2 is selected by the event handler)
    2. RMB on row 3 (row 3 is selected by the event handler)
    3. LMB + SHIFT on row 8 (rows 2..8 are selected) - expected behavior would be rows 3..8.

    If there was a way to mark row 3 with the second step as the reference for step 3 (as it is done apparently with the first LMB-steps) it could be solved.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Since the SHIFT/CTRL-Select is a DataTables feature....

    That is routine keyboard processing, not exclusive to DataTables.

  • allanallan Posts: 63,678Questions: 1Answers: 10,497 Site admin

    @MarianL - You've got this code in your example:

      $('#example tbody').on('mousedown', 'tr', function () {
        table.$('tr.selected').removeClass('selected');
        $(this).addClass('selected');
      });
    

    That mousedown will trigger on left and mouse click. You could use:

      $('#example tbody').on('mousedown', 'tr', function (e) {
       if (e.button !== 0) {
        return; // not left mouse button
       }
        table.$('tr.selected').removeClass('selected');
        $(this).addClass('selected');
      });
    

    That's nothing to do with DataTables or Select though. See the MDN docs if you want to learn move about that.

    Allan

  • MarianLMarianL Posts: 4Questions: 0Answers: 0

    Ok, thanks for your comments.

This discussion has been closed.