【问题标题】:Yii2 how to send controller action response without redirect?Yii2 如何在不重定向的情况下发送控制器动作响应?
【发布时间】:2018-01-22 12:47:51
【问题描述】:

我在 Yii2 中有一个单页应用程序

主页包含 1.带有对象列表的列+创建按钮 2. 创建/更新表单的动态内容栏

我的问题是在加载(通过 ajax)创建表单时,然后我通过 AjaxSubmitButton 发送数据,这很好(数据库更新),但不是用新对象数据更改动态内容区域,而是重定向到视图部分表格。

我怎样才能使新响应进入发送它的同一个 div?

这是现在在#singleObjContainer 中加载的表单

    <div class="course-form">

<?php 
    $form = yii\bootstrap\ActiveForm::begin(); 
?>

<span>
    <h4 class='listTitle'>Add Course</h4> 
</span>
<span class="form-group pull-right">
    <?php 

        AjaxSubmitButton::begin([
            'label' => 'Save',
            'ajaxOptions' => [
                'type'=>'POST',
                'url'=> Url::toRoute(['course/create-form']),
                'success' => new \yii\web\JsExpression('function(html)
                {
                    $("#singleObjContainer").empty().append(html);
                }'),
            ],
            'options' => ['class' => 'btn btn-success', 'type' => 'submit'],
            ]);

        AjaxSubmitButton::end();
    ?>
</span>

<hr class='myHr'>

<div>
<?php

    echo $form->field($model, 'name')->textInput(['maxlength' => true]);

    echo $form->field($model, 'description')->textarea(['rows' => '4']);

    echo $form->field($model, 'img')->label(Html::img(Yii::getAlias('@uploadsUrl/').'courses/'.$model->img,['width' => '30%']),['encodeLabel' => false])->fileInput();

?>
</div>

<?php ActiveForm::end(); ?>

这是控制器动作:

    public function actionCreateForm()
{
    $model = new Course();


    if ( $model->load(Yii::$app->request->post())) 
    {
        // get the instance of the image
        $image = UploadedFile::getInstance( $model, 'img');

        // save image name to the model
        $model->img = $image->baseName.".".$image->extension;

        // go through validation rule and save
        if( $model->save() )
        {
            // validation ok, save image in the file system
            $image->saveAs( Yii::getAlias('@uploads/').'courses/'.$model->img);

            // load the details view into the singleObj container

            $courseStudents = $this->getCourseStudents($model['id']);

            return $this->renderPartial('_viewForm', ['model' => $model, 'courseStudents' => $courseStudents]);


        }
    }
    else 
    {
        return $this->renderPartial('_createForm', ['model' => $model,]);
    }


}

【问题讨论】:

  • 而不是使用renderPartial 使用renderAjax

标签: ajax yii2 single-page-application renderpartial


【解决方案1】:

如果您需要将结果直接发送到 ajax 请求 .. 只需返回 .. 最终使用数组的 json_encode。

  return json_encode( $model);

通过这种方式,您可以获得与 $model 等效的结构的 json 答案

在您的控制器中添加适当的操作

public function actionRefreshForm()
{
  //your appllication code 
   $my_new_html_code ='<div>test new html code </div>;
   return $my_new_html_code;

}

在您的 ajaxOptions 中调用相关操作

 'ajaxOptions' => [
            'type'=>'POST',
            'url'=> Url::toRoute(['course/refresh-form']),
            'success' => new \yii\web\JsExpression('function(html)
            {
                $("#singleObjContainer").empty().append(html);
            }'),
        ],

【讨论】:

  • 响应正常。问题是它重定向页面,我希望它留在原始页面中并更新指定 div 中的响应内容
  • 更好地解释你的评论..你是什么意思?
  • 问题是它重定向页面,我希望它留在原始页面并更新指定div中的响应内容
  • 您正在调用相同的控制器/操作来显示页面和更新 html 。 ???如果不想要重定向/刷新..你应该分开渲染html更新的动作......使用不同的控制器(动作..一个用于创建页面......另一个用于发送新的html
  • 你能举一个简单的例子来说明如何做到这一点吗??
【解决方案2】:

您可以使用yii\widgets\Pjax。我想这对你来说很容易。您可以在本文中了解有关它的更多信息。 http://blog.neattutorials.com/yii2-pjax-tutorial.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    相关资源
    最近更新 更多