Appending html to datatables columns after draw

Appending html to datatables columns after draw

parsonsparsonsparsonsparsons Posts: 29Questions: 8Answers: 0

Is there any easy way to add a div or span to a specific column index's data?

So if I have an array of columns 1,3 and I want to add a div surrounding those columns data how would I do that? Thank you.

This question has accepted answers - jump to:

Answers

  • parsonsparsonsparsonsparsons Posts: 29Questions: 8Answers: 0

    As I'm thinking about it I believe another solution would be if I could just add a class to the <td> that contains that row's data. In fact I think that would be the preferable solution.

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769
    edited December 2019 Answer ✓

    You can use columns.className to add the class name to a td.

    Kevin

  • parsonsparsonsparsonsparsons Posts: 29Questions: 8Answers: 0

    Is it possible to invoke that method after the table has been created through the API?

  • parsonsparsonsparsonsparsons Posts: 29Questions: 8Answers: 0

    I believe I got it to work like this, is using columndefs twice proper syntax?

                    columnDefs: [{ targets: j, orderable: false }],   //make first column invisible
                    columnDefs: [{ className: "my_class", targets: columnsThatHaveMouseover }],
    
  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769
    Answer ✓

    You can use createdRow or columns.createdCell. If the data can change then you can use rowCallback.

    Kevin

  • parsonsparsonsparsonsparsons Posts: 29Questions: 8Answers: 0

    Sounds good, thank you!

  • parsonsparsonsparsonsparsons Posts: 29Questions: 8Answers: 0

    One last question, is there any easy way to avoid applying the class to the column headers?

  • colincolin Posts: 15,143Questions: 1Answers: 2,586
    edited December 2019 Answer ✓

    The class name would be on the header if you use columns.className, if you use one of the three options Kevin suggested in his last mail, any class applied there would only go on that td element.

    Colin

  • parsonsparsonsparsonsparsons Posts: 29Questions: 8Answers: 0

    Thanks again.

This discussion has been closed.