How can I limit number of selected rows?

How can I limit number of selected rows?

whatacarwhatacar Posts: 7Questions: 3Answers: 1

Can you please help me to limit the number of the selected rows to 5, so I am able to select any rows in the table, but the total number of the selected rows shouldn't exceed 5 rows.
Thanks a lot.

This question has an accepted answers - jump to answer

Answers

  • whatacarwhatacar Posts: 7Questions: 3Answers: 1

    I forgot to mention that I am using the Select extension in multi item selection mode.

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421
    edited August 2017 Answer ✓
    var table = $('#example').DataTable();
     
    table.on( 'select', function ( e, dt, type, ix ) {
       var selected = dt.rows({selected: true});
       if ( selected.count() > 5 ) {
          dt.rows(ix).deselect();
       }
    } );
    
  • whatacarwhatacar Posts: 7Questions: 3Answers: 1
    edited August 2017

    Thanks a lot for the code. It works like a charm! Your helpful replay to my question is nothing less than an act of generosity and that is what makes the programmer community a very nice place to be. Generosity makes the world go round ! Not money. Thanks again for your help!

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421

    Thanks a lot for your kind words! I hadn't used multi select before and learned something, too. I did this translation a few months ago and had no idea what it was good for. Now I know :smile: Will use multi select for a new table next week as well. Seems to be a really good way to enable multi row editing. Wasn't really aware of this earlier.

    $.extend( true, $.fn.dataTable.Editor.defaults, {            
        i18n: {        
            multi: {
                    title: "Mehrere Werte",         
                    info: "Die ausgewählten Zeilen enthalten verschiedene Werte für dieses Feld. Um alle Zeilen auf den gleichen Wert zu setzen, \n\
                           klicken Sie bitte hier. Ansonsten werden die Zeilen ihren individuellen Wert für das Feld behalten.",
                    restore: "Änderungen rückgängig machen",
                    noMulti: "Dieses Feld kann einzeln bearbeitet werden, aber nicht als Teil einer Gruppe."
            }
        }      
    
  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    I'll second that - @rf1234 - your input is of tremendous value to the community. Both the time you take to reply to others, and also your own excellent questions to me. Thank you.

    Allan

This discussion has been closed.