getting custom data attributes with data

getting custom data attributes with data

cool_bombomcool_bombom Posts: 17Questions: 4Answers: 0
edited March 15 in Free community support

Hi

I just updated to DataTables version 2 from version 1.

Earlier when calling row().data() the data retrieved was an json object and keys was either the data-data attribute on the th or the index.

example:
html: ".....<th></th><th datat-data='template'></th>....."
calling row().data() returned: {"0":"<div>...</div>", "template":"<div>....</div>"}

version 2 returns an array of columns data, but i now miss the identifier "template" that i used earlier to get the specific data i needed.

example:
html: ".....<th></th><th datat-data='template'></th>....."
calling row().data() returns: ["<div>...</div>", "<div>....</div>"]

is it possible to set a custom data attribute on the tr or th, so i can identify the given column i wish to get the data from, something like the data-sort attribute is included in the array object.

example:
html: ".....<th></th><th datat-data='template'></th>.....<td data-sort='test'>...</td>"
calling row().data() returns: ["<div>...</div>", {"display":""<div>...</div>"","@data-sort":"test"}]

reference: https://datatables.net/reference/api/row().data()

kind regards
cbb

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,434Questions: 1Answers: 10,458 Site admin

    Can you give me a link to a page that shows the issue so I can debug it please?

    Thanks,
    Allan

  • cool_bombomcool_bombom Posts: 17Questions: 4Answers: 0

    Hi Allan

    example:
    https://codepen.io/coolbombom/pen/GRLjeao?editors=1111

    kind regards
    cbb

  • cool_bombomcool_bombom Posts: 17Questions: 4Answers: 0

    Hi Allan

    Just to clarify, it is more a "how do i" than an issue. because i am missing a feature that was in version 1.

    kind regards
    cbb

  • allanallan Posts: 63,434Questions: 1Answers: 10,458 Site admin
    Answer ✓

    Thanks for the link!

    I actually don't see the template property included in the result from row().data() if I update it to use DataTables 1.13.11 (or 1.11.0 or 1.12.0).

    However, I see your point - how to get that data. If it was included in DataTables before, that was more of a happy accident than something that was planned. What to do is use cell().node() and read the attribute from the td element directly.

    I don't seem to be able to fork your CodePen, but:

    table.cell(event.currentTarget, 0).node().getAttribute('data-name')
    

    should do it.

    Allan

  • cool_bombomcool_bombom Posts: 17Questions: 4Answers: 0

    Hi Allan

    thank you for your answer. I managed by finding index of th with attribute data-name, and then getting data from row().data() with the found index.

    kind regards
    c_bb

Sign In or Register to comment.