2016-05-23 11 views
1

データテーブルにjsonデータフォーマットを使用してデータを表示したいが、テーブルにデータがない。 JSONを使用してデータテーブルにデータを表示するのに役立つ人はいますか?jsonをデータテーブルcodeigniterに使用してデータを表示

<table class="table table-striped table-hover table-bordered" id="allcourses"> 
    <thead> 
     <tr role="row"> 
      <th> 
       Course Name 
      </th> 
      <th> 
       Indian Price 
      </th> 
      <th> 
       U S Price 
      </th> 
      <th> 
       Course Duration 
      </th> 
      <th> 
       Discount 
      </th> 
      <th> 
       <center>Pick Course</center> 
      </th> 
     </tr> 
    </thead> 
</table> 

これは私のスクリプトです::これは私の見解である

public function data_courses(){ 
    $result=$this->Users_model->get_active_courses(); 
    echo json_encode($result); 
} 
public function courses_list(){ 
    $this->authentication(); 
    $this->load->view('courses'); 
} 

この

は私のコントローラであり、これは私のモデルである

$(document).ready(function() { 
    $('#allcourses').dataTable({ 
     "aLengthMenu": [ 
       [5, 15, 20, 100, -1], 
       [5, 15, 20, 100, "All"] 
      ], 
"ajax": "Student/data_courses", 
     "aoColumns": [ 
      {"data": "course_name"}, 
      {"data": "price_INR"}, 
      {"data": "price_USD"}, 
      {"data": "no_of_hours"}, 
      {"data": "discount"}, 
      {"data": "status"}, 
      { "bSortable": false } 
     ] 
    }); 
}); 

public function get_active_courses(){ 
    $this->db->select('*'); 
    $this->db->from('courses'); 
    $this->db->where('status',1); 
    $query=$this->db->get(); 
    return $query->result();  
    } 

私のJSONデータ

{"data":[{"course_id":"1","course_name":" PHP","price_INR":"25,000","price_USD":"750","no_of_hours":"86","date":"2016-05-19 17:58:47","status":"1","discount":"5.89"},{"course_id":"5","course_name":"Java","price_INR":"15,000","price_USD":"650","no_of_hours":"25","date":"2016-05-14 07:59:24","status":"1","discount":"2.50"},{"course_id":"6","course_name":"Dot net","price_INR":"23,000","price_USD":"250","no_of_hours":"36","date":"2016-05-14 12:16:33","status":"1","discount":"5.63"},{"course_id":"7","course_name":"python","price_INR":"15000","price_USD":"650","no_of_hours":"78","date":"2016-05-14 19:25:34","status":"1","discount":"2.50"},{"course_id":"9","course_name":"HTML & CSS","price_INR":"28,000","price_USD":"980","no_of_hours":"78","date":"2016-05-14 19:31:16","status":"1","discount":"2.36"}]} 
+0

結果のJSONを教えてください。 – Sebastianb

答えて

1

あなたのJSONが値として表形式のデータで "データ" プロパティを返しチェック、すなわち:このため

{ 
    "data" : [ 
     {...}, 
     {...} 
     ... 
    ] 
} 

、単に

public function data_courses(){ 
    $result=$this->Users_model->get_active_courses(); 
    echo json_encode($result); 
} 
を変更

To:

public function data_courses(){ 
    $result=$this->Users_model->get_active_courses(); 
    echo json_encode(array("data" => $result)); 
} 

詳細情報Here

アップデート:また、あなたはjavascriptの中の列よりもHTMLテーブルのヘッダ列(th要素)の同じ数を持っていることを確認する必要があります。現在のところ、javascriptでは7列、htmlでは6列の列が定義されています。列定義の最後の{ "bSortable": false }を削除してください。

+0

ありがとうsebastianb実際に私はJSONとデータテーブルの新機能です。データを列に変更すると空のテーブルが表示されますが、データは表示されません。 – ohmygood

+0

クエリ(get_active_courses)が実際に何かを返すかどうかを確認してください。 – Sebastianb

+0

last_queryで正しいクエリが表示されていることを確認しました@Sebastianb – ohmygood

関連する問題