作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
1984], -6ren">
我需要有关 ArrayDataProvider 搜索模型的帮助。假设我有一个数组:
$cities = [
['city' => "Chicago", 'year' => 1984],
['city' => "Washington", 'year' => 2001],
['city' => Manchester", 'year' => 1997],
//and so on...
];
$provider = new \yii\data\ArrayDataProvider([
'allModels' => $catalog,
'sort' => [
'attributes' => ['city', 'year'],
],
]);
echo \yii\grid\GridView::widget([
'dataProvider' => $provider,
'filterModel' => (new LibrarySearchModel()),
'columns' => $columns,
'showHeader' => true,
'summary' => false,
]);
最佳答案
这是如何使用 ArrayDataProvider 的示例在 GridView 中带有过滤器.
让我们创建简单的 Action 。
public function actionExample()
{
$data = new \app\models\Data();
$provider = $data->search(Yii::$app->request->get());
return $this->render('example', [
'provider' => $provider,
'filter' => $data,
]);
}
<?php
echo \yii\grid\GridView::widget([
'dataProvider' => $provider,
'filterModel' => $filter,
'columns' => [
'name',
'code',
],
]);
name
和
code
- 这些将在下面定义。
Data
模型。
<?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;
}
}
data
数组行不会从源中删除。
and
一样工作ActiveDataProvider 中的条件我们将每列检查的 bool 结果放入
$conditions
数组并在
array_filter
中返回该数组的乘积.
array_product($conditions)
相当于写
$conditions[0] && $conditions[1] && $conditions[2] && ...
关于php - Yii2 GridView 与 ArrayDataProvider 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49073640/
我正在使用 ArrayDataProvider,我想知道如何在 View 中创建排序链接,就像$sort->link('date')在 yii/data/Sort 最佳答案 关注 this (yii\
我正在尝试使用 ArrayDataProvider 填充我的 listView。但是 dataProvider 由数组组成,而不是对象。我遇到了这个问题,因为模型中的类别是一个 id,我需要另一个表中
我需要有关 ArrayDataProvider 搜索模型的帮助。假设我有一个数组: $cities = [ ['city' => "Chicago", 'year' => 1984],
使用 Yii2 我需要提供一个包含特定数据的 DataProvider。问题是我需要使用数组数据和数据库表中的数据创建一个 DataProvider。 因此,我认为我应该混合使用 ArrayDataP
我是一名优秀的程序员,十分优秀!