Answer the question
In order to leave comments, you need to log in
How to prevent scripts from reconnecting after ajax request in yii2?
On my page using yii2-editable ( demos.krajee.com/editable ) several records from the database are displayed. And there is a button for ajax adding a new entry. Using renderAjax, I update the list, but at the same time, repeated scripts are connected, and all yii2-editables stop working. How can you disable the addition of unnecessary scripts and how is yii generally thought out to refresh the page after an Ajax request?
Answer the question
In order to leave comments, you need to log in
I usually make an ajax request to some action in the controller, where I process the data and issue an updated page using renderPartial, it is returned without layout, which just does not generate additional scripts.
Akellacom says it all, but all this can be done in one controller - why do we need to produce another identical controller with the same logic, but with a partial page rendering. That is, you need to check whether Ajax is a request or not. If Ajax, then render only the view itself (renderPartial), if it is not Ajax, then render the entire page along with the headers.
In Yii1 it was done like this:
$viewFile = 'viewFile';
$viewData = array('model' => $model,);
if(Yii::$app->request->isAjaxRequest){
$this->renderPartial($viewFile, $viewData);
}else{
$this->render($viewFile, $viewData);
}
for posterity)) just disable bundles that are not needed
if (Yii::$app->request->isAjax) {
Yii::$app->assetManager->bundles = [
'yii\bootstrap\BootstrapPluginAsset' => false,
'yii\bootstrap\BootstrapAsset' => false,
'yii\web\JqueryAsset' => false
];
return $this->renderAjax('create', [
'model' => $model,
]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question