0

NOTE - Datatable serverside processing with MySql database

and the code is as follows

$(document).ready(function(){
 var table = $('#userDataList').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "fetchData.php",
        
stateSave: 'true',

    });
});

Also the table / page available at

http://testlearn.infinityfreeapp.com/index-old.html

1)

How to add a class to all

a) even rows dynamically.

b) odd columns dynamically.

2)

How to add a class with a button click to each odd rows with serverside processing with datatable ?

For example

$('tr:nth-child(odd)').addClass('whatever'); 

the above code is useful to add the code for the current page only.

But when we make a change like pagination / search / next page / order then the added class will disappears.

Also try something like

dt.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
      var row = dt.row(this).node();
      if ( rowIdx % 2 === 0 ) {
          $( 'td:eq(3)', row ).addClass("yellow");
      }                
});

the above code also applies / works on the current page only.

3
  • Assuming there's no additional logic (eg when value<0 then add class red), you can do the same with css: eg table > tbody > tr:nth-child(odd) > td:nth-child(4) jsfiddle.net/vkcodtry (using Vincent's fiddle). Commented Aug 31, 2024 at 10:32
  • i use a button click to do that function. available at testlearn.infinityfreeapp.com Commented Aug 31, 2024 at 12:25
  • Self duplicate of: Serverside dynamically addClass Datatables Commented Jun 23 at 9:15

1 Answer 1

0

You should add your JS code on events, you can use the draw.dt event which is triggered every time the data is rendered, more information: https://datatables.net/reference/event/draw

var dt = new DataTable('#myTable');
 
dt.on('draw', function () {
   dt.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
      var row = dt.row(this).node();
      if ( rowIdx % 2 === 0 ) {
         $( 'td:eq(3)', row ).addClass("yellow");
      }                
   });
});

Not so sure about the syntax. Here is a Fiddle: https://jsfiddle.net/s7at8dgz/2/

Sign up to request clarification or add additional context in comments.

9 Comments

but it works on the current page only.
@varghese check at the fiddle I added in my answer, the event is triggered on pagination also
Vincent - how to integrate it with at testlearn.infinityfreeapp.com
vincent - or how to integrate it with at testlearn.infinityfreeapp.com/index-old.html i am a beginner in serverside and datatables. so knowledge is very low average.
Vincent - or how your code can integrate with my basic code as $(document).ready(function(){ var table = $('#userDataList').DataTable({ "processing": true, "serverSide": true, "ajax": "fetchData.php", stateSave: 'true', });
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.