Search code examples
phpyiicgridview

CGridView functionality of searching constants


I have constants id coming from database and i am displaying them in my CGridView using following code

'columns'=>array(
    'id', 
    'name',
    'email',
    array(
        'name' => 'deleted', 
        'value' => '$data->deleted == 1 ? "Yes" : "No"',//like this 
     ),
)

Now when i search through CGridview, it only searches by number i.e 1 or 2. I need to search by Yes or No. Is it possible?


Solution

  • Supposing your model is User and you are displaying that table's CGridView, in the UserController get your parameter like this :-

    if(isset($_GET['User'])){
        $model->setAttributes($_GET['User']);
    }
    if(isset($_GET['User']['deleted'])){
        $value = $_GET['User']['deleted'];
        if($value == 'Yes' || $value == 'yes'){
            $model->setAttribute('deleted', 1);
        }
        if($value == 'No' || $value == 'no'){
            $model->setAttribute('deleted', 0);
        }
    }
    

    Using this you can search the field 'deleted' by 'Yes' or 'No'

    Update :-

    You can use the following code to have a dropdown menu as filters in CGridView

    $filter = array(
        '0' => 'No',
        '1' => 'Yes'
    );
    ...
    array(
        'name' => 'deleted', 
        'value' => '$data->deleted == 1 ? "Yes" : "No"',
        'filter' => $filter
    ),
    

    This will give you a dropdown with filters specified in the array.