Answer the question
In order to leave comments, you need to log in
How to remove multiple requests in ajax?
I am using ajax in yii2. I implemented that when you click in the gridciew, a block comes out, you can view both the order itself, and you can click on edit and go inside the block to edit.
Only when you do a lot of editing and again click on another line so that it opens, then too many requests come out and later it slows down and does not work correctly. Another warning comes out
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/ .
<?php $this->registerJs('$("body").on("click", "#edit", function(){//нажал на кнопку редактировать, происходит событие
var key = $(this).data("key");//получаем id заказа
console.log(key);
$.ajax({
url: "'.Url::toRoute(['zakaz/zakazedit']).'?id="+key,//переводим страницу _zakazedit
success: function(html){
$(".view-zakaz").html(html);
}
})
});') ?>
<?= Html::button('Редактировать', ['id' => 'edit', 'data-key' => $model->id_zakaz]) ?>
<?php $this->registerJs('
$("body").on("click", "#view", function(){//переводит ползователя на просмотр заказа
var key = $(this).data("key"); //получает id заказа от data-key
$.ajax({
url: "'.Url::toRoute(['zakaz/zakazold']).'?id="+key, // открывает данную страницу просмотра _zakazold
success: function(html){
$(".edit-zakaz").html(html);
}
})
});
$("body").on("click", "tr", function(){//если была нажата на другую строку, то информация меняется на другие данные, но есть проблема, то что куда бы не нажал в блоке, но все равно открывает страницу _zakazold
var key = $(this).data("key");//получаем id заказа
$.ajax({
url: "'.Url::toRoute(['zakaz/zakazold']).'?id="+key,
success: function(html){
$(".edit-zakaz").html(html);
}
})
})
') ?>
<?php $form = ActiveForm::begin(); ?>
....
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
<?= Html::button('Вернуться', ['id' => 'view', 'data-key' => $models->id_zakaz]) ?>
public function actionZakazedit($id){
$models = $this->findModel($id);
if($models->load(Yii::$app->request->post()) && $models->save()){
return $this->redirect(['admin']);// если было нажата кнопка сохранить , то сохраняется данные и редирект на странbцу admin
} else {
return $this->renderAjax('_zakazedit', ['models' => $models]);//иначе открывает внутри блока страницу _zakazedit
}
}
public function actionZakazold($id){
$model = $this->findModel($id);
return $this->renderAjax('_zakazold', ['model' => $model]);
}
Answer the question
In order to leave comments, you need to log in
It is impossible to answer your question, you need to look at your code and site. All requests are in the console, see what requests they are, what code launches them, and look for an opportunity to reduce them.
Because it is impossible to understand without a fortuneteller what kind of requests there are, whether they are needed. whether there are duplicates or just not optimally done.
PS: when asking a question, go away from your case and move on to abstractions when explaining. Sometimes it is better to break a question into several questions. And when you introduce concepts like
support with explanations and code. You brought js. but where there "do editing" only you know.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question