简体   繁体   中英

DataTable - Getting error when putting select checkbox in first column

I know it has been addressed already, but I'm just not getting it to work. I'm using DataTable Editor with resposnive and serverside. I'm getting an error when I put the checkbox in the first column like:

js:

var table = $('#mytable').DataTable( {
    dom: "rt",
    ajax: {
        url: "/source.php",
        type: "POST",
        data: function (d) { 

        }
    },
    serverSide: true,
    processing: true,
    select: {
        style:    'os',
        selector: 'td:first-child'
    },              
    columns: [
        {
            data: null,
            defaultContent: "",
            className: "select-checkbox",
            orderable: false, 
            targets:   0            
        },                                        
        { data: "logo" },
        { data: "name" },   
        { data: "product" }                                                
    ]                            
} ); 

That's the Error message:

DataTables warning: table id=mytable - Unknown field: (index 0)

php:

Editor::inst( $db, 'table' )
    ->fields(
        Field::inst( 'logo' ),
        Field::inst( 'url' ),       
        Field::inst( 'name' ),
        Field::inst( 'product' )              
    )

... wenn putting in the last column it works:

    ...
    columns: [                                        
        { data: "logo" },
        { data: "name" },   
        { data: "product" },
        {
            data: null,
            defaultContent: "",
            className: "select-checkbox",
            orderable: false, 
            targets:   0            
        }                                                              
    ]   
    ...

How can I get the checkbox in the first column? (So column 0)

add this in code

 $("#table").DataTable({
            'columnDefs': [{
                    'targets': 0,
                    'bSortable': false,
                    'render': function (data, type, full, meta){
                            return '<input type="checkbox"> <label>Checkbox</label>';
                    }
            }]
 })

Here the answer from the author:

You have server-side processing enabled, and the default ordering is to order on the first column. When that happens, DataTables is telling the server to order on the client-side generated column and throws an error.

Use ``order: [[1, 'desc']]` to resolve that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM