给datatable添加序号列

1、在html中添加一个序号列

序号 项目名称 项目编号

2、js中定义一个null的列

var col = [
    	{"data": null},
        {"data": 'projectName'},
        {"data": 'projectCode'}
]


3、datatable

var myTable = $('#dynamic-table')
        .DataTable({
            "bAutoWidth": true,
            "scrollY": 'auto',
            "aaSorting": [],
            "bFilter": false,
            "bLengthChange": false,
            select: {
                style: 'single'
            },
            "processing": true,
            "serverSide": true,
            ajax: function (data, callback, settings) {
                var params = {
                    //请求参数     
                }
                demo.ajax.postUrl("/summary/getPage.serv",
                    params,
                    function (返回数据) {
                        if () {
                            callback({
                                
                            });
                        }
                        else {
                            
                        }
                        //alert(ret);
                        return;
                    }, true
                )
                //callback(JSON.parse( localStorage.getItem('dataTablesData') ));
            },
            columns: col,

            //字段显示配置及转换
            "columnDefs": [
            	{"searchable": false, "orderable": false, "className": "hidden-xs hidden-sm", "targets": 0},
                {"searchable": true, "orderable": false, "className": "hidden-xs hidden-sm", "targets": 1},
                {"searchable": true, "orderable": false, "className": "hidden-xs hidden-sm", "targets": 2}
            ]            
        });
    //为datatable增加序号
    myTable.on('order.dt search.dt',function(){
    	myTable.column(0,{
    		search: 'applied',
    		order: 'applied'
    	}).nodes().each(function(cell,i){
    		i = i + 1;
    		var page = myTable.page.info();
    		var pageno = page.page;
    		var length = page.length;
    		var columnIndex = (i+pageno*length);
    		cell.innerHTML = columnIndex;
    	});
    }).draw();



你可能感兴趣的:(datatable)