boycod3 boycod3 - 1 year ago 60
jQuery Question

Jquery Datatable null column

Hi i have created the jquery datatable using below code..

The column email id and activated date is may be nullable. When i render the table it is showing 'Request unknown parameter 'email' from data source for row 0'

var tblAllKeys = $('#tblAllKeys').dataTable({
"bDestroy" : true,
"bProcessing" : true,
"bServerSide" : true,
"bLenthChange" : false,
"iDisplayLength" : 10,
"sAjaxSource" : "loadAllKeys",
"oLanguage" : {
"sSearch" : "Search By Activation Key:"
},
"aoColumns" : [
{"sTitle" : "No.","mData" : null,"aTargets": [ 0 ],
"fnRender" : function(obj) {
var columnIndex = obj.oSettings._iDisplayStart + obj.iDataRow+1
return columnIndex;
}
},
{"sTitle" : "Activation Key","mData" : "key", "bSearchable" : true},
{"sTitle" : "Email ID","mData" : "email" , "bSearchable" : false},
{"sTitle" : "App Edition","mData" : "edition", "bSearchable" : false},
{"sTitle" : "Batch Code","mData" : "batch", "bSearchable" : false},
{"sTitle" : "Activated Date","mData" : "aDate" , "bSearchable" : false},
{"sTitle" : "Generated Date","mData" : "gDate", "bSearchable" : false},
{"sTitle" : "Status","mData" : "status", "bSearchable" : false},
],
"fnServerData" : function(sSource, aoData, fnCallback) {
$.ajax({
"dataType" : 'json',
"type" : "GET",
"url" : sSource,
"data" : aoData,
"success" : fnCallback
});
},
"sPaginationType" : "full_numbers",
});

Answer Source

You can make use of columns.defaultContent option available as per their docs on this error, to display default value or empty string when column's doesn't have any value.

"aoColumns": [{
  "sTitle": "No.",
  "mData": null,
  "aTargets": [0],
  "fnRender": function(obj) {
    var columnIndex = obj.oSettings._iDisplayStart + obj.iDataRow + 1
    return columnIndex;
  }
}, {
  "sTitle": "Activation Key",
  "mData": "key",
  "bSearchable": true
}, {
  "sTitle": "Email ID",
  "defaultContent":"",//or specify any other value
  "mData": "email",
  "bSearchable": false
}, {
  "sTitle": "App Edition",
  "mData": "edition",
  "bSearchable": false
}, {
  "sTitle": "Batch Code",
  "mData": "batch",
  "bSearchable": false
}, {
  "sTitle": "Activated Date",
  "mData": "aDate",
  "bSearchable": false
}, {
  "sTitle": "Generated Date",
  "mData": "gDate",
  "bSearchable": false
}, {
  "sTitle": "Status",
  "mData": "status",
  "bSearchable": false
}, ],

Its good to assign defaultContent option to all the columns if you are sure that any of the column might come empty.