I've an array
$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]"),
];
A dataprovider :
$dataProvider = new ArrayDataProvider([
'key'=>'id',
'allModels' => $resultData,
'sort' => [
'attributes' => ['id', 'name', 'email'],
],
]);
And the Gridview :
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
[
'attribute' => 'name',
'value' => 'name',
],
[
"attribute" => "email",
'value' => 'email',
]
]
]);
As is, the code make me View the array in a grid, and the possibility to sort it when clicking on columns. That's ok.
But how to do to use filtering ?
I tried with the following :
$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',
]
]
]);
But it's not working. Does I have to filter myself the object depending on the $get value ?
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With