使用Yii2与数据的数组,并与分选一个GridView和滤波器
问题描述:
我已经阵列使用Yii2与数据的数组,并与分选一个GridView和滤波器
$resultData = [
array("id"=>1,"name"=>"Cyrus","email"=>"[email protected]"),
array("id"=>2,"name"=>"Justin","email"=>"[email protected]"),
array("id"=>3,"name"=>"Mason","email"=>"[email protected]"),
array("id"=>4,"name"=>"Fulton","email"=>"[email protected]"),
array("id"=>5,"name"=>"Neville","email"=>"[email protected]"),
array("id"=>6,"name"=>"Jasper","email"=>"[email protected]"),
array("id"=>7,"name"=>"Neville","email"=>"[email protected]"),
array("id"=>8,"name"=>"Neville","email"=>"[email protected]"),
array("id"=>9,"name"=>"Ronan","email"=>"[email protected]"),
array("id"=>10,"name"=>"Raphael","email"=>"[email protected]"),
];
甲数据提供器:
$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值过滤自己的对象?
答
我的全代码解决方案:
$resultData = [
array("id"=>1,"name"=>"Cyrus","email"=>"[email protected]"),
array("id"=>2,"name"=>"Justin","email"=>"[email protected]"),
array("id"=>3,"name"=>"Mason","email"=>"[email protected]uacorci.ca"),
array("id"=>4,"name"=>"Fulton","email"=>"[email protected]"),
array("id"=>5,"name"=>"Neville","email"=>"[email protected]"),
array("id"=>6,"name"=>"Jasper","email"=>"[email protected]"),
array("id"=>7,"name"=>"Neville","email"=>"[email protected]"),
array("id"=>8,"name"=>"Neville","email"=>"[email protected]"),
array("id"=>9,"name"=>"Ronan","email"=>"[email protected]"),
array("id"=>10,"name"=>"Raphael","email"=>"[email protected]"),
];
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 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',
]
]
]);
+0
只是要清楚。 如果过滤器不是instanceof Model,则不会显示过滤器 – 2016-08-30 13:29:06
答
在以前soultion。我创建了一个循环来创建过滤器,列和searchModel。
$items = [
array("id" => 1, "name" => "Cyrus", "email" => "[email protected]"),
array("id" => 2, "name" => "Justin", "email" => "[email protected]"),
array("id" => 3, "name" => "Mason", "email" => "[email protected]"),
array("id" => 4, "name" => "Fulton", "email" => "[email protected]"),
array("id" => 5, "name" => "Neville", "email" => "[email protected]"),
array("id" => 6, "name" => "Jasper", "email" => "[email protected]"),
array("id" => 7, "name" => "Neville", "email" => "[email protected]"),
array("id" => 8, "name" => "Neville", "email" => "[email protected]"),
array("id" => 9, "name" => "Ronan", "email" => "[email protected]"),
array("id" => 10, "name" => "Raphael", "email" => "[email protected]"),
];
$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']
]
)
]);
答
一个数据提供程序:
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'],
],
]);
你说的 “它不工作” 是什么意思?搜索输入未显示? – arogachev 2015-02-10 09:54:17
我不工作的意思是,只有当我像过滤器一样放置过滤器时才会显示输入。 'filter'=>'', 'value'=>'email',但数据未被过滤。 – Ydakilux 2015-02-10 10:45:00