Answer the question
In order to leave comments, you need to log in
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
]);
}
}
<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>
<?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(); ?>
'godsId'=>$models->id]);
= Comments::widget([Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question