2016-10-09 2 views
0

몇 가지 자습서와 ajax/dataTables에 대한 설명서뿐만 아니라 SO에 대한 몇 가지 답변을 살펴 보았습니다. 내 dataTable은 여전히 ​​JSON 데이터로 채워지지 않습니다.Codeigniter 및 Ajax로 dataTable을 채울 수 없습니다.

HTML :

<table id="table" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%"> 
     <thead> 
      <tr> 
       <th>Status</th> 
       <th>Student Name</th> 
       <th>Exam Name</th> 
       <th>School</th> 
       <th colspan="2">Action</th> 
      </tr> 
     </thead> 

     <tfoot> 
      <th>Status</th> 
      <th>Student Name</th> 
      <th>Exam Name</th> 
      <th>School</th> 
      <th colspan="2">Action</th> 
     </tfoot> 
</table> 

자바 스크립트 :

<script type="text/javascript"> 

    $(document).ready(function() { 

     // Datatables 
     $('#table').DataTable({ 
      "url": "<?php echo site_url('exams/ajax_list'); ?>", 
     }); 

    }); 

</script> 

시험 컨트롤러 ajax_list PHP 함수 :의로 json_encode 출력이 방법으로 탐색 할 때 내가 무엇을 볼 수에서

public function ajax_list() { 
    $list = $this->exam_model->get_datatables(); 
    $data = array(); 
    foreach ($list as $exam) { 
     $row = array(); 
     $row[] = $exam->exam_status; 
     $row[] = $exam->first_name . " " . $exam->last_name; 
     $row[] = $exam->exam_name; 
     $row[] = $exam->exam_school; 

     $data[] = $row; 
    } 

    echo json_encode($data); 
} 

올바르게,하지만 dataTable은 여전히 ​​비어 있습니다.

나는 무엇이 있습니까?

+0

ajax_list의 경로를 두 번 확인 했습니까? – David

답변

1

좋아, 마침내 작동하게되었습니다 ... 저에게 모든 새로운 브랜드로서 이걸 가지고 놀 수있는 기회를 주셔서 감사합니다. 그것은 약간의 놈 이었지만, 다른 모든 것처럼 그것은 단순한 것으로 판명되었습니다.

내가 CI하지만 늘 문제에 대해이 설정하지 않은 ...

문서를 통해 drudging 후 나는 ...이과에

1.Change 당신의 "URL을"등장 "아약스". 사용하는 경로가 정확하다고 가정합니다. 내가 가진 것을 바꿔라.

<script type="text/javascript"> 
    $(document).ready(function() { 
     // Datatables 
     $('#table').DataTable({ 
      "ajax": "./ajax_list.php" // change this to suit. 

     }); 
    }); 
</script> 

2.

는 번째 태그에있는 열 병합 = "2"를 제거합니다.

3.

그리고 마지막으로 당신의로 json_encode에 변경 당신은 내가 들여다 보지 않았다 다른 옵션을 사용하지 않는 한 그것을 좋아하지 않아 ... ...

echo json_encode(['data'=>$data]); 

그리고 희망이 당신을 데려오고 달리는 ... 문서화가 꽤 좋다.

+0

@TimBrownLaw 당신을 사랑합니다. 이 모든 것을하는 것은 효과가있었습니다. 나는 충분히 문서를 철저히 읽지 않고 있다고 생각한다. 테이블이 채워졌습니다 !!! –

+0

@TimothyFisher 자존심을 다해 주셔서 감사합니다.하지만 이제 다행입니다. 건배! – TimBrownlaw

+0

태그에서 colspan = "2"를 잃습니다. 좀 더 자세하게 설명해 주시겠습니까? @TimBrownlaw –

관련 문제