Select only two rows

Select only two rows

Andreas S.Andreas S. Posts: 208Questions: 74Answers: 4

Is it possible to restrict the multi select for two rows?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin
    edited October 2017 Answer ✓

    Yes - you could use the user-select event to cancel the addition of rows if two are already selected.

    Allan

  • Andreas S.Andreas S. Posts: 208Questions: 74Answers: 4

    I have used the code at the end to activate the button. Is with the 'user-select' event possible to activate and deactivated the button. I think so, but what is the different between both events. I did not found the description about the 'select' event.

    This opens another problem for me. How get the data of the selected rows. I tried in the action of the Button to get the data of both selected rows, but only I get an error. With a single row I get the data.

    Andreas

    rlist.on(
        'select', function () {
          var selectedRows = rlist.rows( { selected: true } ).count();
          rlist.button( 0 ).enable( selectedRows === 2 );
        }
      );
    
    buttons: [
          {
            text: '<i class="fa fa-code-fork"></i>',
            titleAttr: 'Merge Entries',
            className: 'txt-BlueViolet',
            action: function ( e, dt, node, config ) {
              var data = rlist.row( { selected: true } ).data();
              $.post({
                url: '/wp-content/plugins/finswim-cup-tools/admin/fsct-server-processing.php?m=1',
                data: data
              })
            },
            enabled: false
          }
    
  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin
    Answer ✓

    The user-select event should only be used to cancel selection if your logic requires that. You should use select to know when a row has actually been selected (i.e. it has been allowed by user-select or has been triggered via the API).

    How get the data of the selected rows.

    rlist.rows( { selected: true } ).data()
    

    would get the data. If that doesn't work, I'd need a link to a test case showing the issue. Also note that you would need to use toArray() to get a plain array of data if you are sending that via Ajax rather than a DataTables object.

    Allan

This discussion has been closed.