1

One of my table's columns needs to render a link based on several object's properties. It's simplified definition would look something like this:

columns: [{
    "data": "obj",
    "render": function(data, type, row) {
        return "<a href='" + obj.x + "'>" + obj.y + "</a>";
    }
} ... ]

It does render as intended, although I would also like the column to be sortable by obj.y property. This is where my problem begins, as I found the mechanics a little bit confusing, but maybe hopefully I just keep missing some obvious solution.

I've attempted to add my custom sorting to jQuery.fn.dataTableExt.oSort, and then set the column's sType, and else to add data-order/data-sort attribute to each cell during rendering, but neither of those worked - the column still seems to apply it's default ordering.

Is there any recommended approach I should try? I'm using plugin's version 1.10.

1 Answer 1

4

If I understood correctly, the render function has a type parameter just for cases like this.

This parameter takes values like 'display','sort','filter', which indicate the context the data is being rendered on.

So in your case, you should check if type === 'sort' and then return the data you want datatables to consider when sorting by that column:

"render": function(data, type, row) {
    if(type==='sort'){
        return obj.y;
    }
    return "<a href='" + obj.x + "'>" + obj.y + "</a>";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.