2013-10-06 54 views
1

我想在GridView的一列中添加select2擴展名。我怎樣才能做到這一點? 另外我只是想使用select2 Yii擴展,而不是使用它的純庫。如何在yii gridview中添加select2擴展列

+0

有沒有可能您解決了這個問題?任何示例代碼,你可以分享? –

+0

@ samuel.molinski我爲每個輸入字段使用不同的id和一個代碼插入s2。您必須在gridview – amirmohammad

+0

Amir中設置'ajaxUpdate'=> false,您是否介意共享此問題的解決方案? – ersks

回答

-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(
    ), 
    )); 

} 

查看更多的Yii教程在http://www.codexamples.com/

+0

在模型中,$ this指向模型類,但對於調用widget,我們必須調用控制器類。所以這個小部件將無法工作。 – amirmohammad

0

使用下面的方式來顯示我沒有測試過,但希望它有助於選擇2,

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)))' 
     ) 
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', 
),