0

I would like to do some css with my jquery Datatable however the only things I found was with html tables. Is it possible to change the look of a datatable when it takes its data from a js array ?

$('#example').DataTable({
    destroy: true,
    "scrollY": "200px",
    "scrollCollapse": true,
    "paging": false,       
    data: datatable,
    columns: [
        { title: "title1" },
        { title: "title2" }
    ]
});

Is there some parameters I could add here ?

3
  • What type of CSS are you trying to implement? Commented May 22, 2017 at 7:53
  • @mmushtaq Something like changing the color of the cells,the text,or the border (like we can do when we use an html table). Commented May 22, 2017 at 7:56
  • 1
    Why not. You can do it with datatable as well. Commented May 22, 2017 at 9:02

1 Answer 1

1

Yes. Look at createdRow and createdCell. Here is an example to demonstrate how to use those callbacks :

var table = $('#example').DataTable({
  createdRow: function(row, data, dataIndex ) {
    $(row).css('color', 'red')
  },
  columnDefs: [{
    targets: 3,
    createdCell: function(td, cellData, rowData, row, col) {
      $(td).css('color', 'green')
    }
  }]
}) 

demo -> http://jsfiddle.net/qsh3zgcc/


Or, as @mmushtaq points out, do what you wanna do in CSS, as you are used to do it :

table.dataTable tbody tr {
  color: red;
}
table.dataTable tbody tr td:nth-child(4) {
  color: green;
}

produces the exact same as the above callbacks.

demo -> http://jsfiddle.net/prkf1y90/

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

Comments

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.