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

时间:2016-01-21 11:24:29

标签: javascript datatables

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

<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>';

请帮助。

2 个答案:

答案 0 :(得分:2)

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

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

将其设置为隐藏在datatables init代码中:

'bVisible': false

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

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

答案 1 :(得分:1)

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

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

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