I want to add an if condition inside the render function of the column of jquery datatables
<script type="text/javascript">
$(document).ready(function () {
$("#wfDefinitionGrid").DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "@Url.Action("GetAllWfDefinitions", "WorkflowDefinition", new { area = "DXAdmin" })",
"type": "POST",
"datatype": "json"
},
"columns": [
{ "data": "Name", "name": "Name" },
{ "data": "Description", "name": "Description" },
{
"render": function (data, type, full, meta) {
return '<div class="action_button">' +
if (@Model.AssnAppRoleModulePermissionModel.Select(s => s.PermissionKey).Contains(EnumHelper.GetDescription(PermissionType.EditWf)))
{
'<img src="@Url.Content("~/images/edit.svg")" title="Edit" onclick="javascript: EditWfDefinition(' + full.WfDefinitionId + ');" />'
} +
'<a href="javascript:void(0)" id="inactiveWorkflowDefinition" onclick="ChangeStatus(' + full.WfDefinitionId + ',' + full.IsObsolete + ')">' +
(full.IsObsolete == false ? '<img src="/images/Inactivate.svg" class="radioImgCls" title="Activate"/>' : '<img src="/images/Active.svg" class="radioImgCls" title="Inactivate" />') +
'</a>' +
'<img src="@Url.Content("~/images/delete.svg")" title="Delete" onclick="javascript: DeleteWfDefinition(' + full.WfDefinitionId + ');" /></div>';
}
}
],
});
});
expected result should be like if the condition is true the whole img tag containing edit.svg will be displayed.
///error: uncaught SyntaxError: unexpected token if
Your problem has nothing to do with datatable rendering a function with an if statement but with your condition being in Razor, the code won't compile inside a javascript condition.
You can try putting your condition in a variable and then compare in your if statement.
var someVariable = "@Model.AssnAppRoleModulePermissionModel.Select(s => s.PermissionKey).Contains(EnumHelper.GetDescription(PermissionType.EditWf))"
if (someVariable == "True"){
//Do something
} else {
//Do something else
}
EDIT:
Keep in mind that this piece of code won't work on a separate Javascript file since it's using Razor to obtain a value. You must always use the script tag in a .cshtml
file.
//for example if you want to add conditions to name Column
var columnDefs : [{ targets: "name",
render : function(data, type, row){
if(data !== 'undefined'){
return data;
}else{
return 'NA';
}
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.