简体   繁体   中英

yii2 render after post-request

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,
    ]);
}

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);
    });
});

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);
public function actionLocating()
{
    $searchModel = new ViewMachinesSearch();
    $dataProvider1 = $searchModel->search(Yii::$app->request->queryParams);
    $dataProvider1->pagination = false;
    $model = new TrackingForm();

    if (Yii::$app->request->post()){
        $model->machine_select = Yii::$app->request->post('keylist');
        $machines = $model->machine_select;
    }else{
        $machines = $model->getMachines();
    }

    if(!in_array(Yii::$app->user->identity->company_id, Yii::$app->CompaniesByUser->getCompaniesByUser(21))){
        $query = New Query;
        $query  ->select(['licenceplate', 'identifier', 'timestamp', 'lat', 'lng'])
                ->from('gps_live')
                ->where(['machine_id' => $machines])
                ->orderBy(['timestamp' => SORT_DESC]);
    }else{
        $subquery = New Query;
        $subquery   ->select(['licenceplate', 'identifier', 'timestamp', 'lat', 'lng'])
                    ->from('btc_m1_v1_1')
                    ->join('INNER JOIN', 'machine', 'machine.id_machine = machine_id')
                    ->where(['machine_id' => $machines])
                    ->andWhere('lat <> 0')
                    ->andWhere('lng <> 0')
                    ->orderBy(['timestamp' => SORT_DESC]);
        $query = New Query;
        $query  ->from(['sub' => $subquery])
                ->groupBy(['sub.licenceplate'])
                ->all();
    }

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

you can create some function on your base controller to make sure it was rendered as an ajax or not, the code seems like this.

protected function renderIsAjax($view, $params = [])
{
    if (Yii::$app->request->isAjax) {
        return $this->renderAjax($view, $params);
    } else {
        return $this->render($view, $params);
}

then in your controller just call that method

return $this->renderIsAjax('locating', [
    'model' => $model,
    'searchModel' => $searchModel,
    'dataProvider1' => $dataProvider1,
    'dataProvider' => $dataProvider,
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM