无法通过Yii TbGridView(v1.x.x)中的下拉菜单进行过滤

时间:2014-11-11 15:37:41

标签: yii frameworks yii-extensions yii-components yii-modules

我正在尝试运行过滤,虽然我在下面遇到一些问题,但我的观点是在PARTIAL视图中注意: -

如果我点击列的标题,它会按预期进行排序,但是如果我从下拉列表中选择一个选项,它就不会过滤项目... 有什么想法吗?

<?php
$myuser = new Myuser('search');    
$filterBtn = $this->widget('bootstrap.widgets.TbButton', array(
    'icon'          => 'filter',
    'size'          => 'small',
    'label'         => $myuser->paging ? 'View All' : 'View Less',
    'htmlOptions'   => array('class'=>'pull-right', 'style'=> 'margin:20px 0;'),
    'url'           => $myuser->paging ? array('/carrot/myuser/admin/paging/0') : array('/carrot/myuser/admin')
), true);

$this->widget('bootstrap.widgets.TbGridView',array(
'id'                => 'myuser-grid','type'=>'striped bordered condensed',
'dataProvider'      => $myuser->search(),
'filter'            => $myuser,
'columns'           => array(
    array(
        'id' => 'user_id',
        'class' => 'CCheckBoxColumn',
        'checked' => 'in_array($data->user_id, $this->grid->owner->model->student_ids)',
        'checkBoxHtmlOptions' => array(
            'name' => 'selected_student_id[]',
        )
    ),
    'firstname',
    'surname',
    'username',
    array(
        'name'              => 'year_id',
        'filter'            => CHtml::activeDropDownList($myuser, 'year_id', CHtml::listData(Organisation::model()->distinctYears, 'year_id', 'year_id'), array('prompt'=>'All Years')),
        'htmlOptions'       => array('style' => 'text-align:center;'),
        'headerHtmlOptions' => array('style' => 'text-align:left;'),
    ),
    array(
        'name'              => 'form_name',
        'header'            => 'Form',
        'filter'            => CHtml::activeDropDownList($myuser, 'form_name', CHtml::listData(Organisation::model()->distinctForms, 'form_name', 'form_name'), array('prompt'=>'All Forms')),
    ),
    array(
        'name'              => 'House',
        'filter'            => CHtml::activeDropDownList($myuser, 'House', CHtml::listData(Organisation::model()->distinctHouses, 'House', 'House'), array('prompt'=>'All Houses')),
    ),
),
)); ?>

我的模型有以下search()方法:

public function search($limit = false)
{
    $criteria = new CDbCriteria();

    $criteria->compare('t.user_id',$this->user_id,true);
    $criteria->compare('t.firstname',$this->firstname,true);
    $criteria->compare('t.surname',$this->surname,true);
    $criteria->compare('t.username',$this->username,true);
    $criteria->compare('t.year_id',$this->year_id);
    $criteria->compare('t.form_name',$this->form_name);
    $criteria->compare('t.House',$this->House);
    $criteria->compare('o.organisation_id',$this->organisation_id);

    $criteria->group = 't.user_id';
    $criteria->together = true;

    return new CActiveDataProvider($this->currentUserOrganisation(), array(
        'criteria'      => $criteria,
        'pagination' => array(
            'pageSize' => $this->paging ? ($limit) ? $limit : OverviewController::PAGE_SIZE : 2000
        ),
        'sort'          => array(
            'defaultOrder'  => array('firstname'=>false, 'surname'=>false),
            'attributes'    => array(
                'organisation_name' => array(
                    'asc'       => 'organisation_name',
                    'desc'      => 'organisation_name DESC',
                    'default'   => 'desc',
                ),
                '*'
            )
        ),
    ));
}

1 个答案:

答案 0 :(得分:0)

我认为您无法使用DropDownList作为过滤器,但您可以轻松使用CHTML::listData()

array(
    'name' => 'year_id',
    'filter' => CHtml::listData(Organisation::model()->distinctYears, 'year_id', 'year_id'),
    'htmlOptions' => array('style' => 'text-align:center;'),
    'headerHtmlOptions' => array('style' => 'text-align:left;'),
),
array(
    'name' => 'form_name',
    'header' => 'Form',
    'filter' => CHtml::listData(Organisation::model()->distinctForms, 'form_name', 'form_name'),
),
array(
    'name' => 'House',
    'filter' => CHtml::listData(Organisation::model()->distinctHouses, 'House', 'House'),
),

这将自动为您生成DropDownList。

我希望这适合你。