i have form with two pjax. (one inside two) when clicked link in second PJAX send query to server but in field "X-PJAX-Container" write ID first container.
Pjax::begin([
'id' => 'register-form'
]);
...
Pjax::begin([
'id' => 'que'
]);
echo yii\helpers\Html::a('update', ['/portal/que/gr-usl/');
Pjax::end();
...
Pjax::end();
location after click update : /portal/que/gr-usl?_pjax=%23register-form
request header
X-PJAX:true
X-PJAX-Container:#register-form
X-Requested-With:XMLHttpRequest
if set data-pjax for link #que request header not changed.
for all link in inside pjax disabled data-pjax an href change to onclick with function
$.pjax({url: $(el).attr('link'), container: '#pjax-grusl'});
links has code
echo yii\helpers\Html::a('update', null,[
'data-pjax' => 'false',
'link' => \yii\helpers\Url::to(['/portal/que/gr-usl/',
'ids' => $ids,
'filter' => $filter,
'gr' => $gr
]),
'onclick' => 'selGr(this)'
]);
but its should work from box :( i don't understand why click by link don't find first pjax container from dom tree.
Related
I used listview with pjax block and this is my view page:
<div class="main-video">
<div>نمایش</div>
<div id="data">
<?php
$model = new ActiveDataProvider([
'query' => FileInfos::find()->where(['type' => 2])->orderBy(['id'=>SORT_DESC]),
'pagination' => [
'pageSize' => 15,
],
]);
Pjax::begin([
'id'=> 'id-pjax',
'enablePushState' => false, // to disable push state
'enableReplaceState' => false, // to disable replace state,
'timeout'=> 999999999
]);
if(isset($model))
echo ListView::widget([
'dataProvider' => $model,
'itemView' => '/partials/_item',
'viewParams' => ['typeName' => 'video'],
]);
Pjax::end();
?>
</div>
<button id="btn-show">نمایش</button>
</div>
And this is my action controller code:
public function actionVideo()
{
return $this->render('video');
}
After click on next page, it's reload on entire page.
in network tab of inspect element, I see two request calls:
1 - video?page=2&per-page=15&_pjax=%23id-pjax
2 - video?page=2&per-page=15
First request no have any response and second request have the rendered view page which reloads to that.
Whats wrong in my code?
How can I fix this issue for have ajax request call for changing page?
This article helped me: https://riptutorial.com/yii2/example/16529/how-to-use-pjax,
So it solved by changing pjax begin block with this code:
Pjax::begin([
'id'=> 'id-pjax-'.uniqid(),
'enablePushState' => false, // to disable push state
'enableReplaceState' => false, // to disable replace state,
'timeout'=> 999999999,
'clientOptions' => ['method' => 'POST']
]);
The export dropdown and toggle dropdown do not appear on click. I can't see any errors in console of developer tool.
I have used exactly same code in another project and that seems to work, the only difference is in the column set.
<?php
$exportColumns=[
'name',
'email',
'username',
];
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' =>$exportColumns,
'dropdownOptions' => [
'label' => 'Export All',
'class' => 'btn btn-secondary'
]
]);
?>
I had a similar issue with my own Yii2 project. In my project the dropdown menu worked, but the buttons that appeared were unresponsive and the entire page froze without any console error.
My own mistake was that I required two bootstrap versions in my composer.json (I left in yiisoft/yii2-bootstrap in my case, removed the other).
Double Bootstrap
I am using Kartik's GridView. I want to show a text in a DataColumn that is an external link. The thing is that I need to have pjax activated in the GridView, but disabled for this particular link. My code is the following:
[
'class'=>'\kartik\grid\DataColumn',
'attribute'=>'idSubject.idnumber',
'value'=>function($model) {
return Html::a($model->getIdSubject()->one()->idnumber ,
['/subject/view','id'=>$model->getIdSubject()->one()->id_subject],
[
'data-pjax'=>0
]);
},
'format'=>'html',
'filter'=>Html::activeTextInput($searchModel, 'idnumber', ['class'=>'form-control']),
'label' => Yii::t('app', 'ID'),
'vAlign' => 'middle'
],
The text link is generated correctly, but without the data-pjax tag. I've put a data-confirm tag as well, and it is ignored. It seems that all extra tags I specify in the link are ignored. However, if I generate the exact same link in an ActionColumn, everything works as expected. Is there a way do add tags to an Html::a element in a DataColumn?
try to Use
[ 'data-pjax' => false ]
Pjax for Alerts
There is alert.php included in layout file above $content. This is as below :
<?php Pjax::begin(['id'=> 'new-alert','enablePushState' => false]); ?>
<?= \odaialali\yii2toastr\ToastrFlash::widget([
'options' => [
'positionClass' => 'toast-bottom-full-width',
//'progressBar' => true,
'timeOut' => 6000,
'extendedTimeOut' => 2000
]
]);?>
<?php Pjax::end(); ?>
And to get flash messages in ajax , there are calls to below container
$.pjax.reload({container:'#new-alert'});
This results in showing of alert messages but also sends an Ajax request to URL whichever page is open. Can we trigger request to "/site/toast" on ajax calls which can renderAjax the above widget inplace of making ajax request on current url ?
Since there are no html "a" tag well as neither any "form" tag here inside widget generated html, Is this correct use of pjax ?
http://localhost:8081/about-us?_pjax=%23new-alert
Pjax for form
Also if we wrap an Active Form inside Pjax, how can we make sure that none of A tags or nested Form trigger a Pjax request except the container form ?
I have removed call for pjax from PHP and made alert as below -
<div id="new-alert">
<?= \odaialali\yii2toastr\ToastrFlash::widget([
'options' => [
'positionClass' => 'toast-bottom-full-width',
//'progressBar' => true,
'timeOut' => 6000,
'extendedTimeOut' => 2000
]
]); ?>
</div>
Instead I am making pjax call from javascript as below -
$.pjax({url: encodeURI(baseUri + "site/toast"), container: '#new-alert', push: false})
public function actionToast()
{
if(Yii::$app->request->getHeaders()->has('X-PJAX'))
{
return $this->renderAjax('//common/alert');
}
else
{
return $this->redirect(Yii::$app->request->referrer);
}
}
$.pjax.reload was previously retrieving additional toasts if there were some for current url as well. Above solution only gets toasts related to ajax.
I have a dropdown list in my form, and I would like to enable the user to an option of 'other', which would enable him to insert a value in to a text box, instead of a value from the dropdownlist.
Is that possible?
<?= $form->field($model, 'institution')->dropDownList(ArrayHelper::map(Institution::find()->all(), 'iId', 'name'), ['class'=>'form-control inline-block']); ?>
The same thing I would like to do with a radioList.
<?= $form->field($model, 'selfDescription')->radioList(array('good'=>'good','very good'=>'very good','best'=>'best')); ?>
thanks.
First. Add "other" option to dropDownList. And handle it in your frontend with JS
$form
->field($model, 'institution')
->dropDownList(
ArrayHelper::map(Institution::find()->all(),
'iId',
'name'
) + ['other' => 'Other'],
[
'class'=>'form-control inline-block',
// next code disables text field when any but "other" option is selected
// works for jQuery 1.6+
'onchange' => new JsExpression('
$("#textFieldSelector").prop("disabled", $(this).find("option:selected").val() != "other");'),
]
);
Then you need to set properly your validators. Just add
['textField', 'required', 'when' => function($model) {
return $model->institution === 'other';
}]