Adding data attributes depending on ajax data

Adding data attributes depending on ajax data

OmnicakeOmnicake Posts: 2Questions: 1Answers: 0

Hello.
I want to pass a data to my Datatable with structure like this

[{mainValue: 1, additionalValue: 'one'}, {mainValue: 2, additionalValue: 'two'}... and so on] 

and then in resulting table get a cells in format like this (adding main value as text inside a cell and second value as data-attribute):

<td data-value="one">1</td>
<td data-value="two">2</td>

I know. that I can export 'mainValue' by columns.data option but how can I adding data-attribute with additional value?

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586
    Answer ✓

    You could do that within createdRow,

    Colin

  • OmnicakeOmnicake Posts: 2Questions: 1Answers: 0

    Thank you, that's really help me.
    But now Im struck with another problem.
    Im trying to use Scroller extension with colReorder plus I have a createdRow function like that:

          createdRow: function(row: any, data: any, dataIndex: any) {
            Object.keys(data).forEach((el, idx) => {
            let cell = data[el];
              if (cell.calcValue) {
                $("td", row).eq(idx).attr("data-value", cell.calcValue);
            }
            });
          }
    

    It works fine until I'm trying to reorder some column and fetch new data from ajax.
    Function bound to index in array and if I change it - it starts to add attribute to incorrect column.
    Is there any way to get new order of columns and map it to object keys?

  • colincolin Posts: 15,144Questions: 1Answers: 2,586
    Answer ✓

    There's a lot going on there. We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

This discussion has been closed.