datatable自動增加序號


 
        
{
   "targets": [0],
   "visible": true,
   "render": function (data, type, full, meta) {
      var id = full.id;
      if (id) {
         return meta.row + 1+ meta.settings._iDisplayStart;
      } else {
         return '';
      }
   }
},
 
        

  


此方法有點小bug,推薦用下面的方法。
 var table = $('#myTable').DataTable({
          "ajax": "data.txt",
          "pageLength": 5,
          "columnDefs": [{
            "searchable": false,
            "orderable": false,
            "targets": 0  //序號列不能排序也不能搜索
          }],
          "columns": [
             { data:  null},
             { data: 'name'},
             { data: 'birthday'}
          ],
          "order": [[ 1, 'asc' ]]  //默認按姓名列排序
        });
 
        //自動給第一列設置行號
        table.on('order.dt search.dt', function () {
            table.column(0, {search:'applied', order:'applied'}).nodes().each(
               function (cell, i) {
                cell.innerHTML = i+1;
              }
            );
        }).draw();
 
        

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM