简体   繁体   English

在Codeigniter中使用jquery / ajax刷新特定的div

[英]Refreshing a specific div using jquery/ajax in codeigniter

I have a table with orders and i'm trying to refresh the table every 5 seconds without refreshing the whole page. 我有一个带有订单的表,我试图每5秒刷新一次表而不刷新整个页面。 After some research i found that i need to use this function: 经过一些研究,我发现我需要使用此功能:

<script>
$(document).ready(function(){
    refreshTable();
});
function refreshTable(){
    $('#the_div_you_need_to_refresh').load('path', function(){
        setTimeout(refreshTable, 5000);
    });
}

The problem is that i dont' really understand what should i put in .load('path') beacuse if i write it like this,it will double my header and it will give me an error: 问题是我真的不明白我应该在.load('path')中放入什么,因为如果我这样写,它将使我的标头加倍,并且会给我一个错误:

.load('<?php echo base_url('page name')?>',function(){
         <--function content-->
})

My view page is this: 我的查看页面是这样的:

<h2><?= $title;?></h2>
<div id="table_container">
    <table class="table table-striped table-hover" id="orders_table">
        <thead>
        <--content-->
        </thead>
        <tbody>
        <?php foreach($orders as $order):?>
            <tr class="active">
                <--content-->
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>

And my controller is this: 我的控制器是这样的:

public function index(){

            if(!$this->session->userdata('logged_in')){
                redirect('users/login');
            }
            $data['title'] = "Comenzi";
            $data['orders'] = $this->order_model->get_orders();

            $this->load->view('templates/header');
            $this->load->view('orders/comenzi',$data);
            $this->load->view('templates/footer');
        }

Can you explain to me how should i do this,please? 请给我解释一下该怎么做?

This is an asynchronous request which is used to get data from some other resource. 这是一个异步请求,用于从其他资源获取数据。 In order to load fresh data after every 5 sec you need to create a page/controller which would be hit by this load function. 为了每隔5秒加载一次新数据,您需要创建一个页面/控制器,此加载功能会对其造成冲击。

you need to enter the url of this page/controller in the path variable. 您需要在path变量中输入此页面/控制器的url。

That controller is supposed to return the fresh data which you would append or replace using the jquery 该控制器应该返回您将使用jquery追加或替换的新数据。

try to create new action in your controller. 尝试在控制器中创建新操作。 for example. 例如。

public function newData(){

    if(!$this->session->userdata('logged_in')){
        redirect('users/login');
    }
    $data['title'] = "Comenzi";
    $data['orders'] = $this->order_model->get_orders();

    $this->load->view('path_to_your_table_view',$data);
}

in your script 在您的脚本中

<script>
    $(document).ready(function(){
        refreshTable();
    });

    function refreshTable(){
        $("#table_container").empty();
        $("#table_container").load("<?php echo base_url('newData')?>", function(){
            setTimeout(refreshTable, 5000);
        });
    }
</script>

i hope it will help. 我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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