I am trying to output an if condition on my two columns in my laravel ajax:
$.each(response.all_categories, function (key, item) {
$('tbody').append('<tr>\
<td><p class="font-weight-bold mb-0">'+item.category_name+'</p>'+item.category_description+'</td>\
<td>View:\
if ('+item.config_view_type+' == 'P'){\
<mark class="mark-orange">Public</mark>\
} elseif ('+item.config_view_type+' == 'R'){\
<mark class="mark-orange">Restricted</mark>}\
<br>Edit:\
</td>\
<td>View:\
</td>\
<td>\
<button type="button" value="'+item.category_id+'" class="btn btn-outline-secondary">\
<i class="fas fa-edit"></i> Edit</button><button type="button" value="'+item.category_id+'" class="btn btn-outline-secondary">\
<i class="fas fa-trash"></i> Delete</button>\
</td>\
</tr>');
});
my if condition about does not show my table columns (no output). Disclaimer: the table is already connected, columns are shown without issue if I take out the if condition.
I also tried:
<td>View:\
@if ('+item.config_view_type+' == 'P')\
<mark class="mark-orange">Public</mark>\
@elseif ('+item.config_edit_type+' == 'R')\
<mark class="mark-orange">Restricted</mark>\
@endif\
<br>Edit:\
</td>\
what is the correct if syntax for this? thanks for any help
Controller:
public function fetchcategory(){
$all_categories = HmsBbrCategory::all();
return response()->json([
'all_categories'=>$all_categories,
]);
}
You can use backtick (`) instead of quotes
$.each(response.all_categories, function (key, item) {
$('tbody').append(`
<tr>
<td><p class="font-weight-bold mb-0">${item.category_name}</p>${item.category_description}</td>
<td>View:
<mark class="mark-orange">${getmark(item.config_view_type)}</mark>
<br>Edit:
</td>
<td>View:
</td>
<td>
<button type="button" value="${item.category_id}" class="btn btn-outline-secondary">
<i class="fas fa-edit"></i> Edit</button><button type="button" value="${item.category_id}" class="btn btn-outline-secondary">
<i class="fas fa-trash"></i> Delete</button>
</td>
</tr>`);
});
function getmark(type) {
var mark = '';
if (type == 'P'){
mark = 'Public'
} else if (type == 'R'){
mark = 'Restricted'
}
return mark;
}