web-dev-qa-db-ja.com

データの配列でYii2を使用し、並べ替えとフィルターでグリッドビューを使用する

私は配列を持っています

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

データプロバイダー:

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

そしてGridview:

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

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

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

    ]
]);

このコードにより、配列がグリッドで表示され、列をクリックしたときに配列を並べ替えることができます。それで大丈夫です。

しかし、フィルタリングを使用するにはどうすればよいですか?

私は次のことを試しました:

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

    ]
]);

しかし、それは機能していません。 $ get値に応じて自分でオブジェクトをフィルタリングする必要がありますか?

9
Ydakilux

完全なコードを使用した私のソリューション:

$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

以前の魂について。フィルター、列、および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']
            ]
    )
]);
1
Chris Hemmens

データプロバイダー:

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
蔡正海