Add HTML attributes for individual cells on server side

Add HTML attributes for individual cells on server side

edmedm Posts: 19Questions: 6Answers: 0
edited May 2017 in Free community support

Hello,

how can i dynamically add HTML attributes for individual cells on server side and pass through AJAX without complex client processing? The attributes should be part of the ajax answer/JSON object (because the rows and cells (in the same column) have different meanings), I need:

  • id for table rows (tr)
  • class for (some) table columns (td)
  • title for (some) table columns (td)

Perhaps something like the "DT_RowId" machanism (https://datatables.net/examples/server_side/ids.html), but for td cells?

Answers

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    DataTables doesn't currently have a built in mechanism for assigned classes and attributes to individual cells based on the data that is returned from the server, like it does for rows (id, class and arbitrary attributes).

    However, you can use the columns.createdCell method for the columns to easily modify the cell's DOM object in any way you need to based on the returned data.

    Allan

  • edmedm Posts: 19Questions: 6Answers: 0
    edited May 2017

    like it does for rows (id, class and arbitrary attributes)

    Oh, perhaps in a later version ;)? Like the "DT_RowData" mechanism (https://datatables.net/manual/server-side), but something like this:

    "first_name": "Angelica",
    "first_name_CellAttr": {
        "id": "cellId",
        "class": "cellClass",
        "title": "cellTitle",
        ...
    },
    

    modify the cell's DOM object in any way you need to based on the returned data

    The class, titles etc. depends not on the content of the cells. Ok, then i have to create the tables with PHP on the server (current processing). Thanks for the hint.

    Eduard

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    Oh, perhaps in a later version

    It is possible yes. I've thought about it a number of times, but am a little concerned about the potential performance impact (needing to check if the cell data is an object or not for every cell). It is something that might make it into a future release though.

    Allan

  • edmedm Posts: 19Questions: 6Answers: 0

    performance impact

    Ok, that could be a reason against. On the other hand: How many cells do the tables have in real life?

    Or you can use a keyword (like the "recordsTotal" or "draw" parameter) at the beginning, to indicate the use of "sub"-objects in the answer - only my 50 cent.

    Eduard

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    On the other hand: How many cells do the tables have in real life?

    Tens of thousands is not uncommon. However, with the next major version I'm considering having deferRender enabled by default which would effectively resolve this issue.

    Allan

  • Karl_SKarl_S Posts: 20Questions: 6Answers: 0

    I did a search and didn't find anything, but asking hoping I have missed something. Has the ability to set custom data attributes per cell been implemented?

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    No, sorry, this hasn't progressed since Allan's earlier replies.

    Colin

This discussion has been closed.