Reputation: 177
Not working filter elements in the CGridView. Tell me how to fix this? For example, you must select a category, choose "Baby", but nothing happens. SiteController
public function actionIndex()
{
$model = new Page;
$dataProvider=new CActiveDataProvider('Page', array(
'criteria'=>$criteria,
'sort' => array(
'defaultOrder'=>array(
'created'=>"DESC"
)),
'pagination'=>array(
'pageSize'=>30,
),
));
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->render('index', array('dataProvider'=>$dataProvider,'model'=>$model));
}
index.php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'enableSorting'=>true,
'filter'=>$model,
'columns' => array(
'title' => array(
'name'=>'title',
'type'=>'raw',
'value'=>'CHtml::link($data->title,Yii::app()->request->baseUrl."/page/".$data->id)',
'headerHtmlOptions' => array('style'=>'width:250px;'),
),
'created' => array(
'name' => 'created',
'value' => 'date("j.m.Y H:i", $data->created)',
'headerHtmlOptions' => array('style'=>'width:90px;'),
'type'=>'raw',
),
'category_id' => array(
'name' => 'category_id',
'value' => '$data->category->title',
'filter' => Category::allCategory(),
'headerHtmlOptions' => array('style'=>'width:150px; text-align:center;'),
'type'=>'raw',
'sortable'=>TRUE
),
'subcategory_id' => array(
'name' => 'subcategory_id',
'value' => '$data->subcategory->title',
'filter' => Subcategory::allCategory(),
'headerHtmlOptions' => array('style'=>'width:150px; text-align: center;'),
'type'=>'raw',
),
'condition_id' => array(
'name' => 'condition_id',
'value'=> '$data->condition->title',
'filter' => Condition::allAttributes(),
'headerHtmlOptions' => array('style'=>'width:90px;'),
'type'=>'raw',
),
),
));
Thanks in advance for answers and assistance.
Upvotes: 0
Views: 2099
Reputation: 3103
You do not load the POST/GET data from the request, and that is why you do not get any filtered values. There are more approaches to this, I show you a simple one i use mostly;
in your controler do this:
$model = new Page;
if(isset($_GET['Page']))
$model->attributes =$_GET['Page'];
$dataProvider = $model->search();
In you Page
model, find the search
function, and adapt your custom Dataprovider there (or make a copy ot the search and use it later)
That should do the job. The important part is - you have to set the attributes somewhere, from the GET request.
Upvotes: 0
Reputation: 6296
Use a dataprovider that will respond to your query.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(), // <== search() function in your model */
'enableSorting'=>true,
'filter'=>$model,
'columns' => array(
)
);
Then in your model
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('field1',$this->field1,true);
$criteria->compare('field2',$this->field2);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'field1 ASC',
),
'pagination'=>array(
'pageSize'=>20
),
));
}
Upvotes: 2