在Jquery Datatables中添加行ID

时间:2014-10-22 09:41:54

标签: javascript jquery database

我使用由Ajax数据源(数组)填充的jQuery来获取此数据表

<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"));             
    } );

1 个答案:

答案 0 :(得分: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