Evgeniy Tkachenko
Evgeniy Tkachenko

Reputation: 1737

Yii2 Fragment Caching for GridView

http://www.yiiframework.com/doc-2.0/guide-caching-fragment.html

My code is:

$dependency = [
    'class' => 'yii\caching\DbDependency',
    'sql' => 'SELECT MAX(updated_at) FROM converter_operator_device;',
];

$variations = [
   [\Yii::$app->request->absoluteUrl, \Yii::$app->user->isGuest],
];

if ($this->beginCache('table-connection',['dependency' => $dependency, 'variations' => $variations])) {                        
   // here gridView.
}

When the fragment cached then filters of GridView doesn't work, because need js GridView. How fix?

UPD: I add before cache the code:

$this->registerJs("$('#table-connection').yiiGridView({'filterUrl':document.URL,'filterSelector':'#table-connection-filters input, #table-connection-filters select'})");
GridViewAsset::register($this);

This works, but how track the cache load?

Upvotes: 0

Views: 1327

Answers (2)

Rockie
Rockie

Reputation: 1

registerJs is perform in Gridview function "run()" maybe you can seprate gridview like below :

$grid = new GridView([
  'layout'=> "{summary}",
]);
echo $grid->run();

if ($this->beginCache('id')) {                        
  echo $grid->renderItems();
  $this->endCache();
}

In my case I only enable sorting in gridview, it work for me.

Upvotes: 0

Mihai P.
Mihai P.

Reputation: 9357

Interesting one :), try having an else after the if that would register the needed JS. So if your dependencies fail you include the GridView, if they do not fail you have the HTML with the included JS file to make it work.

Upvotes: 0

Related Questions