V
V
varenik1632016-11-30 19:47:54
Yii
varenik163, 2016-11-30 19:47:54

yii2 and pjax comments. Why doesn't the container update?

The idea is to add comments without reloading the page. I wrapped the form in pjax, the output too. When you click on the link below (Reply), a cloned form (the same as above) is displayed. When sending a comment, the form is simply removed for some reason. Below is an example of a controller and a view. Guys, tell me what's wrong?
It's view

<?php Pjax::begin([ //форма отправки
        'id' => 'new_com',
        'enablePushState' => false,
        'timeout' => 5000,
        'linkSelector' => '.btn-custom'
    ]) ?>

    <?php $form2 = ActiveForm::begin(['options' => ['data-pjax' => true,'class' => 'comm_tree']]); ?>

    <?= $form2->field($comment, 'message')->textarea(['rows' => 3]) ?>
    <?= $form2->field($comment, 'parent_id')->hiddenInput()->label(false) ?>

    <div class="form-group">
        <?= Html::submitButton('Добавить комментарий', ['class' => 'btn btn-success']) ?>
    </div>
    <?php ActiveForm::end(); ?>
    <?php Pjax::end() ?>


    <?php Pjax::begin([ 'id' => 'task_comments'])  //вывод дерева?>
    <?php $is_child = ''; $lavel = 0;
    if($commentsTree):
    foreach ($commentsTree as $cmt):
        if(!is_object($cmt)){
            var_dump($cmt);
            continue;
        }
        if($cmt->parent_id) {
            $is_child = 'is_child';
            $lavel++;
        }else  $is_child = '';
        $created_at = Yii::$app->formatter->asDatetime($cmt->created_at, "php:d.m.Y")
        ?>
        <div class="comment <?= $is_child ?>" style="margin-left: <?= $cmt->lavel*20 ?>px">
            <a name="comment<?=$cmt->id?>"></a>
            <p><span class="data-grey"><?=$created_at?></span><strong><?= $cmt->user->username ?></strong></p>
            <p><?= str_replace("\n", '<br>', $cmt->comment) ?></p>
            <p><?=Html::a(Yii::t('app', 'Reply'), ['task/view'],['class' => 'com-reply', 'data-parent_id' => $cmt->id])?></p>
            <hr>
        </div>
    <?php endforeach; endif;?>
    <?php Pjax::end() ?>

This is the controller
public function actionView($id)
    {
        \Yii::$app->user->can('learnerAccessTheTask', ['id' => $id]);

        $task = Task::findOne(['id' => $id]);

        $model = UserTask::findOne(['task_id' => $id, 'user_id' => \Yii::$app->user->id]);


        if (!$model){
            $model = new UserTask([
                'status' => UserTask::STATUS_NEW,
                'task_id' => $task->id,
            ]);
            $model->setScenario(UserTask::SCENARIO_INITIAL);
            
            $model->save();

        }

        $commentsTree = CommentsTree::makeTree('UserTaskComment',$model->id,0,0);
        
        $comment = new CommentForm([
            'modelId' => $model->id
        ]);

        if ($model->load(\Yii::$app->request->post()) && $model->complete(\Yii::$app->request->post('status'))) {

            \Yii::$app->session->setFlash('success', 'Сохранено');
            
            
            
            return $this->redirect(['view', 'id' => $model->task_id]);
        }
        
        if ($comment->load(\Yii::$app->request->post()) && $comment->addTaskComment()) {

            \Yii::$app->session->setFlash('success', 'Комментарий добавлен');
            //здесь был рефреш
        }

        return $this->render('view', [
            'task' => $task,
            'model' => $model,
            'comment' => $comment,
            'commentsTree' => $commentsTree,
        ]);
    }

This is JS
$("#new_com").on("pjax:end", function() {
        $.pjax.reload({container:"#task_comments"});
    });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-12-02
@qonand

To be honest, you are not implementing the task quite correctly, from the point of view of architecture and code cleanliness. You have a list of comments and a form - so separate the functionality, you don't have to do everything in one action. Not only will people who will work with the project after you have to dig into this heap of code, but you yourself cannot debug this code even at the current stage. Make a separate action responsible for displaying the list of comments, and a separate action that receives data from the form and saves it. And just pull consistently action games. From the code that is now hard for me to say what the error is, but judging by our discussions in the comments, I can conclude that the problem is saving a new comment - either it is not saved at all, or it is saved with incorrect data. Try to debug saves step by step.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question