2012-02-06 82 views
0
< script type = "text/javascript" > 
    $(function() { 
    var oAllLinksTable = $("#mydatatable").dataTable({ 
     "bProcessing": false, 
     "bServerSide": true, 
     "sAjaxSource": "/myreports/data?Id=" + id, 
     "sPaginationType": "full_numbers", 
     "bDestroy": true 
    }); 
    }); 
< /script> 

我的表如下如何使用jquery在jquery数据表中插入序列号?

<table id="headertagstable" style="width: 100%;" class="grid-table04 margin-b-20"> 
    <thead> 
    <tr> 
     <th width="10%" align="left" valign="middle"> 
     SI No 
     </th> 
     <th width="40%" align="left" class="black-link-first" valign="middle"> 
     Name 
     </th> 
     <th width="25%" align="left" valign="middle"> 
     Date 
     </th> 
     <th width="25%" align="left" valign="middle"> 
     Place 
     </th> 
    </tr> 
    </thead> 
</table> 

所有作品,除了序号罚款。我如何使用jQuery添加序列号?

+0

而不是将更多的代码提供描述以及代码来理解问题 – 2012-02-06 05:43:58

+0

在我如何插入序列号使用jquery我尝试实现“aoColumnDefs”:[{“bSortable”:true,“bSearchable”:true, “fnRender”:function(oObj){//这里如何生成序列号?并使用返回的序列号}“aTargets”:[0] } – 2012-02-06 05:47:27

回答

12

你可以尝试以下

"fnRowCallback" : function(nRow, aData, iDisplayIndex){ 
       $("td:first", nRow).html(iDisplayIndex +1); 
       return nRow; 
      }, 

参考http://datatables.net/forums/discussion/2169/adding-and-deleting-rows-with-row-numbers/p1

另一种解决方案,我只是在计算器本身是如下发现:

var index = iDisplayIndex +1; 
$('td:eq(0)',nRow).html(index); 
return nRow; 

参考Add row number column to jquery datatables

更新d: 仅仅调整了fnRowCallback功能才能正常获得的序列号与paginations

"fnRowCallback" : function(nRow, aData, iDisplayIndex){  
          var oSettings = oAllLinksTable.fnSettings(); 
          $("td:first", nRow).html(oSettings._iDisplayStart+iDisplayIndex +1); 
          return nRow; 
       }, 
+0

嗨,它很好的Hemant Thaks很多.. – 2012-02-06 05:55:39

+0

很高兴它的工作:) – 2012-02-06 05:56:22

+0

嗨hemanth通过使用此序列号总是从1开始,以防万一下一个10值被选择。我需要显示接下来的10到11到20 – 2012-02-16 09:35:25

1

仅仅调整了fnRowCallback功能与paginations正确得到序号

"fnRowCallback" : function(nRow, aData, iDisplayIndex){  
          var oSettings = oAllLinksTable.fnSettings(); 
          $("td:first", nRow).html(oSettings._iDisplayStart+iDisplayIndex +1); 
          return nRow; 
       }, 
1
I've solved using the following code. The code works fine with pagination. 

    var dt = $('#table-id').DataTable({ 
     ..., 
     "createdRow": function (row, data, index) { 
       var info = dt.page.info(); 
       $('td', row).eq(0).html(index + 1 + info.page * info.length); 
      }, 
    });