【问题标题】:Hiding the Edit and delete button in a DataTable based on the identity value根据标识值隐藏 DataTable 中的编辑和删除按钮
【发布时间】:2020-09-20 00:40:06
【问题描述】:

我有以下数据表。我想根据控制器返回的标识值隐藏按钮编辑和删除。

<script>
    var Popup, dataTable;

    $(document).ready(function () {
        dataTable =  $("#BookAssignmentTable").DataTable({
            "ajax": {
                "url": "/bookAssign/GetData",
                "type": "GET",
                "datatype": "json"
            },
            "columns": [
                { "data": "Book" },
                { "data": "Office" },
                { "data": "Group", "width":"100px"},
                {
                    "data": "ID",
                    "render": function (data) {
                        return "<a class='btn btn-success btn-sm' onclick=PopupForm('@Url.Action("StoreOrEdit", "BookAssign")/" + data + "')><i class='fa fa-pencil'></i>Edit</a> <a class='btn btn-danger btn-sm' style='margin-left:5px' onclick=Delete(" + data + ")><i class='fa fa-trash'></i>Delete</a>";
                    },
                    "orderable": false,
                    "searchable": false,
                    "width": "150px",
                    "Visible":"false"
                }
            ],
            "language": {
                "emptyTable" : "No data found please click on <b>Add New </b> Button"
            }
        });
    });
</script>

下面是我的控制器:

public ActionResult Index() {
    var id = new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).Identity.Name;
    return View();
}

if (id== "test1")

然后我想隐藏编辑和删除按钮。是否可以在 DataTable 中做到这一点。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript jquery datatable datatables


    【解决方案1】:

    简单,修改以下代码:

    "data": "ID", "render": function (data) {
    
                                return "<a class='btn btn-success btn-sm' onclick=PopupForm('@Url.Action("StoreOrEdit", "BookAssign")/" + data + "')><i class='fa fa-pencil'></i>Edit</a> <a class='btn btn-danger btn-sm' style='margin-left:5px' onclick=Delete(" + data + ")><i class='fa fa-trash'></i>Delete</a>";
                            },
    

    这样的:

    "data": "ID", "render": function(data) {
    
        if (data == "test") {
            return "";
        } else {    
            return "<a class='btn btn-success btn-sm' onclick=PopupForm('@Url.Action("StoreOrEdit", "BookAssign")/" + data + "')><i class='fa fa-pencil'></i>Edit</a> <a class='btn btn-danger btn-sm' style='margin-left:5px' onclick=Delete(" + data + ")><i class='fa fa-trash'></i>Delete</a>";
        }
    },
    

    【讨论】:

    • 一个小细节。只需在测试值末尾添加 1,因为 OP 发送的是 test1 而不仅仅是 test。顺便说一句,答案很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多