Jozsef Naghi
Jozsef Naghi

Reputation: 1095

Filter data in Yii framework issue?

This is the view :

<?php
      $this->renderPartial('_search', array(
          'model' => $model
      ));
?>

This is the _search.php file:

<div class="wide form">
    <?php 
        $form = $this->beginWidget('CActiveForm', array(
            'action' => Yii::app()->createUrl($this->route),
            'method'=>'get',
        )); 
    ?>
    <div class="row">
        <?php echo $form->label($model,'month'); ?>
        <?php echo $form->textField($model,'month'); ?>
    </div>
    <div class="row">
        <?php echo $form->label($model,'year'); ?>
        <?php echo $form->textField($model,'year'); ?>
    </div>
    <div class="row buttons">
        <?php echo CHtml::submitButton('Filter'); ?>
    </div>
    <?php $this->endWidget(); ?>
</div>

The controller index function :

        $user_id = Yii::app()->user->attributes->id;

        $dataProvider = new CActiveDataProvider('Salaries', array(
            'criteria'=>array(
                'order'=>'id DESC'
            ),
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        $model = new Salaries();
        $this->render('index',array(
            'dataProvider' => $dataProvider,
            'model' => $model
        ));

And the model:

public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('first_name',$this->first_name,true);
        $criteria->compare('last_name',$this->last_name,true);
        $criteria->compare('month',$this->month);
        $criteria->compare('year',$this->year);

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

When I press the filter button the url is generated and it looks like:

/index?Salaries[month]=2&Salaries[year]=&yt0=Filter but it does not do anything. I don't understand why . What am I doing wrong? thx

UPDATE THIS IS THE TABLE:

 $this->widget('GridView', array(
            'id' => 'my-grid',
            'dataProvider' => $dataProvider,
           // 'filter'=>$model,
            'ajaxUpdate'=>false,
            'columns' => array(
                ...............
            ),
 ));

Upvotes: 1

Views: 723

Answers (1)

Justinas
Justinas

Reputation: 43481

1) Make sure you have enabled rules for search on filterable fields.

[model]
public function rules() {
   return array(
        'id, first_name, last_name, /*...*/', 'safe', 'on' => 'search',
   );
}

2) Don't call CActiveDataProvider in controller (you are not passing any filtering data there, bdw), use model's search() method (put ordering and pagination params there too).

[controller]
$model = new Salaries('search'); //pass scenario!
/* 
 * Will assign any filtering attributes to model attributes,
 * if there is no filtering attributes than empty array will be
 * assigned for attributes (default value)
 */
$model->attributes = Yii::app()->request->getParam('Scenario', []); 

[view]
$this->widget('GridView', array(
    'id' => 'my-grid',
    'dataProvider' => $model->search(), // Where your filtering will take effect.
    'filter' => $model,

Upvotes: 1

Related Questions