4

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 :)

1

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.