How to retain the rows selected message.

How to retain the rows selected message.

anknegianknegi Posts: 3Questions: 2Answers: 0
edited December 2019 in Free community support

I am trying to retain the selection of items on server sided datatables.

I created this callback:

"rowCallback": function( row, data ) {           
                
                 if ( $.inArray(data[0], selected) !== -1 ) {                
                    $(row).addClass('selected');
                 }
               },

and added the following code

 $('#datable tbody').on( 'click', 'tr', function () {
              var id = $(this).closest('tr').children('td:first').text();
              var id = parseInt(id);
              var index = $.inArray(id, selected);
              if ( index === -1 ) {
                  selected.push( id );
              } else {
                  selected.splice( index, 1 );
              } 
                   
  });

My code is working absolutely fine and I am able to retain , the selected item even when going to another page.
The problem I am facing is , I am not able to handle the n rows selected which is shown below the datatable.
Every time I changed the page , it starts from 1 item selected , while I have selected 3 items in previous page.
Can anyone help me on this, how to achieve the changes in messages as per the selection in previous page on server sided datatables.

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • colincolin Posts: 15,154Questions: 1Answers: 2,587

    Every time I changed the page , it starts from 1 item selected , while I have selected 3 items in previous page.

    This message comes from the Select extension - when you select a row, it modifies that message. In your code, you're not selecting the row, you're just adding the class. To select it, use row().select().

    Colin

This discussion has been closed.