call widget inside cgiGrid view in yii

2019-09-10 01:53发布

问题:

I have custom widget... I just want to call the widget in Tbgridview...

Here is code..

$this->widget('bootstrap.widgets.TbGridView', array(
'id' => 'test1',
'type' => 'striped bordered condensed',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
    'name',
    'addr',
    'email',

     array(

                   'type'  => 'raw',
                    'value' => $this->widget('DropDownRedirect', array(
                                 'data' => array('1'=>'1','2'=>'2','3'=>'3'))),
                 ),
           )
     ));

its not working.. can anybody help me?...

please refer the class DropdownRedirect given below.

class DropDownRedirect extends CWidget {
public $name; 

public $select; 

public $data; 

public $htmlOptions = array(); 

public $url;

public $replacement = '__value__';

protected function registerScript() {
    $script = '$("#'.$this->id.'").change(function(){'
    .'$(location).attr("href", "'.$this->url.'".replace("'.$this->replacement.'", $(this).val()));'
    .'});';
    Yii::app()->clientScript->registerScript(__CLASS__.$this->id, $script);
}

public function init() {
    if (! isset($this->name))
        $this->name= $this->id;
    $this->registerScript();
}

public function run() {
    if (!isset($this->htmlOptions['id'])) $this->htmlOptions['id'] = $this->id;
    echo CHtml::dropDownList($this->name, $this->select, $this->data, $this->htmlOptions);
}}

Thanks in advance

回答1:

CGridView expects a php expression as value that results in a string once it is evaluated so

  1. the php expression needs to be passed as a string
  2. $captureOutput (last parameter) of CController::widget() needs to be set to true in order to capture the widget instead of sending it directly to the display.
  3. in the php expression $this refers to the column object so the controller should be referenced using $this->grid->controller

Thus:

'value' => '$this->grid->controller->widget("DropDownRedirect",
            array("data" => array("1"=>"1","2"=>"2","3"=>"3")),
            true)'


回答2:

array(
               #'name' => 'name',
               'type'  => 'raw',
                'value' => $this->widget('DropDownRedirect', array(
                             'data' => array('1'=>'1','2'=>'2','3'=>'3'))),
             ),
       )
 ));

i think you need have a name column.