Hi guys,
I’m new at yii and I have a really annoying problem with a dropdownlist.
So basically i have a Gridview which represents a database table. One column is represented by a dropdown list with all available values for this column. Initially the current value is displayed. What I am looking for is some kind of "onchange" function which updates the database when the value is changed. I want to to this with an ajax call.
So here is my implementation of the view
$criteria = new CDbCriteria();
$criteria->select = 'right_desc_r';
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'ext_user.name_usr',
'stream.name_st',
array('name'=> 'actions',
'type' => 'raw',
'header' => 'Rights',
'value' => '$data->actions',
),
)
));
$dataprovider contains all rows as key=>value pairs
for the last column I definded a function getActions in the model which provides the data for this column
(The value of this column is a foreign key to a different table called "Rights")
public function getActions(){
return CHtml::dropDownList('right', $this->right_id_ur, CHtml::listData(Rights::model()->findAll(), 'right_id_r', 'right_desc_r'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url' => Yii::app()->createUrl('externaluser/updaterights/'), //url to call.
'data' => array(
'usr_right_id' => $this->usr_right_id_ur,
'right_id' => 'js:this.index',
),
)
));
}
so if the value of the dropdownlist changes the updaterights action in the externaluser model is called
public function actionUpdaterights(){
$m = UserRights::model()->find('usr_right_id_ur='.$_POST['usr_right_id']);
$m->setAttribute('right_id_ur',$_POST['usr_right_id']);
$m->save();
}
So the problem that I’m facing is, besides the fact that I don’t seem to get the current selected index from ‘js:this.index’, that even if I enter a constant value for ‘usr_right_id’ it is always the last column in the table that gets updated. It seems that in the getAction method “$this->usr_right_id_ur” always returns the id of the last column.
Any help on this topic or an explanation why this happens would be much apprechiated.