Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Yii2 with array of data and a Gridview with sorting and filter

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 ?

like image 668
Ydakilux Avatar asked Feb 10 '15 09:02

Ydakilux


1 Answers

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;
    }
}
  1. Use strtolower() in both places($item['email']) & mailfilter) function if you want your filter to be not case sensitive
  2. 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
like image 108
harsams Avatar answered Nov 30 '22 20:11

harsams