2016-11-07 30 views
2

我想在所有的數據表我都空單元格定義默認值,但我不希望像每列做到這一點:數據表的默認渲染功能空單元

$('#example').dataTable({ 
    "ajaxSource": "sources/deep.txt", 
    "columns": [ 
    { "data": "engine" 
     "render": function (data, type, row) { 
      if(row.platform == null) { 
      return "-"; 
      } else { 
      return row.platform.display; 
      }     
     }, 
    },   
    { "data": "browser", 
     "render": function (data, type, row) { 
      if(row.platform == null) { 
      return "-"; 
      } else { 
      return row.platform.display; 
      }     
     }, 
    }, 
    { 
     "data": "platform", 
     "render": function (data, type, row) { 
      if(row.platform == null) { 
      return "-"; 
      } else { 
      return row.platform.display; 
      }     
     }, 
    } 
    ] 
}); 

有一個機會來定義這個數據表默認值?我應該怎麼做?或者有更好的地方爲空單元定義這個渲染默認動作?

/* default values for tables */ 
$.extend(true, $.fn.dataTable.defaults, { 
    dom: "<'table_scroll_container't>" + "<'dt-row dt-bottom-row'<'col-md-4 m-b-10 h30 no-margin-md text-center text-md-left'B i><'col-md-8 m-b-10 h30 no-margin-md text-center text-md-right'l<'clearfix visible-xs-block visible-sm-block'>p>>", 
    buttons: [], 
    stateSave: true, 
    stateDuration: -1, 
    iDisplayLength: 50, 
    autoWidth: false, 
    processing: true, 
    language: { 
     "processing": "Processing...", 
     "info": "Showing _TOTAL_ entries", 
     "infoEmpty": "No records available", 
     "infoFiltered": "filtered from _MAX_" 
    } 
}); 

所有其他提示,歡迎:)

+2

你在這裏:https://datatables.net/reference/option/columns.defaultContent –

回答

3

使用columns.defaultContent設置爲列默認內容。

您也可以用它來設置所有表的默認值。例如:

$.extend(true, $.fn.dataTable.defaults, { 
    columnDefs: [ 
     { 
     targets: '_all', 
     defaultContent: '-' 
     } 
    ] 
}); 

請參閱this example的代碼和演示。