1
votes

I am using CGridView to display results from postgres function. The CGridView is working fine. Now I want to use a different function in the model for filtering the CGridView.

The current code for CGridView is like

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'purchase-grid',
    'itemsCssClass'=>'table table-bordered table-condensed table-hover table-striped dataTable',
    'filter'=>$model,
    'dataProvider'=>$model->search(),
        ..............

The $model->search() function accepts an id which is used to select rows from the table. I am using CSqlDataProvider to run custom query in the $model->search() and returns the dataprovider. If I use the above code it will display filter textbox for all the fields in the CGridView. But the search function is using an id that is not displayed in the CGridView. So filtering is not working. So I want to use a new function for filtering which will accept the field for filtering. I tried with the code below

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'purchase-grid',
    'itemsCssClass'=>'table table-bordered table-condensed table-hover table-striped dataTable',
    'filter'=>$model->filter_search(),
    'dataProvider'=>$model->search(),
        ..............

But it shows error. Please help.

Thanks in advance.

4
What does Your method $model->filter_search() returns? What data type? - gSorry

4 Answers

0
votes

the filter parameter should receive the model. If you do not want to display the filter a specific column should do:

array(
'name'=>'attributeName',
'value'=>'$data->attributeName',
'filter'=>false,
)
0
votes

The filter property in CGridView should be a CActiveRecord.

You can change the filter input like this code

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'item-categoria-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
    'nombre',
    array(
        'id'=>'tipo_id',
        'header'=>'Tipo',
        'value'=>'$data->tipo->nombre',
        'filter'=>CHtml::activeDropDownList(
            $model,
            "tipo_id",
            CHtml::listData(TipoItem::model()->findAll(), 'id', 'nombre'),
            array(
                'empty'=>'(Seleccione uno)',
            )),
    ),
    'cuentaVenta.descripcion:html:Cuenta de Ventas',

Note: the filter is a CActiveRecord, and the filter in the column is a Select

0
votes

CgridView custom filter elements:

View code:

array(
        'name'=>'status',
        'header'=>'Confirmed',
        'type'=>'raw',
        'value'=>'($data->status==0 ? "No" : "Yes")',
        'filter'=>CHtml::listData($pastEventModel->getYesNoCgridviewFilter(), 'id', 'title'),
    ),

Model code:

public function getYesNoCgridviewFilter()
{
    return array(
        array('id'=>0, 'title'=>'No'),
        array('id'=>1, 'title'=>'Yes'),
    );
}

CgridView custom search function:

View code:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'events-grid2',
    'dataProvider'=>$pastEventModel->search("past"),
    'filter'=>$pastEventModel,

Model code:

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

    if($when == "past")
        $criteria->addCondition("start < ". time(), 'AND');
    elseif($when == "upcoming")
        $criteria->addCondition("start >= ". time(), 'AND');

CgridView twice on the same page:

View code:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'events-grid1',

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'events-grid2',

Controller code:

public function actionAdmin()
{       
    $pastEventModel=new Events('search');
    $pastEventModel->unsetAttributes();  // clear any default values
    if(isset($_GET['ajax'],$_GET['Events']) && $_GET['ajax']=="events-grid2")
    {
        $pastEventModel->attributes=$_GET['Events'];
        unset($_GET['Events']);
    }
    $upComingEventModel=new Events('search');
    $upComingEventModel->unsetAttributes();  // clear any default values
    if(isset($_GET['ajax'],$_GET['Events']) && $_GET['ajax']=="events-grid1")
    {
        $upComingEventModel->attributes=$_GET['Events'];
        unset($_GET['Events']);
    }
    $this->render('admin',array(
        'pastEventModel'=>$pastEventModel,
        'upComingEventModel'=>$upComingEventModel,
    ));
}
0
votes

I have hidden the column by using HtmlOptions as given below.

array(name=>'project_id','headerHtmlOptions' => array('style' => 'display:none'),
     'htmlOptions' => array('style' => 'display:none'),'filterHtmlOptions' => array('style' => 'display:none')),

Then I used the same $model->search() function for filtering.