如何将动态列标题添加到jquery数据表

时间:2014-11-26 15:27:28

标签: jquery datatables jquery-datatables

我正在尝试将动态列标题添加到数据表中,这是我的数据表初始化代码:

var $table=$('#MSRRes').dataTable
( {
        "bFilter": false,                         
        "bDestroy": true,
        "bJQueryUI": true,
        "ajax" :{
                    url: 'getResult.php',
                    type: "POST",
                    data: {
                        formData:postData,
                        formName:'afscpMsr',
                        action:'advanceSrch'
                    }

                }
});

以下代码用于表列标题的静态初始化:                         顾客姓名            功能订单号            NCP帐号            麦肯代码            销售人员            截止日期
(DD / MM / YYYY)            SUP的数量            Zonee Id             细节              

以下代码是ajax调用,用于获取数据表结果显示以及要显示的列名:$ result是json格式,$ colcond ='cust_name,cust_no,mcn_no,sales_contact_name,order_no,COUNT(sup_id), DUE_DATE,z.zoned_from_id';

 $newarray = array(
                "draw"            => 1,
                "recordsTotal"    => sizeof($result),
                "recordsFiltered" => sizeof($result),
                "data"            => $result,
                "column"          => $colCond   
            );

            echo json_encode($newarray);

1 个答案:

答案 0 :(得分:0)

var aryColTableChecked = ["column00", "column01", "column02",    "column03","column00","column00","column00","column00"];
var aryJSONColTable = [];

for (var i=0; i < aryColTableChecked.length; i++ ) {
      aryJSONColTable.push({
                "sTitle": aryColTableChecked[i],
                "aTargets": [i]
       });
};

将其包含在数据表定义中:

 var $table=$('#MSRRes').dataTable
( {
    "bFilter": false,                         
    "bDestroy": true,
    "bJQueryUI": true,
    "ajax" :{
                url: 'getResult.php',
                type: "POST",
                data: {
                    formData:postData,
                    formName:'afscpMsr',
                    action:'advanceSrch'
                }

            },
  "aoColumns": aryJSONColTable
});

});