Cannot do filtering via a dropdown menu in Yii TbGridView (v1.x.x)

73 views Asked by At

I am trying to get the filtering to run although I am having some issues below is my view note that this in in a PARTIAL view :-

If I click on the header of the column it does the sorting as expected, however if I select an option from the dropdown it doesn't filter the items... Any ideas?

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

My model has the following search() method:

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

There are 1 answers

0
Dinistro On

I think you can't use a DropDownList as filter, but you can easily use 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'),
),

This will automatically generate a DropDownList for you.

I hope this works for you.