如何在yii gridview中添加select2扩展列

时间:2013-10-06 13:00:35

标签: gridview yii

我想在GridView的一列中添加select2扩展名。我怎样才能做到这一点? 另外我只想使用select2 Yii扩展而不使用它的纯库。

3 个答案:

答案 0 :(得分:0)

使用以下方式显示Gridview列的select2,希望它有所帮助。

array(            
      'name'=>'category_id',
      'type'=>'html',
      'value'=>'select2::activeDropDown($model,"my_select",CHtml::listData($dataToShowFromModel,"field_name_for_value","field_name_for_text"),array("empty"=>"","placeholder"=>"Please Select",select2Options=>array("allowClear"=>true)))'
)

答案 1 :(得分:0)

我创建了一个扩展CDataColumn的类,为列添加过滤器:

Yii::import('zii.widgets.grid.CDataColumn');

class TbTableDeviceType extends CDataColumn {
    public $model;
    public $fieldName;

    public function init() {
        $ajaxUpdate = $this->grid->afterAjaxUpdate;
        $this->grid->afterAjaxUpdate = "function(id,data){'.$ajaxUpdate.' 
                $('#" . get_class($this->model) . "_" . $this->fieldName .     "').select2({placeholder:' ', allowClear: true});
        }";
    }

    /**
     * Renders the filter cell.
     */
    public function renderFilterCell() {
        echo '<td><div class="filter-container">';
        $deviceTypes = Helper::getDeviceTypesArray();
        $deviceTypes[''] = ''; // Add empty value to select all
        asort($deviceTypes);
        $this->filter = $deviceTypes;
        $model = $this->model;
        $field = $this->fieldName;
        if (empty($model->$field))
            echo CHtml::dropDownList(get_class($this->model) . '[' . $this-    >fieldName . ']', $this->fieldName, $deviceTypes);
        else
            echo CHtml::dropDownList(get_class($this->model) . '[' . $this->fieldName . ']', $this->fieldName, $deviceTypes, array(
                'options' => array(
                    $model->$field => array(
                        'selected' => true
                    )
                )
            ));
        Yii::app()->controller->widget('ext.ESelect2.ESelect2', array(
            'selector' => '#' . get_class($this->model) . '_' . $this-    >fieldName,
            'data' => $deviceTypes,
            'options' => array(
                'placeholder' => ' ',
                'allowClear' => true
            ),
            'htmlOptions' => array(
                'minimumInputLength' => 2,
                'style' => 'width:100%'
            )
        ));
        echo '</div></td>';
    }
}

然后将此列添加到cgridview:

array(
    'class' => 'ext.widgets.TbTableDeviceType',
    'model' => $model,
    'fieldName' => 'deviceType_id',
    'name' => 'deviceType_id',
),

答案 2 :(得分:-1)

查看此Yii Gridview

的示例

在视图中:例如:admin.php

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        'title',          // display the 'title' attribute
        'content:html',   // display the 'content' attribute as purified HTML
        array(            // display 'create_time' using an expression
            'name'=>'category_id',
            'type'=>'html',
            'value'=>'Post::model()->getSelectTwo()',
        ),
        array(            // display 'author.username' using an expression
            'name'=>'authorName',
            'value'=>'$data->author->username',
        ),
        array(            // display a column with "view", "update" and "delete" buttons
            'class'=>'CButtonColumn',
        ),
    ),
));

在Post.php模型中

public function getSelectTwo(){
    $categories = Category::model()->findAll();
    $data = array();
    foreach($categories as $category){
        $data[$category->id] = $category->title;
    }

    $this->widget('ext.select2.ESelect2',array(
      'name'=>'category_id',
      'data'=>$data,
      'htmlOptions'=>array(
      ),
    ));

}

http://www.codexamples.com/

查看更多Yii教程