2016-12-27 53 views
0

我們有DataTable中的一個選項,其中我們可以從另一列的值派生列:INITIALISE jQuery的數據表中使用JSON數據和派生列

我當前的代碼:

customerDataTable = $('#datatable_tabletools').dataTable({ 
      "bDeferRender": true, 
      "aaData" : customerData, 
      "aoColumns" : [{"mDataProp":"id"},{"mDataProp":"card_id"},{"mDataProp":"points"},{"mDataProp":"name"},{"mDataProp":"ref"},{"mDataProp":"refs"},{"mDataProp":"address"},{"mDataProp":"mob"},{"mDataProp":"ref_mob"},{"mDataProp":"profile"},{"mDataProp":"vitems"},{"mDataProp":"edit"},{"mDataProp":"del"},{"mDataProp":"bills"},{"mDataProp":"account"}], 
      /*"columnDefs" : [{orderable:false,targets : [0,1,2,3,4,5,6]},{searchable:false,targets:[2,3,4,5,6,7]}],*/ 
      "sDom" : "<'dt-top-row'Tlf>r<'dt-wrapper't><'dt-row dt-bottom-row'<'row'<'col-sm-6'i><'col-sm-6 text-right'p>>", 
      "oTableTools" : { 
       "aButtons" : ["copy", "print", { 
        "sExtends" : "collection", 
        "sButtonText" : 'Save <span class="caret" />', 
        "aButtons" : ["csv", "xls", "pdf"] 
       }], 
       "sSwfPath" : "<?=$baseUrl?>static/js/plugin/datatables/media/swf/copy_csv_xls_pdf.swf" 
      }, 
      "iDisplayLength": 100, 
      "fnInitComplete" : function(oSettings, json) { 
       $(this).closest('#dt_table_tools_wrapper').find('.DTTT.btn-group').addClass('table_tools_group').children('a.btn').each(function() { 
        $(this).addClass('btn-sm btn-default'); 
       }); 
      } 
     }); 

如果我有一個數據列「ID」的JSON,我可以用它在下面的方式來定義列:

"aoColumns" : [{"mDataProp":"id"},{"mDataProp":"<button onclick='editRecord({id})'>Edit</button>"}], 

目前我有操縱JSON添加從ID派生的新領域的編輯,可以這樣做THROU GH dataTable的定義

回答

0

嘗試用這種方式:

"aoColumns" : [ 
     {"mDataProp":"id"}, 
     {"mDataProp":"<button onclick='editRecord({id})'>Edit</button>", 
     "fnRender": function (oObj) { 
      if (oObj.aData[0] == "1") // its containg your Id 
      { return '<button onclick="editRecord({'+oObj.aData[0]+'})">Edit</button>';} 
     } 
    ],