1

Is it possible to format the Ajax data before inserting it into the page using the DataTables sAjaxSource": "URL"?

I'm currently fettching the following JSON array:

{
    "aaData": [
        {
            "Name": "Trident",
            "Link": "http://google.com"
        }
    ]
}

I can insert it into the Name and Link into the dataTable using:

var oTable = $('#example').dataTable({
    "bProcessing": true,
    "sAjaxSource": "sources/myData.json",
    "sAjaxDataProp": "items",
    "aoColumns": [
        { "mData": "Name" },
        {"mData": "$Link" }
    ]
});

however, I want to insert it as a single Anchor element with the Text= Name and the href = Link. Is this possible?

PS: what have I tried: I looked throw the examples and searched with google but found nothing.

1
  • So you want 1 column instead of 2? But as an anchor? Commented Jan 4, 2013 at 14:55

1 Answer 1

3

I'm a veteran of the old fnRender method, but I believe it is now done like so with mRender:

var oTable = $('#example').dataTable({
    "bProcessing": true,
    "sAjaxSource": "sources/myData.json",
    "sAjaxDataProp": "items",
    "aoColumns": [
        //{ "mData": "Name" },
        { "mData": "Link",
          "mRender": function (data, type, full) {
              return '<a href="' + full[1] + '">' + full[0] + '</a>';
          } }
    ]
});

PS: I edited this to reflect your comment clarifying that you wanted it in a single column. Uncomment the line { "mData": "Name" }, to add the name column back in. Also remember your table definition should also be changed for one or two columns.

Sign up to request clarification or add additional context in comments.

2 Comments

Great, but is it ok if I skip the mRender function and use a function as mData? ("mData": function (source, type, val) { return '<a href="' + source.Href + '">' + source.$name + '</a>'; })
I was wondering that myself. I've been stuck using an older version for a long time so I am new to the mData property and the extent of what it can do. My worry is the function you posted will get escaped to text instead of being printed as HTML output - all I can say is try it and see.

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.