Display data
Display data
Lykos
Posts: 1Questions: 1Answers: 0
Hi, I'd like some help please. I'm using Ignited Datatables in order to display post data in tabular way. These are the main parts of my code:
// in post model
/**
* Get page data on datatables
*/
public function get_datatable() {
$this->load->library('datatables');
$this->datatables->select('id, title, slug, date_published, status');
$this->datatables->from('posts');
return $this->datatables->generate();
}
// in controller
/**
* List all posts
*/
public function index() {
$this->data['datatables'] = true;
$this->data['data_url'] = 'admin/posts/datatables_ajax';
// $this->data['posts'] = $this->post_model->get_datatable();
// dump($this->data['pages']);
// load view
$this->load->view('admin/pages/index', $this->data);
}
public function datatables_ajax() {
$this->output->enable_profiler(false);
echo $this->page->get_datatable();
// echo json_encode($this->page->get_datatable());
exit();
}
// this is my view
<table class="table dataTable table-bordered" cellspacing="0" width="100%">
<thead> <!-- TODO - Display the headings: Title, Date Published, Url Slug -->
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody></tbody> <!-- TODO - Display the posts -->
</table>
// and the script
<?php if(isset($datatables)): ?>
<script type="text/javascript" src="<?php echo base_url('js/dataTables/jquery.dataTables.min.js'); ?>">
<script type="text/javascript" src="<?php echo base_url('js/dataTables/dataTables.bootstrap.min.js'); ?>">
<script type="text/javascript">
$(document).ready(function() {
$('.dataTable').DataTable({
'bProcessing' : true,
'bServerSide' : true,
'sAjaxSource' : '<?php echo base_url($data_url); ?>',
'sServerMethod' : 'POST',
'fnServerData' : function (sSource, aoData, fnCallback) {
$.ajax({
dataType : 'json',
type : 'post',
url : sSource,
data : aoData,
success : fnCallback,
});
}
});
});
</script>
<?php endif; ?>
If I access from the url the datatables_ajax() function I can get the reuslt from database
{"draw":0,"recordsTotal":2,"recordsFiltered":2,"data":[{"id":"1","title":"First post","slug":"first-post","date_published":"2015-09-20","status":"visible"},{"id":"2","title":"Second post","slug":"second-post","date_published":"2015-09-20","status":"visible"}]}
but the problem is I can't display them on my table view. Any ideas what I'm doing wrong? Any help would be appreciated
This discussion has been closed.