SnIpY
SnIpY

Reputation: 662

Yii CGridview not filtering

I'm trying to work with a YII CGridview to display some data.

This is home my model search function looks like:

/**
 * Retrieves a list of models based on the current search/filter conditions.
 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
 */
public function search()
{
    $criteria=new CDbCriteria;
    $criteria->compare('ip',$this->ip,true);
    $criteria->compare('first_use',$this->first_use,true);
    $criteria->compare('last_use',$this->last_use);
    $criteria->compare('memberid',$this->memberid);
    $criteria->compare('countryid',$this->countryid);

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

And this is how my view looks like

$this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'iplog-grid',
        'dataProvider'=>$oIPLog->search(),
        'filter'=>$oIPLog,
        'summaryText' => 'showing you {start} - {end} of {count} logged Ips',
        'columns'=>array(
            array(
                'name'=>'ip',
                'type'=>'raw',
            ),
            array(
                'name'=>'first_use',
                'type'=>'datetime',
            ),
            array(
                'name'=>'last_use',
                'type'=>'datetime',
            ),
        ),
    ));

Displaying the CGridview works, but I can't seem to get the filter on top of it to work. It sends the call and I don't get any error as reponse, it just returns the whole unfiltered data again..

Any clues?

Upvotes: 0

Views: 7319

Answers (2)

Gaurav Parashar
Gaurav Parashar

Reputation: 126

You Have to apply these points: 1.Specify the global variable($_REQUEST) in function of your controller for example

$model = new User('search');

      $model->unsetAttributes();  // clear any default values

        if (isset($_REQUEST['User'])){
            $model->attributes = $_REQUEST['User'];

          }
            $this->render('admin', array(
            'model' => $model,

        ));
  1. Set method type in search form

    <?php $form=$this->beginWidget('CActiveForm', array(
    'action'=>Yii::app()->createUrl('user/admin'),
    'method'=>'POST',
    )); ?>
    

3.In Cgrid view you have to define the url like

 'ajaxUrl'=>Yii::app()->createUrl( 'controller/function' ), 

Upvotes: 0

ThomasVdBerge
ThomasVdBerge

Reputation: 8140

And how exactly does your controller look like?

For the CGridview filter to work you need to check in your controller if there are any filters set and then return the filtered object.

To clarify, something like this should be placed into your controller action

$oObject = new Object('search');
if (isset($_GET['Object'])) {
    $oObject->attributes = $_GET['Object'];
}

Hope this helps

Upvotes: 6

Related Questions