简体   繁体   中英

reload datatable by ajax and json

I'm using datatable plugin and want to refresh table by ajax. I've read here to make my ajax return shows in table but it doesn't work. Here is my html code:

<button type="button" onclick="rld()">test</button>
<table id="sample_1">
       <thead>
          <tr>
             <th> 1 </th>
             <th> 2 </th>
             <th> 3 </th>
          </tr>
       </thead>
</table>

My java function:

function rld(){ 
    var table = $('#sample_1').DataTable( {
        ajax: '<?php echo site_url('admin/test'); ?>',
        deferRender: true,
        columns: [
            { data: '1' },
            { data: '2' },
            { data: '3' }
        ],
        rowId: 'extn',
        select: true,
        dom: 'Bfrtip',
        buttons: [
            {
                text: 'Reload table',
                action: function () {
                    table.ajax.reload();
                }
            }
        ]
    } );
}

I can't get what the problem is. there in no alerts but in the console i get TypeError: f is undefined and TypeError: c is undefined Which i don't know why cause my json return is correct (checked in [JSONLint][3]). Thanks in advance.

Thank you all for your attention :) Found that i didn't call where the data goes when returned as json and the json field's name isn't equal to the column data field name. So make this adjustment:

function reload_table(){
var table = $('#sample_1').DataTable();
    table.destroy();
    var table = $('#sample_1').DataTable( {
        "processing": true,
        "dataType": 'json',
        ajax: '<?php echo site_url('admin/relaod_table'); ?>',
        deferRender: true,
        columns: [
            { data: 'user_firstname' },
            { data: 'user_lastname' },
            { data: 'user_status' },
            { data: 'user_created_date' },
        ],
        dom: 'Bfrtip',
        buttons: [
            {
                text: 'Reload table',
                action: function () {
                    table.ajax.reload();
                }
            }
        ]
    } );
}

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