簡體   English   中英

如何在jquery數據表的渲染函數中添加if條件

[英]How to add if condition inside render function of jquery datatable

我想在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:如果出現意外令牌

您的問題與使用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
    }

編輯:

請記住,由於這段代碼使用Razor獲取值,因此無法在單獨的Javascript文件上運行。 您必須始終在.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM