【发布时间】:2021-12-21 06:25:22
【问题描述】:
目前,我的视图类中有一个从我的数据库中填充的表。为了填充这些数据,我有一个控制器类,它正在获取所有数据,然后,我在视图类中包含以下代码以显示我的所有数据:
<table id="user_data" class="table table-bordered table-striped">
<thead>
<tr>
<th width="35%">First Name</th>
<th width="35%">Last Name</th>
</tr>
</thead>
</table>
<script type="text/javascript" language="javascript" >
$(document).ready(function(){
var dataTable = $('#user_data').DataTable({
"processing":true,
"serverSide":true,
"order":[],
"ajax":{
url:"<?php echo base_url() . 'contacts/fetch_user'; ?>",
type:"GET"
},
"columnDefs":[
{
"targets":[0, 3, 4],
"orderable":false,
},
],
});
});
</script>
控制器类:
function fetch_user(){
$this->load->model("contacts_model");
$fetch_data = $this->contacts_model->make_datatables();
$data = array();
foreach($fetch_data as $row)
{
$sub_array = array();
$sub_array[] = $row->firstname;
$sub_array[] = $row->lastname;
$data[] = $sub_array;
}
$output = array(
"draw" => intval($_GET["draw"]),
"recordsTotal" => $this->contacts_model->get_all_data(),
"recordsFiltered" => $this->contacts_model->get_filtered_data(),
"data" => $data
);
echo json_encode($output);
$this->load->view('crm/contacts/test',$data);
}
模型类:
function make_query()
{
$this->db->select("*");
$this->db->from("crm_contacts");
if(isset($_GET["order"]))
{
$this->db->order_by($this->order_column[$_GET['order']['0']['column']], $_GET['order']['0']['dir']);
}
else
{
$this->db->order_by('id', 'DESC');
}
}
function make_datatables(){
$this->make_query();
if($_GET["length"] != -1)
{
$this->db->limit($_GET['length'], $_GET['start']);
}
$query = $this->db->get();
return $query->result();
}
function get_filtered_data(){
$this->make_query();
$query = $this->db->get();
return $query->num_rows();
}
function get_all_data()
{
$this->db->select("*");
$this->db->from("crm_contacts");
return $this->db->count_all_results();
}
现在每当我传递这个 URL http://localhost/contacts/fetch_user?length=10&start=10&draw=9,我的输出看起来像这样:
它在顶部显示所有值作为 JSON,而不是在我的实际表中,它只显示处理。
【问题讨论】:
-
它按照你写的那样做:
echo json_encode($output);
标签: php jquery ajax codeigniter