1

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

How to add a class to all

a) even rows dynamically.

b) odd columns dynamically.

(with or without a button click)

1
  • Please see How to Ask, then revise your title to ask a clear, specific question in plain English. Also note that you're expected to make an attempt and show your effort. Commented Sep 5, 2024 at 14:11

1 Answer 1

0

You can do this using rowCallback() on datatable initialisation.

$(document).ready(function () {
  var table = $("#userDataList").DataTable({
    processing: true,
    serverSide: true,
    ajax: "fetchData.php",
    stateSave: "true",
    rowCallback: function (row, data, index) {
      if (index % 2 == 0) {
        $(row).addClass("oddRow");
      } else {
        $(row).addClass("evenRow");
      }
    },
  });
});

Use this CSS to see the difference

table.dataTable tbody tr.evenRow{
    background-color: green;
}
table.dataTable tbody tr.oddRow{
    background-color: red;
}

Demo

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

6 Comments

Yuvaraj M - I don't know how to add separate class to each header cell. Did you know that ? If 'YES' can you share with me ?
so you want unique class names for each headers right ?
possible to do it with css ?
unable to create any more questions that was informed by stackoverflow. No problem leave it. cheers
|

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.