To add dropdownlist to CGridView

Hi,

I have two related tables. I want to add a dropdownlist to the CGridView such that it populates only those values that match the id(FKey) of the another table. I went through many related post but found no solution. Could anyone help me or guide me?

My tables:

Parameters–>parameter_id,parameter_name

ConditionParameter–>cond_parameter_id,cond_parameter,parameter_id

I want to populate the cond_parameter in my dropdownlist depending on the parameter_id of the first table.

Please help me.

Please anyone…

Atleast anyone let me know how to create a dropdownlist in a cgridview.

you mean a filter?

http://blog.mbischof.de/cgridview

Perhaps no one has replied because your question is a bit unclear.

Questions:

  1. Are you trying to browse the Parameters, or ConditionParameters?

    My guess is: you’re trying to browse Parameters, one Parameter per line. Is that true?

  2. In the view, next to each Parameter name, you want a dropdown

    showing all Conditions matching parameter_id, right?

  3. In the view, what is the purpose of showing the Conditions in a dropdown?

    CGridView is meant for viewing, not for updating, so I don’t see why it’s

    useful to display the set of Conditions as a dropdown. Why not just

    display each condition, separated by newlines?

  4. Your schema. Your database table and column names seem strange to me. E.g.,

    a. Why is Parameters plural, but ConditionParameter is singular?

    b. In your parameters table, your column names repeat the table name.

    You have parameters.parameter_id and parameters.parameter_name
    
    
    Why not the simpler, and more standard names, like below:
    
    
    parameters.id, parameters.name
    

I’ll check on this in the a.m. if no one else has replied.

:mellow:

Thanks Emily for your response.

Yes i want to display parameter names and depending on parameter_id i want to populate dropdownlist for condition parameters.

If it is not possible in cgridview then will have to try some other option.

Yes it should have been just Parameter(singular) and Condition Parameters(plural). I never notice that.

Also my database has many tables thats the reason i mention it as parameter_id and parameter_name instead of just id and name for better readability.

What about this question?

  1. In the view, what is the purpose of showing the Conditions in a dropdown?

CGridView is meant for viewing, not for updating, so I don’t see why it’s

useful to display the set of Conditions as a dropdown. Why not just

display each condition, separated by newlines?

Stated another way, a dropdown is intended to allow the to select one value from a set of many. But your parameters table has a one-to-many relationship with your parameter_conditions table, so it makes no sense (to me) to just choose one.

yes what u said is correct. so i tried the following cgridview code


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

	'id'=>'parameters-grid',

	'dataProvider'=>$model->search(),

	

	'columns'=>array(

		array( 

			'header'=>'Id',

			'value'=>'$data->parameter_id'),

		array( 

			'header'=>'Parameter',

			'value'=>'$data->parameter_name'),

			

		'para.cond_parameter',

		

		array(

			'class'=>'CButtonColumn',

			'template'=>'{addvalues}',

			'buttons'=>

			array(

			'addvalues'=>array(

			'label'=>'Add Values',

			'url'=>'Yii::app()->createUrl("conditionParameter/create")'),

			),

here para is the relation name, but not working as expected. May be since para.cond_parameter is an array of value. Dont know how to sort out this problem.

According to my understanding of your schema, the foreign relation is in the ConditionParameter table, not in the Parameters table. So, when browsing the Parameters table, you cannot refer to ‘para.cond_parameter’, because it does not exist; i.e., it does not get returned by the relations() function of the Parameters A/R class.

I’m going to sign off of this thread because I’ve run out of time. Best of luck!

Thanks Emily, I got the solution for it from your reply.

I used the cgridview of ConditionParameter and called the parameter name from Parameters there. I was doing it the reverse way.

Thanks :)

If you’ve solved this, I’d love to see your final solution here, thanks!

:mellow: