I have a Grid View where i have 2 columns (FOOD and OPERATIONS). In FOOD column I am showing my menu. In OPERATIONS column, I have 2 buttons (DELETE and EDIT).
What I want is if FOOD is "HAMBURGER" I want the two buttons to be visible, otherwise I want to hide them.
Here is the demonstration of what I want
I have tried this code in jQuery. But I don’t think if the condition implementation is correct.
Menu = $('#tblMenu').DataTable({
columns: [
{ "data": "FOOD", responsivePriority: 1, "searchable": true },
{
"data": null,
render: function (data, type, row) {
btn = '<div class="d-flex">';
btn += '<a class="btn btn-info" href="/editMenu?id=' + row.idOrder + '">EDIT</a>';
btn += '<a class="btn btn-danger" id="deleteMenu" href="/deleteMenu?id=' + row.idOrder + '">DELETE</a>';
//Condition
if (data.find("FOOD") != "HAMBURGER") {
/*btn +=*/ $('<a class="btn btn-info" href="/editMenu?id=' + row.idOrder + '">EDIT</a>').hide();
$('<a class="btn btn-danger" href="/deleteMenu?id=' + row.idOrder + '">DELETE</a>').hide();
} else {
$('<a class="btn btn-info" href="/editMenu?id=' + row.idOrder + '">EDIT</a>').show();
$('<a class="btn btn-danger" href="/deleteMenu?id=' + row.idOrder + '">DELETE</a>').show();
}
btn += '</div>';
return btn;
},
}
]
});
//HTML
<table class="table align-middle table-bordered text-center w-100" style="font-size:75%; " id="tblMenu">
<thead class="table-primary">
<tr>
<th class="align-middle">FOOD</th>
<th class="align-middle">OPERATIONS</th>
</tr>
</thead>
</table>
3
Answers
Can you show us more of your code? You are using jQuery here,
Please update more on your question of what you are trying to achieve, you are currently select to all instance of element, not though, use
The question was changed few moments after I finished crafting this demo. So this answer doesn’t perfectly fit the scenario with the datatables.net but yet it gives hints on a strategy you could implement.
In your latest code you are trying to evaluate a condition based on the input data and crafting some elements on the fly to fill the table.
You could yet have a second column containing the buttons on each single row but just show/hide that cell based on the existence of a given class in that row.
Here on document ready I’m looping through all the table rows and if the value
Hamburger
is found on the first cell, to the whole rows we add the classenabled
. Such condition will trigger the css rule styling that cell as visible opposed to hidden.I noticed that DataTables is being used as well as Bootstrap. I assumed that OP has BS4 since DataTables has yet to have a stylesheet for BS5. See Figure I for the list of external files loaded for the example.
Figure I
In the
dataTable()
plugin,columnDefs
parameter andrender: function()
property were used to resolve the problem. See Figure II.Figure II