Yii framework custom methods inside datatable row

I have been using the  edatatables extension from the yiiframework for a while now. I have noticed that you can do a ton of things with it. These things take very little effort. The extension is very powerful and was well done. Recently I ran into a situation that wasn’t apparent to me right away on how to implement a column with customized HTML. I wasn’t able to use the default column_name value since I needed to format it. The first thing I did was create a method inside the given model that would return the HTML. In the Yii framework models cannot call partials so I had to have the HTML within that models method. To me this looked out of place and felt dirty. Being a pragmatic programmer I decided to look into a better solution. I stumbled upon this solution while researching this. This solution did not seem to be common so I think this post could be beneficial to others.

The solution was to call an internal method within that controller and pass in the given data row. Through the internal function I am then able to render a partial and pass that HTML back to the actual row. When I called render partial in Yii I add the true parameter at the end so it doesn’t render it real time. Instead keeping it in a variable to return. This allowed me to remove the method from the model and move that HTML logic into the view layer. The code below show my implementation that involved adding the custom array to the columns definition and create an internal method to be called.

$columns = array( ‘column1’, ‘column2’, ‘column3’, array( ‘header’ =>         ‘Column 4’, ‘class’ => ‘EDataColumn’, ‘sortable’ => ‘false’, ‘value’ =>         array($this, ‘_latestPrices’), ‘type’ => ‘html’, ),

protected function _partialViewBeingCalled($data, $row) {

$result = $this->renderPartial(’../shared/_myPartial’,array( ‘Data’ => $data, ), true);

return $result;


As you can see the solution is very simple. The hardest part was figuring out how the datatable would call the internal method while passing the data. After those updates I now have a clean solution that I can feel satisfied with.

Leave a Reply

Your email address will not be published. Required fields are marked *