金色木叶枫 2015-02-04 16:33:22 25793次浏览 14条评论 31 5 0

Yii2.0在GridView中下拉过滤筛选的实现

细微的方便也许对于我们的用户来说将会获得最好的体验,用最方便,最快捷,最易操作的方式实现用户需要的功能是我们的工作和职责,今天分享一个在Yii2.0在GridView中下拉过滤筛选的实现,希望能够大家带来一点点的帮助和建议,不说废话了,直接看demo吧

如下是文章管理列表页中如何实现的demo

view层代码

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'id',
         [
            'attribute' => 'category',
            'label'=>'栏目',
            'value'=>
             function($model){
                  return  Article::get_type_text($model->category);   //主要通过此种方式实现
                },
            'filter' => Article::get_type(),     //此处我们可以将筛选项组合成key-value形式
         ],

        'title',
        [
            'attribute' => 'uid',
            'label'=>'管理员',
            'value'=>
            function($model){
                return  Article::get_uid_type_text($model->uid);   //主要通过此种方式实现
            },
        ],

        [
            'attribute' => 'updatetime',
            'label'=>'更新时间',
            'value'=>
            function($model){
                return  date('Y-m-d H:i:s',$model->updatetime);   //主要通过此种方式实现
            },
            'headerOptions' => ['width' => '170'],
        ],

        ['class' => 'yii\grid\ActionColumn', 'header' => '操作'],
    ],
]); ?>

model层代码

/**
* 将栏目组合成key-value形式
*/
public static  function  get_type(){
    $cat = Category::find()->all();
    $cat = ArrayHelper::map($cat, 'id', 'name');
    return $cat;
}

/**
* 通过栏目id获得栏目名称
* @param unknown $id
* @return Ambigous <unknown>
*/

public static  function  get_type_text($id){
    $datas = Category::find()->all();
    $datas = ArrayHelper::map($datas, 'id', 'name');
    return  $datas[$id];

}

好了至此我们的基本功能就已经实现了,功能很简单但是却很有用。

原文链接地址

觉得很赞
  • 评论于 2015-02-04 21:09 举报

    很好,留个名,以后忘记了来看看!

    1 条回复
    评论于 2015-02-04 21:37 回复

    呵呵, 谢谢关注,欢迎常来!

  • 评论于 2015-02-09 10:46 举报

    一直想整个2.0的下拉菜单,终于看到了这篇教程,想问下:filter这种配置在哪里发现的啊,一直感觉学习方法不对,望指点,GridView没有看到啊

    1 条回复
    评论于 2015-07-02 15:35 回复

    这个东西是对列设置, 在DataColumns这个类里面, 有filter属性, 如果是未赋什, 直接生成个INPUT 如果是array生成个SELECT, 如果是字符串, 直接返回字符串, 如果为false不生成过滤表单对象

  • 评论于 2015-02-10 11:03 举报

    标记一下,以后可能会用得着

  • 评论于 2015-05-29 15:26 举报

    没有效果,是哪里出错了吗

  • 评论于 2015-06-26 17:01 举报

    filter不能是匿名函数,可以是字符串、数组、布尔值。

        /**
         * @var string|array|boolean the HTML code representing a filter input (e.g. a text field, a dropdown list)
         * that is used for this data column. This property is effective only when [[GridView::filterModel]] is set.
         *
         * - If this property is not set, a text field will be generated as the filter input;
         * - If this property is an array, a dropdown list will be generated that uses this property value as
         *   the list options.
         * - If you don't want a filter for this data column, set this value to be false.
         */
        public $filter;
    
  • 评论于 2015-11-06 10:36 举报

    感谢分享!!!

  • 评论于 2016-03-08 10:51 举报

    实现还是有点问题,不知道什么情况

  • 评论于 2016-04-22 11:47 举报

    我的怎么有没有下拉框选择的效果

  • 评论于 2016-08-08 09:31 举报

    非常感谢分享

  • 评论于 2016-09-25 10:27 举报

    这个分页有问题, 比如 当前页是10页, 如果下拉搜索选择条件改变 不会自动跳转到第一页开始 而是从当前页开始,哪位有解决 请说下 非常感谢!

  • 评论于 2016-09-27 17:05 举报

    请问这个提示怎么写呢? value=0 的时候 请选择类型

  • 评论于 2017-05-16 09:44 举报

    这是什么问题

  • 评论于 2017-05-16 09:45 举报

    PHP Fatal Error – yii\base\ErrorException

    Call to undefined method backend\models\MCnArea::get_type()

  • 评论于 2019-02-12 11:31 举报

    解决了,Thanks♪(・ω・)ノ

您需要登录后才可以评论。登录 | 立即注册