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();

  

 
posted @ 2018-11-08 10:46  y-xs  阅读(5572)  评论(0编辑  收藏  举报