How to use "if condition" correctly in "columns"[]
How to use "if condition" correctly in "columns"[]

Link to test case:
https://jsfiddle.net/mLr7p5js/3
Debugger code (debug.datatables.net):
15 tests complete. No failures or warnings found!
If you are having problems with your DataTables, please upload a data profile using the Upload option below, and post a support request in DataTables forums, with a link to a page showing the issue so we can help to debug and investigate the issue.
Error messages shown:
DataTables warning: table id=example - Requested unknown parameter 'quantity' for row 3, column 2. For more information about this error, please see http://datatables.net/tn/4
Description of problem:
I'm using Datatable in my project and i would like to use "if condition" to return a button based on condition results. The example below is what i would like to use. if "status" is equal "yes" and "quantity" is more than zero, return "view button" as code below:
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"columns": [
{ "data": "id" },
{ "data": "date" },
{ "data": "quantity" },
{ "data": "status",
render: function(data, type, row) {
if(data == 'yes' && data.quantity >0) {
return '<button>View</button>';
}else if (data == 'no' && data.quantity == 0) {
return '<button>Delete</button>';
}
}
}
],
});
});
Based on code above, when i try to use this logical, i got a message from Datatable as below:
DataTables warning: table id=example - Requested unknown parameter 'quantity' for row 3, column 2. For more information about this error, please see http://datatables.net/tn/4
In this case, how can i do to improve this (if condition) as code above? Thanks a lot
This question has accepted answers - jump to:
Answers
See the usage for
columns.render
.data
only refers to the current cell being processed. To access other cells in that row, use the third parameter,row
,Colin
In addition to Colin's comment you need to always return something. In between lines 23 and 24 you need to return something. Could be the column data with
return data;
or a blank cell withreturn "";
.Kevin
Thanks for the feedback. You guys helped me a lot.
According to your guidance, i found a question that also helps me too https://datatables.net/forums/discussion/45381/if-and-else-condition-with-data-tables
I added some options from columns.render and applied "row parameter" inside render function. I replace "data == "yes" to "row.status == "yes" and works fine.
Edited code:
Thanks @colin and @kthorngren .