Using Yii2 with array of data and a Gridview with sorting and filter
On the previous soultion. I created a loop to make the filters, columns and 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']
]
)
]);
here are some improvements for the filtering function
function ($item) {
$mailfilter = strtolower(Yii::$app->request->getQueryParam('filteremail', ''));
if (strlen($mailfilter) > 0) {
return strpos(strtolower($item['email']), $mailfilter) !== false;
} else {
return true;
}
}
- Use strtolower() in both places($item['email']) & mailfilter) function if you want your filter to be not case sensitive
- Check also type for strpos() ("!== false" instead of "!= false") function, otherwise, it will not work while trying to filter for the first characters of the string
My solution with full code :
$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]"),
];
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',
]
]
]);