Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynagrid's pageSize configuration value not applied when using ArrayDataProvider #141

Closed
antoniom opened this issue Nov 24, 2016 · 2 comments

Comments

@antoniom
Copy link
Contributor

The problem:

Suppose that you have the following code:

$query = new yii\db\Query;
$provider = new yii\data\ArrayDataProvider([
    'allModels' => $query->from('Employee')->all(),
    'pagination' => [
        'pageSize' => 10,
    ],
]);

echo \kartik\dynagrid\DynaGrid::widget([
    'columns' => ['EmployeeID', 'UserName'],
    'gridOptions'=>[
        'dataProvider'=>$provider,
        'panel'=>[
            'before'=>'{dynagrid}'
        ],
    ],
    'options' => ['id' => 'dynagrid-demo']
]);

As you can see the default pageSize set to ArrayDataProvider is 10. The GridView rendered is the following:
screenshot from 2016-11-24 11 39 39

If someone clicks the Grid Configuration options and change the page size from 10 to 5, he would expect the items per page to be reduced at 5.
screenshot from 2016-11-24 11 46 41

However the rows per page stay the same, i.e. the new configuration value is never applied.

Possible Solution:

After some research on the DynaGrid class I found that getAttributeLabel() method is forcing ArrayDataProvider to populate its models before the new Pagination object is applied, i.e.

$models = $provider->getModels();
if (($model = reset($models)) instanceof Model) {
    return $model->getAttributeLabel($attribute);
} else {
   return Inflector::camel2words($attribute);
}

A possible fix could be to invalidate the provider's contents by calling refresh(), i.e.

$models = $provider->getModels();
$provider->refresh();
if (($model = reset($models)) instanceof Model) {
    return $model->getAttributeLabel($attribute);
} else {
   return Inflector::camel2words($attribute);
}

I can provide a PR for the above fix, or you may come up with a more elegant solution.

@kartik-v
Copy link
Owner

Sure please submit a PR... and let know if it works fine on your tests.

@metola
Copy link

metola commented Jan 3, 2017

Hi, I have same problem and fix with $provider->refresh(); in getAttributeLabel function from dynagrid.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants