Question

I am unable to set the value of the column day in the cgrid view : I am able to see the dropdownlist but all have the same day Monday set irrespective of the value of the day as represented by $data->day

$this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
        'columns'=>array(
        array(
            'type'=>'raw',
            'name'=>'day',
        'value'=> ' CHtml::dropDownList(\'someName\'.$row,\'$data->day\',array(
                    \'Mo\'=>\'Monday\',
                    \'Tu\'=>\'Tuesday\',
                    \'We\'=>\'Wednesday\',
                    \'Th\'=>\'Thursday\',
                   \'Fr\'=>\'Friday\',
                   \'Sa\'=>\'Saturday\',
                   \'Su\'=>\'Sunday\',))',  

                ),
            'ts_id'
        )
    )); 
Était-ce utile?

La solution 2

Just a minor issue resolved the whole episode. I had put quotes around $data->day in the original post. which was not needed.

   array(  'type'=>'raw',
            'name'=>'day',
            'value'=> ' CHtml::dropDownList(\'someName\'.$row,$data->day,
             array(\'Mo\'=>\'Monday\',
                        \'Tu\'=>\'Tuesday\',
                        \'We\'=>\'Wednesday\',
                        \'Th\'=>\'Thursday\',
                       \'Fr\'=>\'Friday\',
                       \'Sa\'=>\'Saturday\',
                       \'Su\'=>\'Sunday\',)
              )',  
     ),

Autres conseils

If you can accept a minor break in MVC then things will get much easier to read if you put a little getter method into your model:

public function getDayDropDown()
{
    $days = array('Mo'=>'Monday', ...);
    return CHtml::dropDownList('someName', $this->day, $days);
}

Now in your gridview you can use it as a column like

array(
    'name' => 'Day',
    'type' => 'raw',
    'value' => '$data->dayDropDown',
),

This is an example of dropdown value and update onchange:

$this->widget('bootstrap.widgets.TbGridView', array(
    'id'=>'session-grid',
    'type'=>'striped bordered condensed',
    'dataProvider'=>$PSDataProvider,
    'filter'=>$PSModel,
    'emptyText'=>'No hay sesiones abiertas.',
    'columns'=>array(
        array('name'=>'username', 'header'=>'Nombre de Usuario', 'filter'=>CHtml::activeTextField($PSModel, 'username', 
                 array('placeholder'=>'Buscar por usuario...'))),
        array('name'=>'product', 'header'=>'Producto', 'filter'=>CHtml::activeTextField($PSModel, 'product', 
                 array('placeholder'=>'Buscar por producto...'))),
        array('name'=>'expire', 'value'=>'date("d-m-y H:i:s", $data->expire)','header'=>'Hora de Expiracion', 'filter'=>CHtml::activeTextField($PSModel, 'expire', 
                 array('placeholder'=>'Buscar por expiracion...'))),
        array(
          'header'=>CHtml::dropDownList('pageSize',$pageSize,array(5=>5,10=>10,20=>20,50=>50),array(
            'onchange'=>"$.fn.yiiGridView.update('session-grid',{ data:{pageSize: $(this).val() }})",
            'class'=>'span1 custom-tb-dropdown-inline',
          )),
          'class'=>'bootstrap.widgets.TbButtonColumn',
          'htmlOptions'=>array('style'=>'width: 50px'),
          'template'=>'{delete}',
          'buttons'=>array(            
            'delete' => array(
              'label'=>'Terminar sesión',
            ),
          ),
          'deleteConfirmation'=>'Está seguro que desea terminar la sesión seleccionada?',
          'deleteButtonUrl'=>'$this->grid->owner->createUrl("productSession/delete", $data->primaryKey)'
        ),
    ),
));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top