I am not sure whether the question is asked before. but i couldnt find it. Thats the reason i am asking now.
I have to use more datatables. so i am writing a common function to call it again and again by passing parameters. For that common code, i have to use inline if statement to add column widths, here i attached my code. someone help me to insert the inline condition inside it.
function common_datatable(file_ajax, module_name, btns_list, widths, view_btn) {
return $('#'+module_name+'_table').DataTable({
"processing": true,
"serverSide": true,
"ajax": file_ajax,
"bLengthChange": false, "bAutoWidth": false , "sScrollX": "100%",
aoColumns : [
(widths[0] !=0) ? '{ "sWidth": "1%" }' : '',
{ "sWidth": "30%" },
{ "sWidth": "30%"},
{ "sWidth": "9%"}
], orderCellsTop: true,
"scrollX": true,
"order": [ [1, "asc"] ],
"columns": [{"orderable": false}, null, null, null, null, null ]
});
}
The above code i passed array of width's through the width parameter, this is the code i am using,
(width[0] !=0) ? '{ "sWidth": "1%" }' : '',
but its not working inside it.
It is not possible to initialize an array with conditional element as you are trying. Try like following
function common_datatable(file_ajax, module_name, btns_list, widths, view_btn) {
var aoColumns = [
{ "sWidth": "30%" },
{ "sWidth": "30%"},
{ "sWidth": "9%"}
];
if(widths[0] != 0) {
aoColumns.unshift({"sWidth": "1%"});
}
return $('#'+module_name+'_table').DataTable({
"processing": true,
"serverSide": true,
"ajax": file_ajax,
"bLengthChange": false, "bAutoWidth": false , "sScrollX": "100%",
"aoColumns": aoColumns,
"orderCellsTop": true,
"scrollX": true,
"order": [ [1, "asc"] ],
"columns": [{"orderable": false}, null, null, null, null, null ]
});
}