Yii2: EmptyDataProvider

Created on 4 Dec 2014  路  27Comments  路  Source: yiisoft/yii2

there are situations, where it is necessary, to show, initially, empty datagrid .
After the user to select required filters, then it will execute query.

Currently, it is necessary to execute a query that not return data.

I don't think it is necessary to execute this query, knowing that any data it will returned?

How can we solve this problem?

enhancement

Most helpful comment

Yes, but today, i can't.
Could be tomorow?

All 27 comments

Maybe ArrayDataProvider w/ empty array will solve a problem?

The problem, that ArrayDataProvider does not configure labels of columns

You can specify columns instead of relying on what's got from database.

But, this way, i will lose the functionality attributeLabels from ActiveRecords

No. Why?

'columns' => [
    [
        'attribute' => 'title',
       // do not override label or value here!
    ]
]

Currently, does not working when DataProvider is empty

it should be possible to call setModels([]), setKeys([]) and setTotalcount(0) to avoid triggering any query.

I think necessary creates the property autoload into BaseDataProvider

When autoload is false then it will execute what @cebe told

I think we could introduce a method forceEmpty or setEmpty as a shortcut for the above method calls.

Good idea. i think better setEmpty()

The label problems was solved, but the how to show, initially, empty datagrid whit any dataprovder?

Use the approach suggested by @samdark
It should work as described now

It's working, but the issue is trigger query to show empty grid.
Currently, is necessary to write something like:

$query = Person::find()->where(['id' => null]); // this is not necessary, but, currently, it's the unique way

$dataProvider = new ActiveDataProvider([
     'query' => $query     
]);

Well yeah...

what do you suggest @leandrogehlen ?

@cebe suggested create a method:

public function forceEmpty()
{
    $this->setModels([]); 
    $this->setKeys([]); 
    $this->setTotalcount(0);
}

But this is the solution to ArrayDataProvider.

i think better to add autoload property into BaseDataProvider, then all provider can have this feature

can you make a pr?

Yes, but today, i can't.
Could be tomorow?

@leandrogehlen yup, its not obligatory :+1:

I was trying to implement this feature, but i think that the implementation made by @SilverFire resolved the issue.

public function actionIndex()
{
    if (Yii::$app->request->get('empty', false)) {
        $dataProvider = new ArrayDataProvider([
            'modelClass' => Person::className()
        ]); 
    } else {
        $query = Person::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query
        ])
    }      

    return $this->render('index', [
        'dataProvider' => $dataProvider
    ]);
}

@leandrogehlen how about doing that on the ModelSearch::search() method?

you can count the number of results with $query->count() if its zero, then return the ArrayDataProvider else the ActiveDataProvider

But the intention is to avoid query trigger.
Currently, when result of query is empty, this featrue is working

Relates to #12390

Perhaps there should be an option inside yii\db\Query which can turn off the actual query running as it will not return any result anyway instead of using 1=0 condition.

Possible fix: #12708

@klimov-paul I think is an excelent solution

Was this page helpful?
0 / 5 - 0 ratings