【问题标题】:Yii2. Multiple pjax in the same page. Second not working rightYii2.同一页面中有多个 pjax。二是不能正常工作
【发布时间】:2019-11-21 10:15:44
【问题描述】:

一开始对不起我的英语!

我对 pjax 有疑问。可以在一页中使用多个吗? 我尝试在页面的两个地方使用相同的 pjax 块。块有不同的 id。 第二块不能正常工作:页面刷新!

它像 yii2-widget 一样工作。小部件在页面上启动两次!
我的代码:

   public function run()
   {
      $id_performer = $this->id_performer;
      $id_city = $this->id_city;
      $QueryItems = City::getItemsByPerformer($id_performer, $id_city);

      $countQuery = clone $QueryItems;
      $allCount = $countQuery->count();

      $haveMoreButton = \Yii::$app->params['perPage'] < $allCount;

      $items = $QueryItems->limit(\Yii::$app->params['perPage'])->all();
      return $this->render('items_widget', compact('items', 'haveMoreButton', 'id_performer', 'id_city'));
   }

和一般视图文件:

<?php  $pjax = Pjax::begin(['enablePushState' => false, 'timeout' => false]);
$pjaxId = $pjax->getId();
$pjax->linkSelector = '#link' . $pjaxId;
?>
<?php if (!empty($items)) : ?>
      <?php foreach ($items as $item): ?>
         <?php include __DIR__ . '/../catalog-item.php'; ?>
      <?php endforeach; ?>
      <?php if (isset($haveMoreButton) && $haveMoreButton): ?>
         <div>
            <?= Html::a('Show More', ['/site/pjax-news', 'limit' => (isset($limit) ? $limit : (\Yii::$app->params['perPage'] * 2)), 'id_performer' => (isset($id_performer) ? $id_performer : 0),'id_city' => (isset($id_city) ? $id_city : 0)],
               ['class' => 'btn big dark button_show_more', 'id' => 'link' . $pjaxId, 'data-pjax' => 1]); ?>
         </div>
      <?php endif; ?>
<?php endif; ?>
<?php Pjax::end(); ?>

等待你的想法!谢谢!

【问题讨论】:

  • 我没有看到您在页面上多次使用它的地方,上面的代码只定义了一次Pjax,而且在循环之外也是如此?添加导致问题的实际代码,否则即使他愿意,也没有人能够帮助您
  • @MuhammadOmerAslam 我猜他是想说这段代码是在页面中被调用两次的小部件的一部分。
  • 是的,正如我所说,wiget 被 ItemsWidget::widget() 调用了 2 次;
  • 页面上的两次。我通过 php 和 html 代码检查了 pjax-objects 和 pjax-blocks 具有不同的 id!

标签: php yii yii2 pjax


【解决方案1】:

如果你想在一页中使用两个 pjax,你可以这样做:

Pjax::begin([
 'id' => 'id1',
 'enablePushState' => false,
]); ?>
 //write your code here
 <?php Pjax::end(); ?>
Pjax::begin([
 'id' => 'id2',
 'enablePushState' => false,
]); ?>
 //write another code here
 <?php Pjax::end(); ?>

【讨论】:

    猜你喜欢
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多