2个数据变量的mrender不起作用

时间:2015-01-02 06:52:44

标签: javascript arrays ajax jquery-datatables

我想将mrender用于2个数据变量。我的ajax数据源就像:

{"campaigns":{"campDetails":[{"campaign_id":"1012","campaign_name":"RP - Axe Sample (Submit)","campaign_desc":"Get your FREE sample of Axe Deodorant!","cost":"$0.00","epc":"$0.14","cr":"8.01","payout":"$1.25","url":"http:\/\/www.adworkmedia.com\/go.php?camp=1012&pub=24043&sid=","image":"http:\/\/www.adworkmedia.com\/image.php?pub=24043&id=3061&sid=","categories":"Email & Zip Submits, Survey \/ Freebie \/ Sweepstakes","incent":"Incent,No Points,No Cash","countries":"US","teaseText":"Get a Sample of Axe Deodorant! Details Apply!","teaseDesc":"Get a Sample of Axe Deodorant with your participation!","conversion_point":"E-mail Submit","device_type":"All Devices (Mobile Friendly)"},{},{}....]}}

所以我使用sAjaxDataProp以

的形式呈现数据
$(document).ready(function() {
    $('#example').dataTable( {
         "order": [[ 2, "desc" ]],
        "sAjaxSource": "http://localhost/survey/justearn/index/dist/downloadfile/php/",
        "processing": true,
        "sAjaxDataProp": "campaigns.campDetails",
         "aoColumns": [
            { "mData": "campaign_name" },
            {
    "mData": "url",
    "mRender": function(data, type, val) {
        switch (type) {
            case 'display':
                return '<a href="/data/' + url + '">' + url + '</a>';
                break;
            // optionally, add case statements for 'sort', 'filter', and 'type'
            default:
                return data.bar;
                break;
        }
    }
},
            { "mData": "cr" }



        ]
    } );
} );

}

我能够在处理时获得campaign_name和cr列,但第二列似乎有错误,那是因为我想将2 mData用于相同的mRender函数

我尝试使用

 {"mData": "foo",
     "mData2": "bar",
     "mRender": function(data, data2){
          return '<a href="/data/' + data + '">bar</a>'; //
      }
    }

由于我是数组新手,你可以帮我解决第二列,所以我可以使用mRender它,你可以使用上面发布的ajax源代码,我希望第二列的两个mData变量为“url”和“campign_desc”使用上面ajax数据源中的mRender。

1 个答案:

答案 0 :(得分:0)

如果您使用Firebug调试代码,您将能够看到val包含行数据,因此如果您想要的方式显示它只是以下情况:

{
   'mRender': function (data, type, val) {
            return <a href="/data/' + val[0] + '">' + val[1] + '</a>';
    }
}