使用带有数据数组的 Yii2 和带有排序和过滤的 Gridview
Posted
技术标签:
【中文标题】使用带有数据数组的 Yii2 和带有排序和过滤的 Gridview【英文标题】:Using Yii2 with array of data and a Gridview with sorting and filter 【发布时间】:2015-04-10 07:08:37 【问题描述】:我有一个数组
$resultData = [
array("id"=>1,"name"=>"Cyrus","email"=>"risus@consequatdolorvitae.org"),
array("id"=>2,"name"=>"Justin","email"=>"ac.facilisis.facilisis@at.ca"),
array("id"=>3,"name"=>"Mason","email"=>"in.cursus.et@arcuacorci.ca"),
array("id"=>4,"name"=>"Fulton","email"=>"a@faucibusorciluctus.edu"),
array("id"=>5,"name"=>"Neville","email"=>"eleifend@consequatlectus.com"),
array("id"=>6,"name"=>"Jasper","email"=>"lectus.justo@miAliquam.com"),
array("id"=>7,"name"=>"Neville","email"=>"Morbi.non.sapien@dapibusquam.org"),
array("id"=>8,"name"=>"Neville","email"=>"condimentum.eget@egestas.edu"),
array("id"=>9,"name"=>"Ronan","email"=>"orci.adipiscing@interdumligulaeu.com"),
array("id"=>10,"name"=>"Raphael","email"=>"nec.tempus@commodohendrerit.co.uk"),
];
数据提供者:
$dataProvider = new ArrayDataProvider([
'key'=>'id',
'allModels' => $resultData,
'sort' => [
'attributes' => ['id', 'name', 'email'],
],
]);
还有 Gridview:
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
[
'attribute' => 'name',
'value' => 'name',
],
[
"attribute" => "email",
'value' => 'email',
]
]
]);
按原样,代码使我可以在网格中查看数组,并且可以在单击列时对其进行排序。没关系。
但是如何使用过滤呢?
我尝试了以下方法:
$searchModel = ['id' => null, 'name' => '', 'email' => ''];
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
[
'attribute' => 'name',
'value' => 'name',
],
[
"attribute" => "email",
'filter' => '<input class="form-control" name="filteremail" value="da" type="text">',
'value' => 'email',
]
]
]);
但它不起作用。 我是否必须根据 $get 值过滤自己的对象?
【问题讨论】:
“它不工作”是什么意思?搜索输入未显示? 我所说的不工作的意思是,只有当我像在示例中那样放置过滤器时,才会显示输入。 'filter' => '', 'value' => 'email', 但是数据没有被过滤。 【参考方案1】:我的完整代码解决方案:
$resultData = [
array("id"=>1,"name"=>"Cyrus","email"=>"risus@consequatdolorvitae.org"),
array("id"=>2,"name"=>"Justin","email"=>"ac.facilisis.facilisis@at.ca"),
array("id"=>3,"name"=>"Mason","email"=>"in.cursus.et@arcuacorci.ca"),
array("id"=>4,"name"=>"Fulton","email"=>"a@faucibusorciluctus.edu"),
array("id"=>5,"name"=>"Neville","email"=>"eleifend@consequatlectus.com"),
array("id"=>6,"name"=>"Jasper","email"=>"lectus.justo@miAliquam.com"),
array("id"=>7,"name"=>"Neville","email"=>"Morbi.non.sapien@dapibusquam.org"),
array("id"=>8,"name"=>"Neville","email"=>"condimentum.eget@egestas.edu"),
array("id"=>9,"name"=>"Ronan","email"=>"orci.adipiscing@interdumligulaeu.com"),
array("id"=>10,"name"=>"Raphael","email"=>"nec.tempus@commodohendrerit.co.uk"),
];
function filter($item)
$mailfilter = Yii::$app->request->getQueryParam('filteremail', '');
if (strlen($mailfilter) > 0)
if (strpos($item['email'], $mailfilter) != false)
return true;
else
return false;
else
return true;
$filteredresultData = array_filter($resultData, 'filter');
$mailfilter = Yii::$app->request->getQueryParam('filteremail', '');
$namefilter = Yii::$app->request->getQueryParam('filtername', '');
$searchModel = ['id' => null, 'name' => $namefilter, 'email' => $mailfilter];
$dataProvider = new \yii\data\ArrayDataProvider([
'key'=>'id',
'allModels' => $filteredresultData,
'sort' => [
'attributes' => ['id', 'name', 'email'],
],
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
[
'attribute' => 'name',
'value' => 'name',
],
[
"attribute" => "email",
'filter' => '<input class="form-control" name="filteremail" value="'. $searchModel['email'] .'" type="text">',
'value' => 'email',
]
]
]);
【讨论】:
只是为了清楚。如果过滤器不是模型实例,则不会显示过滤器 在过滤函数中使用“不相等”(!==)而不是“不相等”(!=),以避免错误过滤,请参阅stpos【参考方案2】:关于上一个灵魂。我创建了一个循环来制作过滤器、列和 searchModel。
$items = [
array("id" => 1, "name" => "Cyrus", "email" => "risus@consequatdolorvitae.org"),
array("id" => 2, "name" => "Justin", "email" => "ac.facilisis.facilisis@at.ca"),
array("id" => 3, "name" => "Mason", "email" => "in.cursus.et@arcuacorci.ca"),
array("id" => 4, "name" => "Fulton", "email" => "a@faucibusorciluctus.edu"),
array("id" => 5, "name" => "Neville", "email" => "eleifend@consequatlectus.com"),
array("id" => 6, "name" => "Jasper", "email" => "lectus.justo@miAliquam.com"),
array("id" => 7, "name" => "Neville", "email" => "Morbi.non.sapien@dapibusquam.org"),
array("id" => 8, "name" => "Neville", "email" => "condimentum.eget@egestas.edu"),
array("id" => 9, "name" => "Ronan", "email" => "orci.adipiscing@interdumligulaeu.com"),
array("id" => 10, "name" => "Raphael", "email" => "nec.tempus@commodohendrerit.co.uk"),
];
$searchAttributes = ['id', 'name', 'email'];
$searchModel = [];
$searchColumns = [];
foreach ($searchAttributes as $searchAttribute)
$filterName = 'filter' . $searchAttribute;
$filterValue = Yii::$app->request->getQueryParam($filterName, '');
$searchModel[$searchAttribute] = $filterValue;
$searchColumns[] = [
'attribute' => $searchAttribute,
'filter' => '<input class="form-control" name="' . $filterName . '" value="' . $filterValue . '" type="text">',
'value' => $searchAttribute,
];
$items = array_filter($items, function($item) use (&$filterValue, &$searchAttribute)
return strlen($filterValue) > 0 ? stripos('/^' . strtolower($item[$searchAttribute]) . '/', strtolower($filterValue)) : true;
);
echo GridView::widget([
'dataProvider' => new ArrayDataProvider([
'allModels' => $items,
'sort' => [
'attributes' => $searchAttributes,
],
]),
'filterModel' => $searchModel,
'columns' => array_merge(
$searchColumns, [
['class' => 'yii\grid\ActionColumn']
]
)
]);
【讨论】:
【参考方案3】:数据提供者:
if ($this->load($params))
$name = strtolower(trim($this->name));
$resultData= array_filter($resultData, function ($role) use ($name)
return (empty($name) || strpos((strtolower(is_object($role) ? $role->name : $role['name'])),$name) !== false);
);
$dataProvider = new ArrayDataProvider([
'key'=>'id',
'allModels' => $resultData,
'sort' => [
'attributes' => ['id', 'name', 'email'],
],
]);
https://getyii.com/topic/736
【讨论】:
【参考方案4】:这里是过滤功能的一些改进
function ($item)
$mailfilter = strtolower(Yii::$app->request->getQueryParam('filteremail', ''));
if (strlen($mailfilter) > 0)
return strpos(strtolower($item['email']), $mailfilter) !== false;
else
return true;
-
如果您希望过滤器不区分大小写,请在两个地方使用 strtolower()($item['email']) 和 mailfilter) 函数
还要检查 strpos() 函数的类型(“!== false”而不是“!= false”)函数,否则,在尝试过滤字符串的第一个字符时它将不起作用
【讨论】:
以上是关于使用带有数据数组的 Yii2 和带有排序和过滤的 Gridview的主要内容,如果未能解决你的问题,请参考以下文章
如何在带有自定义过滤器的 Spring Data mongodb 中使用分页和排序?