【问题标题】:Show/hide jQuery datatable action link button based upon column value根据列值显示/隐藏 jQuery 数据表 actionlink 按钮
【发布时间】:2021-11-16 18:14:48
【问题描述】:

我用于检索数据的 LINQ 查询:

public ActionResult GetRegFirmList()
        {
            try
            {
                var data = (from z in db.BusinessModels
                            select z).OrderByDescending(z => z.BusinessKey).ToList();
                return View(data);
            }
            catch (Exception ex) { throw; }
        }

我在 jQuery 数据表中有两个操作链接按钮。如果特定行中Status 的值是1,我需要隐藏Certificate 的按钮。 Application 的操作链接按钮不应隐藏。我该怎么做?

<link href="~/Content/DataTables/css/dataTables.bootstrap.min.css" rel="stylesheet" />
<table id="tblBusinessData">
    <thead class="table-success">
        <tr>
            <th>generate</th>
            <th style="visibility:hidden;">
                @Html.DisplayNameFor(model => model.BusinessKey)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.BusinessName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PropName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Status)
            </th>
            
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.ActionLink("Certificate", "GenerateCertificate", new { id = item.BusinessKey }, new { @class = "btn btn-warning" })
                    @Html.ActionLink("Application", "GenerateApplication", new { id = item.BusinessKey }, new { @class = "btn btn-warning" })
                </td>
                <td style="visibility:hidden;">
                    @Html.DisplayFor(modelItem => item.BusinessKey)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.BusinessName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.PropName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Status)
                </td>
            </tr>
        }
    </tbody>
</table>
<script src="~/Scripts/DataTables/jquery.dataTables.min.js"></script>
<script src="~/Scripts/DataTables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript">
    $('#tblBusinessData').DataTable({
        "columnDefs": [{
            "targets": [1],
            "visible": false
        }],
        "order": [
            [1, "desc"]
        ]
    });
</script>

【问题讨论】:

    标签: c# html jquery razor datatables


    【解决方案1】:

    你可以像这样添加条件

    @if(item.Status!=1)  
    {
        @Html.ActionLink("Certificate", "GenerateCertificate", new { id = item.BusinessKey }, new { @class = "btn btn-warning" })
    }
    

    所以你的 foreach 循环应该如下所示

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @(item.Status!=1){@Html.ActionLink("Certificate", "GenerateCertificate", new { id = item.BusinessKey }, new { @class = "btn btn-warning" })}
                @Html.ActionLink("Application", "GenerateApplication", new { id = item.BusinessKey }, new { @class = "btn btn-warning" })
            </td>
            <td style="visibility:hidden;">
                @Html.DisplayFor(modelItem => item.BusinessKey)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.BusinessName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PropName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Status)
            </td>
        </tr>
    }
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 2014-05-12
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多