【发布时间】:2019-08-15 08:49:09
【问题描述】:
我根据活动-非活动状态从用户表中获取数据,之后应该使用数据表显示 foreach 循环数据,但出现错误
DataTables 警告:表 id=users_list - JSON 响应无效
所以我的要求是
1) Show all data in the data table
2) per page 5 records
3) Default search box of a data table
4) Pagination of a data table
以下是我的代码,但这对我不起作用 在 index.php 文件下拉列表中,基于 onchange 事件 ajax 调用 ajax 控制器中的 users_list 方法。
In index.php file drop-down list with ajax call request
<select class="custom-select" onchange="get_users(this.value)">
<option value="active">Active</option>
<option value="inactive">Inactive</option>
</select>
<div class="show_data"></div>
<script>
function get_users(status){
$.ajax({
type:'POST',
url:'<?php echo SITE_URL . 'ajax/users_list'; ?>',
data:{ status:status},
success: function(data)
{
$(".show_data").html(data);
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Failed');
}
});
}
</script>
在控制器 Ajax.php 中
function users_list(){
$status=$this->input->post('status'); //active or inactive
$data['users']=$this->db->where('status',$status)->get('users')->result();
$this->load->view('view_assets',$data);
}
在 view_assets.php 文件中
<table id="display_userinfo">
<thead>
<tr class="bg-light">
<th>user id</th>
<th>user name</th>
<th>Mobile no</th>
</tr>
</thead>
<?php foreach ($users as $user) { ?>
<tbody>
<tr>
<td><?= $user->id ?></td>
<td><?= $user->name ?></td>
<td><?= $user->mobile ?></td>
</tr>
<?php } ?>
</tbody>
<script>
$('#display_userinfo').DataTable({
"processing": true,
"serverSide": true,
});
</script>
【问题讨论】: