Stefan
Stefan

Reputation: 14863

jQuery DataTables ajax format data

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.

Upvotes: 1

Views: 3991

Answers (1)

Codesleuth
Codesleuth

Reputation: 10541

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.

Upvotes: 3

Related Questions