0 レビュー
1 回答
php-Jqueryを使用してサーバー側のページ付けを取得する
現在、データベースから入力されているテーブルがビュークラスにあります。このデータを入力するために、すべてのデータをフェッチするコントローラークラスがあり、その後、すべてのデータを表示する次のコードをビュークラスに含めています。
<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();
}
これで、このURLhttp://localhost/contacts/fetch_user?length=10&start=10&draw=9
を渡すと、出力は次のようになります。
上部にすべての値がJSONとして表示されており、Processingだけが表示されている実際のテーブルには表示されていません。
わからない
0
レビュー
答え :
解決:
問題を確認したところ、Ajax呼び出しにGETメソッドを使用していることがわかりました。実際、このデータテーブルはデータの処理にPOSTメソッドを使用していました。 POSTリクエストをajaxURLで送信できます。詳細については、データテーブルの手動リンクでサーバー側の処理をチェックアウトしてください: https://datatables.net/manual/server-side
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。