yii2在请求后渲染 [英] yii2 render after post-request

查看:124
本文介绍了yii2在请求后渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有checkboxcolumn和google-map(2amigos)的gridview的视图.用户通过选择网格视图中的相应项目来选择要在地图中显示的机器.一个html按钮将所选的键提交给控制器.遵循控制代码(通过后请求更新dataProvider):

I have a view with a gridview with checkboxcolumn and a google-map (2amigos). The user selects the machines to display in the map by selecting the corresponding items in the gridview. A html-button submitts the selected keys to the controller. Following the controler code (dataProvider gets updated by post-request):

//some code before render   
$dataProvider = new ActiveDataProvider(['query'=>$query, 'pagination'=>false]);
return $this->render('locating', [
    'model' => $model,
    'searchModel' => $searchModel,
    'dataProvider1' => $dataProvider1,
    'dataProvider' => $dataProvider,
    ]);
}

这是html按钮的js脚本:

And this is the js-script for the html-button:

$('#showButton').click(function(){
    var keys = $('#w0').yiiGridView('getSelectedRows');
    $.post("http://localhost:8080/eddb1/frontend/web/index.php?r=tracking/locating",
    {'keylist': keys},
    function(data){
        console.log(data);
    });
});

问题是,对于post请求,php-render命令的结果将以html字符串的形式写入到post-response中,但视图不会重新呈现.如果我使用activeForm而不是gridview(选择机器)和一个submit-button来做同样的事情,页面将按照其应有的方式重新渲染(但是这种方式对我不起作用,因为我需要带有filter和排序).我该怎么做才能在请求后重新渲染地图(或视图)?

The problem is, that with the post request the result of php-render command gets writen as a html-string to the post-response but the view doesn't get re-rendered. If I do the same with an activeForm instead of a gridview (to select the machines) and a submit-button the page gets re-rendered as it should (but this way doesn't work for me because I need the gridview with filter and sorting). What can I do that the map (or the view) gets re-rendered with the post-request?

推荐答案

尝试使用

Try to use renderAjax method to render your view when it is requested via ajax. I assume that you use actionLocating to render your view for page get request and for ajax. So before you call render in your controller, check if this is ajax request or not:

$renderMethod = Yii::$app->request->isAjax ? 'renderAjax' : 'render';

return $this->$renderMethod('locating', $viewParams);

这篇关于yii2在请求后渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆