Recently we’ve discovered great jQuery plugin DataTables. Except it looks great it has also some wonderful AJAX features which allows to apply it to really huge data lists.
Here is the quick way to tie it with your data source using Doctrine ORM (please refer to this post in case you need to use it with Propel: http://nibsirahsieu.wordpress.com/2010/02/14/jquery-datatables-and-symfony/.
actions.class.php
public function executeList(sfWebRequest $request) { if ($request->isXmlHttpRequest()) { $q = Doctrine_Query::create() ->select('column1,column2,column3,column4') ->from('table'); $pager = $this->getPager('table', $q, $request->getParameter('page', $this->getPage()), $request->getParameter('iDisplayLength')); $aaData = array(); $list = $pager->getResults(); foreach ($list as $v) { $aaData[] = array( "0" => $v->getColumn1(), "1" => $v->getColumn2(), "2" => $v->getColumn3(), "3" => $v->getColumn4(), ); } $output = array( "iTotalRecords" => count($pager), "iTotalDisplayRecords" => $request->getParameter('iDisplayLength'), "aaData" => $aaData, ); return $this->renderText(json_encode($output)); } } |
listSuccess.php
<style type="text/css" title="currentStyle"> @import "/sfDataTables/css/demo_page.css"; @import "/sfDataTables/css/demo_table.css"; </style> <script type="text/javascript" language="javascript" src="/sfDataTables/js/jquery.dataTables.js"> <table cellpadding="0" cellspacing="0" border="0" class="display" id="example"> <thead> <tr> <th width="25%">Column1</th> <th width="25%">Column2</th> <th width="25%">Column3</th> <th width="25%">Column4</th> </tr> </thead> <tbody> <tr> <td colspan="3" class="dataTables_empty">Loading data from server</td> </tr> </tbody> <tfoot> <tr> <th width="25%">Column1</th> <th width="25%">Column2</th> <th width="25%">Column3</th> <th width="25%">Column4</th> </tr> </tfoot> </table> <script> $(document).ready(function() { $('#example').dataTable( { "bStateSave": true, "sPaginationType": "full_numbers", "bProcessing": true, "bServerSide": true, "sAjaxSource": "<?php echo url_for('module/list') ?>", }) }); </script> |
Please note that these 2 parameters:
"bStateSave": true,
"sPaginationType": "full_numbers",
are rather optional and everything will work w/o them.
Also pay attention to this plugin “extras” – there are some really sweet things to extend its functionality.
Btw, have you been used this or similar plugins before? Do you think it’s good enough for 100K records list?
Our tests show that it works like charm but never knows how it goes in real life.
8 replies on “Using DataTables with Symfony (Doctrine version)”
[…] Using DataTables with Symfony (Doctrine version) | SymfonyLab […]
[…] En parcourant la toile, je suis tomb? sur un post du blog “Symfonylab.com” qui trait? ce sujet. J’ai donc jug? utile de faire suivre l’information. Le post original se trouve ? l’adresse suivante: http://www.symfonylab.com/using-datatables-with-symfony-doctrine-version/ […]
The command filling $aaData is bugged – will always overwrite the “0” key.
You are right, thank you for the updated. It has been fixed
I get the following error when trying to use this code:
Call to undefined method homeActions::getPage
Have you ommitted this method? What should it contain?
FYI, this page /site (symfonylab.com) breaks in Mac Safari 5
Hi,
I’m having an issue getting the paging to work with this and would be grateful if you could contact me. The paging buttons don;t seem to fire an event and no XHR request is made to the server when I click the next or previous buttons. Symfony version is 1.4. I can display the data and the show x entries dropdown is working.
Many thanks!
Hi,
i’m curious about using getPager(), cannot finds this method at my symfony 1.4.
do you use any special plugins, where getPager described?
Attempted to call an undefined method named “getPager” of class “Symfony\CRUDBundle\Controller\DefaultController”
why this happen?