[Solved] How To Dynamically Generate Buttons For Cgridview Column?

Hello,

Is it possible to dynamically add buttons to the CGridView widget?

This is what I would like to do:




$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

    'columns'=>array(

        'id',

        'name',

        array(

            'class'=>'CButtonColumn',

        	'header'=>'Select an available button',

        	'template'=>'** Add some number of buttons here based on some pre-defined variable **',

        	'buttons' => array(

                // Add the properties for some number of buttons here based on some pre-defined variable

        	)

        ),

    ),

    

));



The thing that makes this hard is that I need to define specific properties (in the ‘buttons’ array) for each button that has been added.

Any ideas?

Thanks!

Extend the CButtonColumn

Hi chipit24

Here is an example of a custom button:




'buttons'=>array(

	'myview'=>array(

		'label' => 'View',

		

		'url'=>'$this->grid->controller->createUrl("trn2_mct/viewtree", array(

			"modId"		=> $data->primaryKey,

			"modelcode" 	=> "ar_trn2",

			"pmodelcode"	=> "'.$modelcode.'",

			"viewfolder"	=> "trn1",

			"view"		=> "treelist1",

			"modelfuncname"	=> "getTreeLabelPrint",									

		))',

		

		'click'=>'function(){

			loadingGif_in();

			initialiseDialog("dialog1", "iframe1", "Loading...");

			setIframeSourceUrl("iframe1", $(this).attr("href"));

			return false;

		}',

	),



As you can see, property values can be set by using variables.

Look at the url. It’s properties consist of strings as well as variables. In fact, you should be able to pass the whole url in a variable, which will allow you to create these variables in the controller or model. The button will then look something like this:




'buttons'=>array(

	'myview'=>array(

		'label' => 'View',

		'url'	=> $model->functionCreatingUrl(), // obtained from model

		'click'	=> $click, // passed from controller

	),



Not sure if this is what you requested.

Thanks for the replies.

Gerhard Liebenberg: That is not what I requested. Perhaps my original post was a bit ambiguous.

PeRoChAk: Extending the CButtonColumn class seems a bit daunting for me right now.

The template property will require a string, and the buttons property will require an array. I can dynamically generate both in my controller and pass it onto the view. But the caveat is each button in the button array will require a URL unique to the row where it is inside the CGridView table. Hmm, maybe I can just insert a function that takes the unique row values as parameters, and have that function return an array.

Is this clear?

If I get it working I will most my code here.

UPDATE:

I got this working by using the method I described above. It will be hard to tell what I’m doing if I post my code, but I created a function in my controller which returns an array that I can use in CGridView to generate my buttons.