Jquery Datatables 如何在渲染函数中获取列 Id

Jquery Datatables How to get column Id in render function

本文关键字:函数 获取 Id Datatables Jquery      更新时间:2023-09-26

我正在尝试创建对行单元格的引用。这是我的代码:

<table class="table table-striped table-bordered table-hover little_margin_table" id="data-table" width="100%">
                        <thead>
                        <th>First Name</th>
                        <th>Email</th>
                        <th>Password</th>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.Items)
                            {
                                <tr id="@item.Id">
                                    <td>@item.FirstName</td>
                                    <td>@item.Email</td>
                                    <td>@item.Password</td>
                                </tr>
                            }
                        </tbody>
                    </table>

Javascript代码:

 $(document).ready(function () {
        $('#data-table').dataTable({
            bFilter: false,
            aoColumnDefs: [
              {
                  bSortable: false,
                  aTargets: [1, 2],
              },
            {
                "targets": 0,
                "render": function (data, type, full, meta) {
                    return '<a href = "@(Url.Action("IndexPage", "Company"))/' + ROWID '</a>';
                }
            },
            ]
            })

在这里,我假设行 Id:

<tr id="@item.Id">

如何让它进入函数渲染:

"render": function (data, type, full, meta) {
                        return '<a href = "@(Url.Action("IndexPage", "Company"))/' + ROWID '</a>';

请帮忙。

您可以在表格中添加额外的列:

<td>@item.FirstName</td>
<td>@item.Email</td>
<td>@item.Password</td>
<td>@item.Id</td>

在数据表中设置为隐藏 init 代码:

'bVisible': false

当您使用 render 时,您现在可以从 full 中获取 Id 值:

"render": function (data, type, full, meta) {
         return '<a href = "@(Url.Action("IndexPage", "Company"))/' + full[3] + '</a>';

您可以使用委托的事件处理程序在单击链接时将id添加到链接中:

$("#data-table").on("click", "td:eq(0) a", function(e) {
   this.href+=$(this).closest('tr').attr('id');
})

并且忘记在render回调中向href添加ROWID。该表是服务器端生成的,您的Model.items永远不会作为数据传递给客户端,因此我看不到任何其他解决方法。