简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM