【问题标题】:CGridView functionality of searching constants搜索常量的 CGridView 功能
【发布时间】:2014-03-15 19:12:15
【问题描述】:

我有来自数据库的常量 id,我正在使用以下代码在我的 CGridView 中显示它们

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

现在当我通过 CGridview 搜索时,它只按数字搜索,即 1 或 2。我需要按 Yes 或 No 搜索。可以吗?

【问题讨论】:

    标签: php yii cgridview


    【解决方案1】:

    假设您的模型是用户并且您正在显示该表的 CGridView,在 UserController 中获取您的参数,如下所示:-

    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);
        }
    }
    

    使用它,您可以通过“是”或“否”搜索“已删除”字段

    更新:-

    您可以使用以下代码在 CGridView 中将下拉菜单用作过滤器

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

    这将为您提供一个下拉列表,其中包含在数组中指定的过滤器。

    【讨论】:

    • 很好!但这并没有扩展 CGridView 的智能搜索功能。
    • @Namelus 检查智能搜索功能的更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多