[英]How to add if condition inside render function of jquery datatable
I want to add an if condition inside the render function of the column of jquery datatables 我想在jquery数据表的列的render函数内添加一个if条件
<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. 如果条件为true,则预期结果应类似于包含edit.svg的整个img标签。
///error: uncaught SyntaxError: unexpected token if
///错误:未捕获的SyntaxError:如果出现意外令牌
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. 您的问题与使用if语句呈现函数的数据表无关,但是您的条件在Razor中,因此代码无法在javascript条件内编译。
You can try putting your condition in a variable and then compare in your if statement. 您可以尝试将条件放入变量中,然后在if语句中进行比较。
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. 请记住,由于这段代码使用Razor获取值,因此无法在单独的Javascript文件上运行。 You must always use the script tag in a
.cshtml
file. 您必须始终在
.cshtml
文件中使用脚本标签。
//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';
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.