S
S
Sergey Beloventsev2016-10-19 10:10:03
Yii
Sergey Beloventsev, 2016-10-19 10:10:03

What is the correct way to organize the use of Pjax in a widget?

I'm trying to create a comment creation widget here is actually
Comment.php

namespace frontend\widget\comments;
use common\models\Comment;
use yii\base\Widget;
use Yii;
class Comments extends Widget{
    public $goodsId;
    public function init(){
        parent::init();
    }
    public function run(){
        $commentmodel= new Comment();
        $comments=Comment::find()->where(['id_gods'=>$this->godsId])->all();
        if ($commentmodel->load(Yii::$app->request->post())) {
            $commentmodel->save();
        }
        return $this->render('html',[
            'commentmodel'  =>$commentmodel,
            'comments'      =>$comments
        ]);
    }
}

here is views/html
<section>
       <?= $this->render('_form',['commentmodel'=>$commentmodel]) ?>
</section>
<section>
    <?php Pjax::begin(['id' => 'reloded']) ?>
             <p class="description"><?= $comment->text ?></p>
<?php Pjax::end();
$this->registerJs(
    '$("document").ready(function(){
        $("#new_relode").on("pjax:end", function() {
            $.pjax.reload({container:"#reloded"});  //Reload GridView
        });
    });');
?>
</section>

here is views/_form
<?php use yii\widgets\ActiveForm;
use yii\widgets\Pjax;
use yii\helpers\Html;
Pjax::begin(['id' => 'new_relode']);
$form = ActiveForm::begin(['method'=>'post']]);
?>
     <?= $form->field($commentmodel, 'text',['options'=>['class'=>'text col-lg-10 col-md-10 col-sm-12 col-xs-24']])->textarea()->label(false); ?>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>

like this, I insert it on the page , I press the button and the page is completely updated, do not tell me why?
<?= Comments::widget(['godsId'=>$models->id]);?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-10-20
@webinar

Try setting pjax to a very long timeout.
By default, timeout = 1000, so if it doesn't get a response within 1 second, it sends a regular get request.
Most likely this is the issue.
You can check like this:

Pjax::begin([
'id' => 'new_relode',
'timeout' => 1000000,
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question