Search code examples
yiifilterwidgetcgridviewdataprovider

Filters in CGridView not filtering


Can You check why filtering is not working in CGridView? When i type for exaple 'Adam' in filter field, nothing happens. I can't find my mistake, everything looks ok but not working. I helped with that article: Yii: CGridView Filter examples

CONTROLLER

 <?php
    class UzytkownikController extends CController
    {
        public function actionIndex()
        {
            $Dane = new Uzytkownik('search');
            $Dane -> unsetAttributes();  // clear any default values
            if(isset($_GET['Uzytkownik']))
            {
                $Dane->attributes=$_GET['Uzytkownik'];
            }
            $this -> render ('index', array(
                'Dane' => $Dane,
            ));
        }
    }
    ?>

MODEL

<?php
class Uzytkownik extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function search()
    {
        $criteria = new CDbCriteria;
        $criteria -> compare('imie', $this -> imie, true);

        return new CActiveDataProvider($this, array(
                'criteria' => $criteria,
                )
        );
    }
}

?>

enter image description here

WIEV

<?php 
    $this -> widget('zii.widgets.grid.CGridView', array(
        'dataProvider' => $Dane -> search(),
        'filter' => $Dane,
        'columns' => array(
                array(
                    'name' => 'imie',
                    'type'=>'raw',
                ),
                array(
                    'name' => 'nazwisko',
                    'type'=>'raw',
                    'filter' => false,
                ),
                array(
                    'name' => 'data',
                    'filter' => false,
                ),
            ), 
        )
    );
?>

enter image description here


Solution

  • For future reference:

    In order to make sure the $model->attributes "saves" the attributes the model needed the following addition:

    public function rules() {
        return array(
          array('imie', 'safe', 'on'=>'search')
        );
    }
    

    And $_GET should have been used instead of $_POST Because the CGridView widget uses GET when posting to the server:

    class UzytkownikController extends CController
    {
        public function actionIndex()
        {
            $Dane = new Uzytkownik('search');
            $Dane -> unsetAttributes();  // clear any default values
            if(isset($_GET['Uzytkownik']))
            {
                $Dane->attributes=$_GET['Uzytkownik'];
            }
            $this -> render ('index', array(
                'Dane' => $Dane,
            ));
        }
    }