Yii2 GridView 与 ArrayDataProvider 搜索 [英] Yii2 GridView with ArrrayDataProvider search
问题描述
我需要有关 ArrayDataProvider 搜索模型的帮助.假设我有一个数组:
I need help with search model for ArrayDataProvider. Let's say i have an array:
$cities = [
['city' => "Chicago", 'year' => 1984],
['city' => "Washington", 'year' => 2001],
['city' => Manchester", 'year' => 1997],
//and so on...
];
我创建了一个 ArrayDataProvider:
I create an ArrayDataProvider:
$provider = new \yii\data\ArrayDataProvider([
'allModels' => $catalog,
'sort' => [
'attributes' => ['city', 'year'],
],
]);
然后我创建一个GridView:
Then I create a GridView:
echo \yii\grid\GridView::widget([
'dataProvider' => $provider,
'filterModel' => (new LibrarySearchModel()),
'columns' => $columns,
'showHeader' => true,
'summary' => false,
]);
一切正常,但我需要在 GridView 中进行过滤.没有使用 ActiveDataProvider 的选项,我找不到任何关于如何在 ArrayDataProvider 中过滤数据的教程.有人可以帮我编写过滤器模型的代码或推荐我的案例的文档吗?
All works fine, but i need a filtering in GridView. There is no option to use ActiveDataProvider and I cant find any tutorial how to filter a data in ArrayDataProvider. Can someone help me with code for filter model or recomend the docs for my case?
推荐答案
这是如何使用 ArrayDataProvider 在 GridView.
This is example of how to use ArrayDataProvider with filters in the GridView.
public function actionExample()
{
$data = new \app\models\Data();
$provider = $data->search(Yii::$app->request->get());
return $this->render('example', [
'provider' => $provider,
'filter' => $data,
]);
}
这是 GridView 的经典 Yii 2 方法,所以我不会解释它(您可以在上面链接的指南中找到详细信息).
This is classic Yii 2 approach to the GridView so I will not explain it (you can find details in the Guide linked above).
<?php
echo \yii\grid\GridView::widget([
'dataProvider' => $provider,
'filterModel' => $filter,
'columns' => [
'name',
'code',
],
]);
同样,与 ActiveDataProvider 方法没有什么不同.正如您在这里看到的,我们期待两列:name
和 code
- 这些将在下面定义.
Again, nothing different from the ActiveDataProvider approach. As you can see here we are expecting two columns: name
and code
- these will be defined below.
准备将处理数据源的模型.注释中给出了解释.
Prepare the model that will handle the data source. Explanation is given in the comments.
<?php
namespace app\models;
use yii\base\Model;
/**
* Our data model extends yii\base\Model 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 \yii\data\ArrayDataProvider([
// 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
数组行.
The filtering in this example is handled by the array_filter function. Both columns are filtered "database LIKE"-style - if column value contains the searched string the data
array row is not removed from the source.
为了使它像 ActiveDataProvider 中的 和
条件一样工作,我们将每列检查的布尔结果放入 $conditions
数组中,并在 array_filter 中返回该数组的乘积代码>.
To make it work like and
conditions in ActiveDataProvider we put boolean result of every column check in the $conditions
array and return product of that array in array_filter
.
array_product($conditions)
相当于编写 $conditions[0] &&$conditions[1] &&$conditions[2] &&...
这一切都会产生具有两列的可过滤和可排序的 GridView 小部件.
This all results in the filterable and sortable GridView widget with two columns.
这篇关于Yii2 GridView 与 ArrayDataProvider 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!