【问题标题】:How to get row index in Datatables?如何获取数据表中的行索引?
【发布时间】:2015-07-29 10:37:45
【问题描述】:

我使用以下 javascript 来获取行索引。通过使用此 ID(表头)始终以 1 开头,以防在每个页面中选择下 10 个值。我需要将下一个 10 显示为 11 到 20。非常感谢..

  $(document).ready(function() {
             $('#example').dataTable( {
                "processing": true,
        "serverSide": true,
                "ajax": "scripts/server_processing.php",

                "aoColumns": [
                    {
                        "sTitle": "ID"},

                    {
                        "sTitle": "E Mail"},
                    {
                        "sTitle": "FirstName"},
                    {
                        "sTitle": "LastName"},
                    {
                        "sTitle": "Company"},
                    {
                        "sTitle": "Course"},
                    {
                        "sTitle": "Module"},
                    {
                        "sTitle": "Completions"},
                    {
                        "sTitle": "First"},
                    {
                        "sTitle": "Last",
                        "sClass": "center"},
                    {
                        "sTitle": "Lowest"},
                    {
                        "sTitle": "Highest",
                        "sClass": "center"},

//                    {
//                        "sTitle": "#"},
                ],

                 "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                     var index = iDisplayIndex +1;
                     $('td:eq(1)',nRow).html(index);
                     return nRow;
                 }


            });


        });

【问题讨论】:

标签: javascript jquery ajax datatables


【解决方案1】:

编辑:

查看此链接Datatables-addrow,代码在这里:

var t = $('#example').DataTable( {
        "columnDefs": [ {
            "searchable": false,
            "orderable": false,
            "targets": 0
        } ],
        "order": [[ 1, 'asc' ]]
    } );

    t.on( 'order.dt search.dt', function () {
        t.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
            cell.innerHTML = i+1;
        } );
    } ).draw();

在 jquery 中获取索引:

$('#example tbody').on( 'click', 'tr', function () {
    alert( 'Row index: '+table.row( this ).index() );
} );

欲了解更多信息,请访问Datatables

【讨论】:

  • 谢谢,但我想在表格中显示行索引列。
  • 正确匹配你的html代码,javascript代码。控制台中的任何错误?
  • @Dim236 它适用于演示中的所有人。您必须重新检查一遍。
猜你喜欢
  • 1970-01-01
  • 2015-08-23
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多