Get value of attribute

Get value of attribute

StijnQCAStijnQCA Posts: 8Questions: 2Answers: 1

Hello all,

I have an application in which each column has to be formatted differently. I use render in the following way:

columnDefs: [
    {
        targets: "_all",
        render: function (data) {
                return window[?????](data);
        }
    }
],

On the position of the questionmarks i would like to have the value of an attribute; data-??? (the name doesnt really matter)
What attribute can I use for this?

Even better would be to have:

<th data-render="function"></th>

Is there any way to do this?

Thanks

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Hi @StijnQCA ,

    Not sure if this what you're looking for, but you can give each column a unique class with columns.className. That would allow you to then set CSS formatting rules for each.

    Hope that helps,

    Cheers,

    Colin

  • StijnQCAStijnQCA Posts: 8Questions: 2Answers: 1

    Hey @colin

    Thanks for the quick reply.

    I want to use a function for each column that formats the cell in a specific way.
    For example the status column should be formatted like:

    function formatStatus(data) {
        return '<span class="label label-' + data.style + '">' + data.value + '</span>'
    }
    

    Unfortunately this is not achieved by setting the columns.className| Option.

  • Rob BrunRob Brun Posts: 56Questions: 2Answers: 14

    Hi StijnQCA, I think that you may want to use the created row callback and then apply a class to the cell. Say your column is in the 4th position.

     "createdRow": function (row, data, index) {
           var style = data.style
           $('td', row).eq(4).addClass(style);
    });
    

    I don't know if this can work I haven't tested it.

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Hi again,

    Yep, that's just for the CSS class. If you want to do more, and change how it looks, you can use columns.render, and the display option. Take a look at this example - it changes the column into a URL and truncates with ellipses.

    Hope that's the one you're after!

    Cheers,

    Colin

  • StijnQCAStijnQCA Posts: 8Questions: 2Answers: 1

    I ended up adding the value to the data object like:

    {
        targets: "_all",
        render: function (data) {
            return window[data.formatter](data);
        }
    }
    

    this way the formatter used depends on what is retrieved from the server.

This discussion has been closed.