I am trying to use datatables within the Yii framework. I am also using the savestate param provided by the datatable to save the current pagination, no of entries etc within the table.
The table I am currently using consists of different columns which uses datatables for pagination, no of entries etc. One of the columns is ‘View’ when clicked displays the detail view corresponding to the column at the bottom of the page below the table.
When pressing view each time, the page refreshes and displays the data. However the pagination and no of entries is not working properly as it keeps on jumping to different page numbers and shows different list of entires. Below is my code for reference.
Controller.php
public function actionIndex() {
$this->render('index');
}
public function actionView($id){
$this->render('view', array('id' => $id));
}
index.php
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>View</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>View</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td><a href="<?php echo Yii::app()->createUrl('site/view', array('id' => 1)); ?>" class="view">View</a></td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td><a href="<?php echo Yii::app()->createUrl('site/view', array('id' => 2)); ?>" class="view">View</a></td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td><a href="<?php echo Yii::app()->createUrl('site/view', array('id' => 3)); ?>" class="view">View</a></td>
</tr>
<tr>
<td>Rhona Davidson</td>
<td>Integration Specialist</td>
<td>Tokyo</td>
<td><a href="<?php echo Yii::app()->createUrl('site/view', array('id' => 4)); ?>" class="view">View</a></td>
</tr>
<tr>
<td>Colleen Hurst</td>
<td>Javascript Developer</td>
<td>San Francisco</td>
<td><a href="<?php echo Yii::app()->createUrl('site/view', array('id' => 5)); ?>" class="view">View</a></td>
</tr>
<tr>
<td>Sonya Frost</td>
<td>Software Engineer</td>
<td>Edinburgh</td>
<td><a href="<?php echo Yii::app()->createUrl('site/view', array('id' => 6)); ?>" class="view">View</a></td>
</tr>
</tbody>
</table>
<script>
$(document).ready(function() {
$('#example').dataTable({
"aLengthMenu": [[2, 3, 4, 5, -1], [2, 3, 4, 5, "All"]],
stateSave: true
});
});
</script>
view.php
<?php $this->renderPartial('index');?>
<div style="background: green; margin-top:10px;">
<h2> <?php echo $id;?></h2>
</div>
Thank you.