[英]Refreshing a specific div using jquery/ajax in codeigniter
我有一个带有订单的表,我试图每5秒刷新一次表而不刷新整个页面。 经过一些研究,我发现我需要使用此功能:
<script>
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#the_div_you_need_to_refresh').load('path', function(){
setTimeout(refreshTable, 5000);
});
}
问题是我真的不明白我应该在.load('path')中放入什么,因为如果我这样写,它将使我的标头加倍,并且会给我一个错误:
.load('<?php echo base_url('page name')?>',function(){
<--function content-->
})
我的查看页面是这样的:
<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>
我的控制器是这样的:
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');
}
请给我解释一下该怎么做?
这是一个异步请求,用于从其他资源获取数据。 为了每隔5秒加载一次新数据,您需要创建一个页面/控制器,此加载功能会对其造成冲击。
您需要在path
变量中输入此页面/控制器的url。
该控制器应该返回您将使用jquery追加或替换的新数据。
尝试在控制器中创建新操作。 例如。
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);
}
在您的脚本中
<script>
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$("#table_container").empty();
$("#table_container").load("<?php echo base_url('newData')?>", function(){
setTimeout(refreshTable, 5000);
});
}
</script>
我希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.