2016-01-21 62 views
2

我試圖創建一個引用到行單元格。 這是我的代碼:Jquery Datatables如何獲取渲染函數中的列ID

<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

你可以添加一個額外的列到表:

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

被設置爲隱藏在數據表初始化代碼:

'bVisible': false 

當您使用render你現在可以得到的ID值來自full

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

你可以使用一個委託事件處理程序id添加到鏈接被點擊時:

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

而忘記在render回調添加ROWIDhref。該表是在serverside中生成的,並且您的Model.items從不作爲數據傳遞給客戶端,所以我看不到任何其他解決方法。