1

I have page with data table and i need to refresh only data in table and i use bootstrap datatable to sort but when i use refresh data in my page will can not use bootstrap to sort data. How can i fix it?

this my code for get data to refresh

    $request = $this->m_request->get_request_list();

    $row = '';
    $row .= '<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="datatable">';
    $row .= '<thead>';
    $row .= '<tr>';
    $row .= '<td>Start</td>';
    $row .= '<td>End</td>';
    $row .= '<td>Type</td>';
    $row .= '</tr>';
    $row .= '</thead>';
    $row .= '<tbody>';
    foreach ($re as $r){
        $row .= '<tr id="sh">';
        $row .= '<td>'.$r->date_start.'</td>';
        $row .= '<td>'.$r->date_end.'</td>';
        $row .= '<td>'.$r->leave_type.'</td>';
        $row .= '</tr>';
    }
    $row .= '</tbody>';
    $row .= '</table>';
    echo $row;

and this is my javascript to get data to show

var $container = $("#show_data");
$container.load("<?php echo site_url('request/get')?>");
var refreshId = setInterval(function()
{
    $container.load('<?php echo site_url('request/get')?>');
}, 3000);

if i show table in view page the data will can not refresh to get data and if i call table in controller page i will can not use bootstrap datatable. Please help. Thank you so much for your help.

1 Answer 1

1

Reinitialize you datatable after each ajax success

var $container = $("#show_data");
$container.load("<?php echo site_url('request/get')?>",function(){ $('table').DataTable();});

var refreshId = setInterval(function()
{
    $container.load("<?php echo site_url('request/get')?>",function(){ $('table').DataTable();});
}, 3000);
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, it's impossible if i set auto reload page when have new data and if not have new data not reload page.
why reload the page? ajax is for not reloading the page, you defeat the purpose of ajax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.