Answer the question
In order to leave comments, you need to log in
Why does a pjax request replace one get parameter with another?
Good afternoon. There is a section with reviews on the company. In section two Select2 from Kartik. One for selecting a company, the second for selecting a review score. Roughly speaking, "Company N", reviews with a rating of 3.
I display reviews using a ListView and after selecting a filter, I update the block using pjax. After selecting a company or rating, the selected value flies to the url, but the problem is that I need the ability to filter by both parameters at the same time - "Reviews for company N with a rating of 3". But now the choice of a value in one list overrides the value of another in the url, i.e. There is only 1 parameter in the url at a time. How can I overcome this problem? Thank you.
<?= Html::beginForm(Url::current(), 'GET', ['id' => 'filter-form']); ?>
<?= Select2::widget([
'name' => 'category_id',
'id' => 'insurance',
'value' => $insurance_id = Yii::$app->request->get('insurance') ? Yii::$app->request->get('insurance') : 'Все компании',
'data' => ArrayHelper::merge(['' => 'Все компании'], ArrayHelper::map(ReviewCategory::find()->all(), 'id', 'title')),
'size' => Select2::SIZE_LARGE,
'pluginOptions' => [
'tags' => true
],
'options' => [
],
]) ?>
<?= Select2::widget([
'name' => 'rating',
'id' => 'rating',
'value' => Yii::$app->request->get('rating') ? Yii::$app->request->get('rating') : 'Любая оценка',
'data' => ArrayHelper::merge(['' => 'Любая оценка'], ['1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5]),
'size' => Select2::SIZE_LARGE,
'pluginOptions' => [
//'tags' => true,
//'id' => 'rating',
],
'options' => [
'id' => 'rating',
],
]) ?>
<?= Html::endForm(); ?>
$('#insurance').on('change', function() {
$.pjax({
timeout: 4000,
url: $('#filter-form').attr('action'),
container: '.my_class_for_summary',
fragment: '.my_class_for_summary',
scrollTo: true,
replace: false,
data: {insurance: this.options[this.selectedIndex].value},
});
});
$('#rating').on('change', function() {
$.pjax({
timeout: 4000,
url: $('#filter-form').attr('action'),
container: '.my_class_for_summary',
fragment: '.my_class_for_summary',
scrollTo: true,
data: {rating: this.options[this.selectedIndex].value},
});
});
Answer the question
In order to leave comments, you need to log in
The answer turned out to be much simpler.
$('select').on('change', function() {
$insurance_id = $('#insurance').val() ?? 0;
$rating = $('#rating').val() ?? 0;
$.pjax({
timeout: 4000,
url: $('#filter-form').attr('action'),
container: '.my_class_for_summary',
fragment: '.my_class_for_summary',
scrollTo: true,
replace: false,
data: {insurance: insurance_id, rating: rating},
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question