Zend Framework and dojo-grid sorting example

12 Jan

In my previous post “dojo-grid-in-zend-framework:creating-nice-and-cool-grid-in-php-using-zend-framework-and-dojo” I discuss how to creat nice and cool gird using Zend framework and dojo.

In this article I am going one step further and show you how easy it is to apply sorting to your dojo grid.

Please read my previous article befor writing this code.

In your model write

public function getJobsData($sort,$order)


$select = $this->_db->select()


->order(” “.$sort.” ” . $order);

$results = $this->getAdapter()->fetchAll($select);

return $results;


If you compare this model with the previous one, you will find that we are passing tow extra parameter here and a line

->order(” “.$sort.” ” . $order);

This will sort our data according to the field we will click on.

Now In your controller, write

public function  recordsAction()


$jobs = new Jobs();

if ($this->getRequest()->isXmlHttpRequest()){







$sort = substr($sort,1,strlen($sort));

$order = “Desc”;

} else {



$data= $jobs->getJobsData($sort,$order);

$dojoData= new Zend_Dojo_Data(‘id’,$data,’id’);

echo $dojoData->toJson();



Here we first check if we get ajax request. If yes, we get the value of the variable sort (dojo grid passed this as parameter when we click on it field), so we should not worry about it. Dojo grid either pass “field name” or field name with -ve sign.

we check if the sort variable has -ve sign attached with it. If we found we then remove the first character and set sort order to “Desc”. if we don’t find -ve sign we simply set sort order to “Asc”. we then call our model function

getJobsData() giving it $sort and $order variable. the result return is converted to json.

The last minor change we will do in our view template code

<div dojoType=”dojox.data.QueryReadStore” clientSort=”true jsId=”activeStore”, url=”records” ></div>

The only change we did is highlited in red.

That’s it nice and cool dojo grid with sorting.

Hopefully in next article I’d discuss how to implement pagging in the dojo grid.

3 Responses to “Zend Framework and dojo-grid sorting example”

  1. Leandro January 15, 2009 at 2:18 pm #

    thanks guy!
    it’s run fine.

  2. Poldi January 19, 2009 at 1:11 pm #

    it works, but AFAIK the magic method is ‘clientSort=”true”’ described in last paragraphs. you can simply add this to example before and thats it. component will allow you sort by any column. not sure about reason for adding sort into fetching???

  3. Jim March 17, 2009 at 3:41 am #

    Thanks for the summary. A couple of things I’ve found out while trying to get things going that might be helpful for others.

    – Having exit in my code just didn’t work for me. I had to put the following in my controller’s action for it to work
    // this has no associated renderer
    // this has no associated layout

    – Grid sorting works just fine, however, the actual sort mechanism it was using is another call to the data store’s url, with sort=blah or sort=-blah, so I had to add this logic to my controller to read and modify the query. I am sure there is something wacky about that, is the records it retrieves could be doubled if I were scrolled several “pages” down….not sure though.

    Hopefully that is of use to others.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: