design question - should this be in the controller or the view?

I'm trying to wrap up jqGrid into a widget and I'm stuck on where it's best to define the columns.

The options are to put it in the view like so:



View code:


<div class="yiiForm">


<?php $this->beginWidget("application.widgets.jqGrid.jqGrid", array(


    "gridId"=>"browser", "pagerId"=>"pager", "dataUrl" => $this->createUrl("artistData"), 


    "sortColumn" => "artistName", "rows" => 40,


    "columns" => array(


        array("id" => "artistName", "name" => "Artist Name"),


        array("id" => "artistType", "name" => "Artist Type"),


        array("id" => "artistKey", "name" => "Artist Key"),


    )


)); ?>


<table id="browser"></table>


<div id="pager" class="scroll" style="text-align:center;"></div>


<?php $this->endWidget(); ?>


</div><!-- yiiForm -->


or to shove the column definition into the action on the controller:



Controller code:


<?php


class BrowserController extends CController


{


    public function actionArtist()


    {


        $this->render("artist");


    }


    


    public function artistColumns()


    {


        return array(


            array("id" => "artistName", "name" => "Artist Name"),


            array("id" => "artistType", "name" => "Artist Type"),


            array("id" => "artistKey", "name" => "Artist Key"),


        );


    }


    


    public function actionArtistData()


    {


        Yii::import("application.widgets.jqGrid.jqGrid");


        $criteria = new CDbCriteria;


        $count = ArtistRecord::model()->count($criteria);


        jqGrid::applyCriteria($criteria);


        $artists = ArtistRecord::model()->findAll($criteria);


        


        $this->renderDynamicInternal(


            array("jqGrid", "renderData"), 


            array($artists, $count, $this->artistColumns())


        );


    }


?>





View code:


<div class="yiiForm">


<?php $this->beginWidget("application.widgets.jqGrid.jqGrid", array(


    "gridId"=>"browser", "pagerId"=>"pager", "dataUrl" => $this->createUrl("artistData"), 


    "sortColumn" => "artistName", "rows" => 40,


    "columns" => $this->artistColumns()


)); ?>


<table id="browser"></table>


<div id="pager" class="scroll" style="text-align:center;"></div>


<?php $this->endWidget(); ?>


</div><!-- yiiForm -->


The advantage of the latter is that if we have a model with more columns than we want to display, we can pass the column definition to the script which builds the json data, allowing that script to reduce the data which is returned to the jqGrid control on each sort, page, etc. It just doesn't feel quite right to have that definition in the controller rather than the view because defining which columns are to be displayed is essentially the domain of the view.

Also, is that way too much information to be passing to the beginWidget call?

Once I'm done, I'll wrap the thing up and put it up here for people to download and use.

Your widget design is fine, and both usages of the widget are acceptable. For simple column definitions, we can just put the definition inside the view. For complex scenarios, putting it in controller makes the view look cleaner. So it really depends on the user's taste and need.

I do have a question: why you call renderDynamicInternal() in actionArtistData()? The method is marked as internally used, and it is acutally called by renderDynamic when using together with fragment cache. So you shouldn't call renderDynamic either. For your purpose, I think you can just echo the result instead of calling any render methods.

I think the renderDynamicInternal thing is just a hang-up from not quite being used to just echoing whenever it's time to do some outputting.

It was a classic case of The Complicator’s Gloves - scouring the API for a render method that would output the result when a simple echo would suffice.

I guess I'm more used to doing things the PRADO way than the Yii way and I'm sure I'll be guilty of similar errors in approach for a while yet. I will definitely remove it and replace the call with an echo!

Yeah, I understand you.  ;D

ShabbyRobe,

Any progress on the widget…

I would like to start playing with it…

Thanks,

Edward

I hit a point with it where browsing works and the tree view works, but that's where I left it. The code badly needs a cleanup but if you're happy to work with it as is I'll make sure it works and upload it tomorrow.