web-dev-qa-db-fra.com

Utilisation de Yii2 avec un tableau de données et d’un Gridview avec tri et filtrage

J'ai un tableau

$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]"),     
    ];

Un fournisseur de données:

$dataProvider = new ArrayDataProvider([
        'key'=>'id',
        'allModels' => $resultData,
        'sort' => [
            'attributes' => ['id', 'name', 'email'],
        ],
]);    

Et le Gridview:

echo GridView::widget([
        'dataProvider' => $dataProvider,

        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id',

            [
            'attribute' => 'name', 
            'value' => 'name',
            ],
            [
            "attribute" => "email",
            'value' => 'email',
            ]

    ]
]);

En l'état, le code me fait afficher le tableau dans une grille et la possibilité de le trier en cliquant sur les colonnes. C'est bon. 

Mais comment faire pour utiliser le filtrage?

J'ai essayé avec ce qui suit:

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

    ]
]);

Mais ça ne marche pas. Dois-je filtrer moi-même l'objet en fonction de la valeur $ get?

9
Ydakilux

Ma solution avec le code complet:

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

    ]
]);
7
Ydakilux

Sur la solution précédente. J'ai créé une boucle pour créer les filtres, les colonnes et le modèle de recherche.

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

Un fournisseur de données:

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

0
蔡正海