我想在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>';
}
}
],
});
});
预期结果应类似于条件为true时,将显示包含edit.svg的整个img标签。
///错误:未捕获的SyntaxError:如果出现意外令牌
答案 0 :(得分:0)
您的问题与使用if语句呈现函数的数据表无关,但是您的条件在Razor中,因此代码无法在javascript条件下编译。
您可以尝试将条件放入变量中,然后在if语句中进行比较。
var someVariable = "@Model.AssnAppRoleModulePermissionModel.Select(s => s.PermissionKey).Contains(EnumHelper.GetDescription(PermissionType.EditWf))"
if (someVariable == "True"){
//Do something
} else {
//Do something else
}
编辑:
请记住,这段代码无法在单独的Javascript文件上运行,因为它使用Razor获取值。您必须始终在.cshtml
文件中使用脚本标签。
答案 1 :(得分:0)
//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';
}
]