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

My table as follows

 <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>

All works fine except serial number. How can I add serial number using jquery ?

2
  • rather than inserting more code provide description along with code to understand the problem Commented Feb 6, 2012 at 5:43
  • in my how can i insert serial number using jquery i try to implement "aoColumnDefs": [{"bSortable": true, "bSearchable": true, "fnRender": function (oObj) {//here how can i generate serial number? and use return the serial number }"aTargets": [0] } Commented Feb 6, 2012 at 5:47

11 Answers 11

25

you can try following

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

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

another solution i just found on stackoverflow itself is as follow:

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

refer Add row number column to jquery datatables

Updated : Just tweak the fnRowCallback function to get serial numbers correctly with paginations

    "fnRowCallback" : function(nRow, aData, iDisplayIndex){      
                          var oSettings = oAllLinksTable.fnSettings();
                           $("td:first", nRow).html(oSettings._iDisplayStart+iDisplayIndex +1);
                           return nRow;
                },
Sign up to request clarification or add additional context in comments.

2 Comments

hi hemanth by using this the serial no always starts with 1 in case next 10 values are selected .i need to show next 10 as 11 to 20
does not export serialno column rendered with rowcallback function.
10

just add the following code

"columns": [
        {
        "title": "Serial",
        render: function (data, type, row, meta) {
        return meta.row + meta.settings._iDisplayStart + 1;
        }
        }
       ],

2 Comments

The code has an issue with pagination. When exporting with CSV from the next page, it gives all rows, but the starting SR NO is not 1; it is the value of SR NO on that page. The code seems buggy.
Awesome solution dear, worked well with search and pagination in DataTable 2.x version, thanks :)
9

Here is the simple answer. Use datatable render method.

Example :

var i = 1;

$("#myTable1").dataTable().fnDestroy();


$('#myTable1').DataTable({

  ajax: base_url + 'specific_function',

  columns: [

    {
      "render": function(data, type, full, meta) {
        return i++;
      }
    },
    {
      "data": "col_2_data"
    },
    {
      "data": "col_3_data"
    },
    {
      "render": function(data, type, full, meta) {
        return '<button class="btn btn-success btn-sm" onclick="editBUT(' + full.id + ')">EDIT</button>';
      }
    }
  ]
});

2 Comments

Done .Thank you
Not worked for me, when we use pagination from 1 to 2 or 2 to 1, it always increase the showing record's serial number. This is not correct solution. Generating another issue.
7

I have solved using following code. The code works fine for me.

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

Use this.fnSettings (); instead of oAllLinksTable.fnSettings(); will solve the issue.

refer https://datatables.net/forums/discussion/5316/strange-behavior-of-fnsettings

1 Comment

This works with pagination... Thankx bro... but while you click sort ascending or descending, its not changing like 10,9,8,7,....
4

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

Comments

3

Just tweak the fnRowCallback function to get serial numbers correctly with paginations

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

Comments

3
$('#users').DataTable({
    processing: true,
    serverSide: true,
    ajax: "",
    columns: [{
          data: id,
          render: function (data, type, row, meta) {
               return meta.row + meta.settings._iDisplayStart + 1;
          }
    }]
});

1 Comment

render used with columnDefs not with columns
1

when we use Server Side Rendering, One of the Easy & Best Way for Display Auto Increment Sr No. instead of table row id... I used "Laravel Yajra DataTable"

simply use return meta.row + 1; .

see Below Example Code

columns: [
    {data: 'SrNo',
       render: function (data, type, row, meta) {
            return meta.row + 1;
       }
    },
    .... //your Code(Other Columns)
 ]

Or You can also Use like this

columns: [
        {data: 'SrNo',
           render: function (data, type, row, meta) {
                return meta.row + meta.settings._iDisplayStart + 1;
           }
        },
        .... //your Code(Other Columns)
     ]

Comments

0

The continuation of serial number breaks when next pagination is clicked, using iDisplayIndex the following code:

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

instead you can use aData for displaying continuation of serial number:

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

Comments

0
"fnDrawCallback": function(oSettings) {
      var count=0;
      $('#myTable').find('tr').each(function() {
        $(this).find('td').eq(0).before('<td>' + count + '</td>');
        count++;
      });
    }

1 Comment

Code-only answers may solve the problem but they are much more useful if you explain how they solve it. Community requires theory as well as code both to understand your answer fully.
0

Here is an another new solution which works with Datatable 1.10.

Briefly discussed in here: https://datatables.net/examples/api/counter_columns.html

$(document).ready(function() {
    var t = $('#example').DataTable( {
        "columnDefs": [ {
            "searchable": false,
            "orderable": false,
            "targets": 0
        } ],
        "order": [[ 1, 'asc' ]]
    } );
                
    t.on( 'draw.dt', function () {
    var PageInfo = $('#example').DataTable().page.info();
         t.column(0, { page: 'current' }).nodes().each( function (cell, i) {
            cell.innerHTML = i + 1 + PageInfo.start;
        } );
    } );
} );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.