DataTables:createdCell不适用于页面加载(工作后)

时间:2016-11-18 11:12:22

标签: datatables datatables-1.10

我使用数据表,我想在列中显示一个图标(基于列数据中的值)。 我写了这样的代码:

$(document).ready(function() {

var oTable = $("#initiativeTable").DataTable({
    "serverSide":true,
    "processing":true,
    "searching": false,
    "ordering": true,
    "order": [[ 0, "desc" ]],
    "ajax": '${basePath}/cns/initiative/table.json?teams=${teams}&fields=${fields}&search=${search}',
    "columns": [
                { "data": "id_init" },{ "data": "parent_name_init" },{ "data": "name_cust" },{ "data": "name_init" },{ "data": "code_paf" },{ "data": "name_team" },{ "data": "pa_name_emp" },{ "data": "pgm_name_emp" },{ "data": "pm_name_emp" },{ "data": "id_initstt" },{ "data": "description_contrtype" },{ "data": "description_inittype" },{ "data": "is_revised_init" }] ,
                "columnDefs": [{    "targets": 12,
                       "createdCell": function (td, cellData, rowData, row, col) {
                           if ( cellData == 'false' ) {
                               $(td).html("");
                             }else{
                              $(td).html("<span class='glyphicon glyphicon-ok'></span>");
                             }                               
                       }
                     }
}); 
});

DataTables工作正常,但列显示数据的值,而不是图标。如果我重新排序表或进入第二页分页,createdCell会工作并显示图标。 它不仅仅适用于页面的第一次加载。 有什么问题?

提前致谢

干杯

利玛

1 个答案:

答案 0 :(得分:0)

createdCell的语法是Inside columns,请检查columns.createCell

columns: [

          {
            data : "name_data",
            createCell: function(){
                //code
            }
          } 

]

您可以使用此代码:

{ 
  "data": "is_revised_init",
  "createdCell": function (td, cellData, rowData, row, col) {
                       if ( cellData == 'false' ) {
                           $(td).html("");
                         }else{
                          $(td).html("<span class='glyphicon glyphicon-ok'></span>");
                         }                               
                   }

}