J
J
Julia Kovalenko2016-06-07 12:02:57
Yii
Julia Kovalenko, 2016-06-07 12:02:57

How to specify selection condition in dataProvider in Yii2?

I'm creating a gridView based on an array:

GridView::widget([
        'dataProvider' => new ArrayDataProvider([
            'allModels' => $user['friends']['items'],
            'sort' => [
                'attributes' => ['id', 'username', 'email'],
            ]
        ]),
        'columns' => [
        ...

And I need to select only such $user['friends']['items'], where the condition is met How to do it?
isset($user['friends']['items']['set_phone']);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2016-06-07
@kovalenko_jul_s

DataProvider - not responsible for the selection condition, you need to filter the array before passing it to the DataProvider, for example using array_filter

D
devian3000, 2016-06-07
@devian3000

No need to use array_filter on the DataProvider!
Open the documentation, everything is written there
nix-tips.ru/yii2-razbiraemsya-s-gridview.html
If in an example, then use searchModel!

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'rowOptions'=>function ($model, $key, $index, $grid){
        $class=$index%2?'odd':'even';
        return [
            'key'=>$key,
            'index'=>$index,
            'class'=>$class
        ];
    },
]); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question