Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datatables default render function for empty cells

I want to define default value for all empty cells in all datatables i have, but i don't want to do this for every column like that:

$('#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;
          }                 
      },
    }
  ]
} );

Is there a chance to define this in datatable defaults? How should i do that? Or there is a better place to define this render default action for empty cells?

/* 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_"
    }
});

All other tips are welcome :)

like image 714
lucaste Avatar asked Jan 06 '23 02:01

lucaste


1 Answers

Use columns.defaultContent to set default content for a column.

You can also use it to set default values for all tables. For example:

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

See this example for code and demonstration.

like image 115
Gyrocode.com Avatar answered Jan 11 '23 12:01

Gyrocode.com