Yii2 GridView与ArrrayDataProvider搜索

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii2 GridView与ArrrayDataProvider搜索相关的知识,希望对你有一定的参考价值。

我需要有关ArrayDataProvider的搜索模型的帮助。假设我有一个数组:

$cities = [
    ['city' => "Chicago", 'year' => 1984],
    ['city' => "Washington", 'year' => 2001],
    ['city' => Manchester", 'year' => 1997],
    //and so on...
];

我创建了一个ArrayDataProvider:

$provider = new yiidataArrayDataProvider([
    'allModels' => $catalog,
    'sort' => [
        'attributes' => ['city', 'year'],
    ],
]); 

然后我创建一个GridView:

echo yiigridGridView::widget([
        'dataProvider' => $provider,
        'filterModel' => (new LibrarySearchModel()),
        'columns' => $columns,
        'showHeader' => true,
        'summary' => false,
    ]);

一切正常,但我需要在GridView中进行过滤。没有选项可以使用ActiveDataProvider,我找不到任何教程如何过滤ArrayDataProvider中的数据。有人可以帮助我使用过滤器模型的代码或为我的案例推荐文档吗?

答案

这是如何在ArrayDataProvider中使用GridView和过滤器的示例。

让我们创建简单的动作。

public function actionExample()
{
    $data = new appmodelsData();
    $provider = $data->search(Yii::$app->request->get());

    return $this->render('example', [
        'provider' => $provider,
        'filter' => $data,
    ]);
}

这是GridView的经典Yii 2方法,因此我不会解释它(您可以在上面链接的指南中找到详细信息)。

现在看来。

<?php

echo yiigridGridView::widget([
    'dataProvider' => $provider,
    'filterModel' => $filter,
    'columns' => [
        'name',
        'code',
    ],
]);

同样,与ActiveDataProvider方法没什么不同。正如您在此处所见,我们期待两列:namecode - 这些将在下面定义。

Data模型。

准备将处理数据源的模型。注释中给出了解释。

<?php

namespace appmodels;

use yiiaseModel;

/**
 * Our data model extends yiiaseModel class so we can get easy to use and yet 
 * powerful Yii 2 validation mechanism.
 */
class Data extends Model
{
    /**
     * We plan to get two columns in our grid that can be filtered.
     * Add more if required. You don't have to add all of them.
     */
    public $name;
    public $code;

    /**
     * Here we can define validation rules for each filtered column.
     * See http://www.yiiframework.com/doc-2.0/guide-input-validation.html
     * for more information about validation.
     */
    public function rules()
    {
        return [
            [['name', 'code'], 'string'],
            // our columns are just simple string, nothing fancy
        ];
    }

    /**
     * In this example we keep this special property to know if columns should be 
     * filtered or not. See search() method below.
     */
    private $_filtered = false;

    /**
     * This method returns ArrayDataProvider.
     * Filtered and sorted if required.
     */
    public function search($params)
    {
        /**
         * $params is the array of GET parameters passed in the actionExample().
         * These are being loaded and validated.
         * If validation is successful _filtered property is set to true to prepare
         * data source. If not - data source is displayed without any filtering.
         */
        if ($this->load($params) && $this->validate()) {
            $this->_filtered = true;
        }

        return new yiidataArrayDataProvider([
            // ArrayDataProvider here takes the actual data source
            'allModels' => $this->getData(),
            'sort' => [
                // we want our columns to be sortable:
                'attributes' => ['name', 'code'],
            ],
        ]);
    }

    /**
     * Here we are preparing the data source and applying the filters
     * if _filtered property is set to true.
     */
    protected function getData()
    {
        $data = [
            ['name' => 'Paul', 'code' => 'abc'],
            ['name' => 'John', 'code' => 'ade'],
            ['name' => 'Rick', 'code' => 'dbn'],
        ];

        if ($this->_filtered) {
            $data = array_filter($data, function ($value) {
                $conditions = [true];
                if (!empty($this->name)) {
                    $conditions[] = strpos($value['name'], $this->name) !== false;
                }
                if (!empty($this->code)) {
                    $conditions[] = strpos($value['code'], $this->code) !== false;
                }
                return array_product($conditions);
            });
        }

        return $data;
    }
}

此示例中的过滤由array_filter函数处理。两列都经过过滤“数据库LIKE”样式 - 如果列值包含搜索到的字符串,则不会从源中删除data数组行。

为了使它像ActiveDataProvider中的and条件一样工作,我们在$conditions数组中放入每列检查的布尔结果,并在array_filter中返回该数组的产品。

array_product($conditions)相当于写$conditions[0] && $conditions[1] && $conditions[2] && ...

这都导致了具有两列的可过滤和可排序的GridView小部件。

以上是关于Yii2 GridView与ArrrayDataProvider搜索的主要内容,如果未能解决你的问题,请参考以下文章

Yii2 GridView CheckboxColumn

YII2 Gridview

Yii2 - Gridview 播放下一首歌曲

yii2 gridview 怎么加checkbox

Yii2 GridView 按年份过滤日期

YII2 Gridview 批量删除