如何将data-id添加到Datatable单元格

时间:2017-11-25 10:39:50

标签: javascript jquery ajax datatables

我需要将data-id属性添加到AJAX Datatable的“action”列,数据id的值应该是从服务器检索的ID

我仍然得到数据属性的未定义值...我做错了什么? 非常感谢您的帮助。

var table = $('#table').DataTable({ 
    "processing": true, //Feature control the processing indicator.
    "serverSide": true, //Feature control DataTables' server-side processing mode.
    "responsive": true,
    "autoWidth": false, // Make Datatable really responsive
    "order": [[ 0, "asc" ]], //Initial order for registration column.
    "stateSave": true, // Save user state
    "lengthMenu": [[15, 25, 50, -1], [15, 25, 50, "All"]],
    "responsive": true,
    "searchHighlight": true,

    // Load data for the table's content from an Ajax source
    "ajax": {
        "url": url + '/ajax_list',
        "dataType": "json",
        "type": "POST"
    },
    "columns": [
                  { "data": "id" },
                  { "data": "contributer" },
                  { "data": "images" },
                  { "data": "created_at" },
                  { "data": "action" },
               ] ,    
    //Set column definition initialisation properties.
    "columnDefs": [
    { 
        "targets": [ 4 ], //fifth column.
        "orderable": false, //set not orderable
        "searchable": false, //set not searchable

        "createdRow": function ( row, data, index ) {
            $( row ).find('td:eq(4)').attr('data-id', '3');
        }
    }],
    dom: "Bfrtip",
    buttons: [{
        extend: "copy",
        className: "btn-sm"
    }, {
        extend: "csv",
        className: "btn-sm"
    }, {
        extend: "excel",
        className: "btn-sm"
    }, {
        extend: "pdf",
        className: "btn-sm"
    }, {
        extend: "print",
        className: "btn-sm"
    }],
    "language": {
        "processing": "Loading. Please wait ..."
    },
});      

0 个答案:

没有答案