11

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 ?

2
  • What do you mean by "it's not working"? The search inputs are not shown? Commented Feb 10, 2015 at 9:54
  • What I mean by not working is that the input are show only if I put the filter like in the sample. 'filter' => '<input class="form-control" name="filteremail" value="da" type="text">', 'value' => 'email', But the data are not filtered. Commented Feb 10, 2015 at 10:45

4 Answers 4

9

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',
            ]

    ]
]);
Sign up to request clarification or add additional context in comments.

2 Comments

Just to be clear. Filters will not be shown if the filter is not instanceof Model
Use "not identical" (!==) instead of "non equal" (!=) in filter function, to avoid wrong filtering, see stpos
2

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']
            ]
    )
]);

Comments

1

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

Comments

0

A dataprovider :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.