2014-10-22 37 views
0

我有此使用jQuery数据表由Ajax的数据源填充(阵列)另外在JqueryDatatables行ID的

<script> 
    var dataSet = [ 
       ['Trident','Internet Explorer 4.0','Win 95+','4','X','Win 95+','4','X'], 
       ['Trident','Internet Explorer 5.0','Win 95+','5','C','Win 95+','4','X'], 
       ['Other browsers','All others','-','-','U','Win 95+','4','X'] 
       ]; 

    $(document).ready(function() { 
     var oTable; 


     oTable = $('#applicationsDatatable').dataTable({ 
      "data": dataSet, 
      "sScrollY": "auto", 
      "bJQueryUI": true, 
      "sPaginationType": "full_numbers", 
      "bPaginate": true, 
      "bLengthChange": true, 
      "bFilter": true, 
      "aaSorting": [[ 0, "asc" ]], 
      "aoColumns": [ 
         { "bSortable": false }, 
         null,null,null,null,null,null,null 
      ], 
      "bSort": true, 
      "bInfo": true, 
      "bAutoWidth": true, 
      "bSortCellsTop": true, 
      "sDom": 'tlpi<"clear">' 
     });  
    }); 

</script> 





<table cellpadding="0" cellspacing="0" border="0" class="display normaltable" id="applicationsDatatable" > 
<thead> 
<tr> 
    <th>header1</th> 
    <th>header2</th> 
    <th>header3</th> 
    <th>header4</th>  
    <th>header5</th> 
    <th>header6</th> 
    <th>header7</th> 
    <th>header8</th> 
</tr> 
</thead> 
<tbody> 
<c:forEach items="${appList}" var="item" varStatus="rowIndex"> 
    <tr id="${item.id}"> 
     <td ></td> 
     <td ></td> 
     <td ></td> 
     <td ></td> 
     <td ></td> 
     <td ></td> 
     <td ></td> 
     <td ></td> 
    </tr> 
</c:forEach> 

我想每一行中分配ID,即我分配时创建表时, 但我得到“未定义”试图存取权限的ID时,我就排

$('#applicationsDatatable').delegate("tr", "click", function() {      
     alert ($(this).attr("id"));    
    }); 

回答

0

点击使用创建的行回调可能工作。这样你可以为你的数据表的每一行分配一个id。

"createdRow" : function(nRow, aData, iDisplayIndex) 
{ 
$(nRow).attr('id', 'row' + (iDisplayIndex + 1)); 
} 

如果这样有效,你应该可以像你已经做的那样访问行的id元素。

在这里寻找参考。

https://datatables.net/reference/option/createdRow

https://datatables.net/reference/option/rowCallback

+1

哉!有用 :-) – Baldrick 2014-10-22 10:05:39