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.
Related
As the title said, I want in some case to hide or disable the breadcrumbs in my Yii2 app.
Is there any general solution or should I put code in any of my view?
I eventually find this solution :
First add a param in the app config/params.php : 'ariane'
return [
'adminEmail' => 'admin#example.com',
'bsVersion' => '3.x',
'ariane' => false,
];
then add code to empty the links array conditionally in the layout main.php:
<?php
if(isset(Yii::$app->params['ariane']) && Yii::$app->params['ariane'] == false){
$this->params['breadcrumbs'] = [];
}
?>
Why not to put breadcrumb in layout and check it for condition only once.
I mean this way in layout file:
<?php if ($condition) {
Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
]);
} ?>
<?= $content ?> //view file content
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']
]);
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.
I'm trying to make a form that changes the input according to the button pressed, using pjax of yii2, no page loads.
My view
<div id="container-ajax">
<?php Pjax::begin([]);
echo $this->render('noleggia_andata', ['form' => $form, 'noleggi_partenza' => $noleggi_partenza]);
Pjax::end(); ?>
</div>
This is one, I tried with ActionView from Button but do not know how to do, can you give me any suggestions?
View
<?= Html::a("Go", ['noleggi/noleggia_andata'], ['class' => 'btn-discard-orange']) */ ?>
Controller
public function actionNoleggia_andata()
{
$noleggi_partenza = new Noleggi_partenza;
return $this->render('noleggia_andata',['noleggi_partenza' => $noleggi_partenza]);
}
Controller view rederAjax
public function actionNoleggia_andata()
{
$noleggi_partenza = new Noleggi_partenza;
return $this->renderAjax('noleggia_andata',['noleggi_partenza' => $noleggi_partenza]);
}
I am trying render page in grid view, it was success but the footer is show on the column i render, how to remove its
This is the colomn GridView i write :
[ 'format' => 'raw',
'value' => function($model) { return "<p class='feedback'>". $model->KOMENTAR ."</p><br><p class='feedback-date'>". $model->TANGGAL ."</p><hr><div id='replay-". $model->ID_KOMENTAR."'><ul></ul><div id='replay-column'></div></div>";},
],
I render the page inside div class="replay-column"
This the following js i made for rendering page when i click button:
$("#replay-to-"+ID_KOMENTAR).on("click", function(){
$.get('index.php?r=feedback/feedbacktest',null, function(data){
$('#replay-column').append(data);
});
});
And this the controller :
public function actionFeedbacktest(){
echo $this->render('feedback_test');
}