Hitman911
(Hitman911)
September 13, 2013, 2:15pm
1
hi,
I’m working on a CGridView widget populated by CArrayDataProvider,
in my case the columns of the grid depend on the SQL query result ( the SQL Query it self is build throw a looping function );
so i have to pass columns array property from the action in the controller,
in controller :
$columns = array();
$columns[] = 'lid';
$formFields = FormFields::model()->findAll();
$sqlString = "SELECT l.id AS lid";
foreach ($formFields as $field){
$sqlString .=", SQL CODE";
$columns[] = array('name'=>$field->field_name_key,'header'=>$field->name);
}
$sqlString .= " FROM table l";
//$columns[]= array('class'=>'CButtonColumn');
$rawData=Yii::app()->db->createCommand($sqlString)->queryAll();
$dataProvider=new CArrayDataProvider($rawData, array(
'keyField'=>'lid',
'sort'=>array(
'attributes'=>array(
'lid',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
$this->render('admin',array(
'model'=>$dataProvider,
'columns'=>$columns
));
in view :
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'lead-grid',
'dataProvider'=>$model,
'columns'=>$columns,
));
every thig work until now, but when i whoul like to add CButtonColumn to $column array it dosen’t work any more
any idea to fix this problem
Why it’s not working? You got an error message? Something does not behave as expected? If you want help please provide more details.
Do you add the CButtonColumn only to the array passed to the widget?
softark
(Softark)
September 13, 2013, 2:30pm
3
Hi Hitman911,
I think that CButtonColumn assumes that the key field is ‘id’ when it constructs the urls for the buttons.
So you may have to specify ‘viewButtonUrl’, ‘updateButtonUrl’ and ‘deleteButtonUrl’ properties manually. Or, I believe you can change the key field name from ‘lid’ to ‘id’.
Hitman911
(Hitman911)
September 13, 2013, 2:32pm
4
nineinchnick:
Why it’s not working? You got an error message? Something does not behave as expected? If you want help please provide more details.
Do you add the CButtonColumn only to the array passed to the widget?
in the controller code i just uncomment this line
//$columns[]= array('class'=>'CButtonColumn');
and i have this error
Trying to get property of non-object
…\framework\base\CComponent.php(612) : eval()'d code(1)
Hitman911
(Hitman911)
September 13, 2013, 2:40pm
5
softark:
Hi Hitman911,
I think that CButtonColumn assumes that the key field is ‘id’ when it constructs the urls for the buttons.
So you may have to specify ‘viewButtonUrl’, ‘updateButtonUrl’ and ‘deleteButtonUrl’ properties manually. Or, I believe you can change the key field name from ‘lid’ to ‘id’.
hi
i changed the lid by id but it does’t work
softark
(Softark)
September 13, 2013, 2:46pm
6
Hmm, does CButtonColumn support only AR objects? I’m not sure.
Try specifying the url expressions manually, please.
Hitman911
(Hitman911)
September 13, 2013, 3:04pm
7
softark:
Hmm, does CButtonColumn support only AR objects? I’m not sure.
Try specifying the url expressions manually, please.
i used this code
$columns[] = array('type'=>'html','value'=>'CHtml::link("view",Yii::app()->createUrl("lead/view", array("id"=>@$data->id)))');
i had this url lead/view&id=
how can i pass the id to the url ?
Hitman911
(Hitman911)
September 13, 2013, 3:35pm
8
i kept my code but just modifing the colomn code from
$columns[]= array('class'=>'CButtonColumn');
to
$buttons = array(
'class'=>'CButtonColumn',
'template'=>'{view}',
'buttons'=>array(
'view' => array ('url'=>'Yii::app()->createUrl("lead/view", array("id"=>$data->id))'),
,
)
);
$columns[]= $buttons
Still not work, but i found the problem
when i replace $data->id by 2 it work !!!
so the probleme it’s how to get the id from CArrayDataProvider
Hitman911
(Hitman911)
September 13, 2013, 4:19pm
9
finally worked , just replaced $data->id by $data[‘id’]