yii2 pjax in the listview fails in pagination - php

I have a listview that i would like use pagination using pjax but the pagnation fails to work with pjax
This is what i have tried
<?php Pjax::begin([
'id' => 'w0', //checked id on the inspect element
'enablePushState' => true // i would like the browser to change link
]); ?>
<?= ListView::widget([
'summary' => false,
'dataProvider' => $dataProvider,
'emptyText' => "",//Yii::t('app', 'Sorry we have no items currently.'),
'itemOptions' => ['class' => 'item'],
'options' => ['data-pjax' => true ], //this is for pjax
'layout' => '{items}{pager}',
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
'maxButtonCount' => 5,
'options' => [
'class' => 'pagination col-xs-12'
]
],
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('_items_list', ['model' => $model]); //This shows one model
},
]) ?>
<?php Pjax::end(); ?>
When i click on the items nothing hapens and when i remove the pjax opening and closing tags it works with normal page refresh what else do i need to add

You are close to your solution.
<?php Pjax::begin([
'id' => 'w0', // checked id on the inspect element
'enablePushState' => true // I would like the browser to change link
'timeout' => 10000 // Timeout needed
]); ?>
I have added only 1 line: 'timeout' => 10000. By default timeout is equal to 1000 (one second). Since you do not get back results within that 1 second, browser uses normal page refresh instead of Pjax. By replacing 1000 to 10000, you give 10x more time to get response (it shouldn't take that long but just in case). Now Pjax has enough to retrieve results from server and update ListView without reloading the page.
If you still get nothing, it is likely to be an error on server side. Check network (in browser) to see what server returns (it should return status 200).

Related

Yii2: How to configure bootstrap modal to work with escape key and Select2?

I have a Yii2 Modal widget and I need to close the modal when escape key is pressed.
<?= Modal::widget([
'id' => 'modal_view',
'options' => [
'tabindex' => false, // important for Select2 to work properly
]
]) ?>
I found the Bootstrap Modal Options and the clientOptions but it didn't work:
<?= Modal::widget([
'id' => 'modal_view',
'size' => 'modal-lg',
'options' => [
'tabindex' => false, // important for Select2 to work properly
],
'clientOptions' => ['backdrop' => 'static', 'keyboard' => true]
]) ?>
For the ESC key to work correctly you need to set the tabindex="-1" for the bootstrap modal.
Don't know if you are assigning it false for the select2 to work correctly is because of layout issues? because for me it works correctly with -1. See image
So change your code to
<?= Modal::widget([
'id' => 'modal_view',
'size' => 'modal-lg',
'options' => [
'tabindex' => "-1",
],
'clientOptions' => ['backdrop' => 'static', 'keyboard' => true]
]) ?>
You can see this discussion for details.
EDIT
The issue you were facing is related to the focus.Bootstrap registers a listener to the focusin event which checks whether the focused element is either the overlay itself or a descendent of it
You can fix this by overwriting the enforceFocus function which registers the event on the modal.
Add the following line of javascript in your view
Bootstrap 3
$js=<<<JS
$.fn.modal.Constructor.prototype.enforceFocus = function() {};
JS;
$this->registerJs($js, View::POS_READY);
Bootstrap 4
$js=<<<JS
$.fn.modal.Constructor.prototype._enforceFocus = function() {};
JS;
$this->registerJs($js, View::POS_READY);

show only one content in yii widget

Hai I am trying to display some products view on yii if some condition is applied i have to limit contents in the following code:
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'item'],
'itemView' => function ($model, $key, $index, $widget) {
return Html::a(Html::encode($model->name), ['view', 'id' => $model->id]);
},
]) ?>
I had added something like
pagination' => [
'pageSize' => 1,
],
But unable to get the desired result when i use pagination I am getting error
Setting unknown property: yii\widgets\ListView::pagination
how to limit content in view? is it possible?
I think you did mistake. You have to use it in form:
...
'pager' => ['pagination'=> ['pageSize' => 1]],
...
pagination is under the pager option

Yii2 Pjax reloads entire page on second form submit

I cannot seem to figure out why pjax reloads the page on the second form submission. It works exactly as intended on the first form submission pulling from this url /site/profile?UserSearch%5Bsearchstring%5D=mi&_pjax=%23form-pjax, however, after that first one it loses the ending of the url /site/profile?UserSearch%5Bsearchstring%5D=m. I checked the actual html code and the form is retaining the data-ajax attribute. I tried increasing the timeout as had been suggested when pjax is reloading the entire page, but that did not change anything.
Below is the code from my view
<?php Pjax::begin(['timeout' => 5000, 'id' => 'form-pjax']); ?>
<?php $form = ActiveForm::begin([
'method' => 'get',
'action' => Url::to(['site/profile']),
'options' => ['data-pjax' => true ],
]); ?>
<?= $form->field($searchModel, 'searchstring', [
'template' => '<div class="input-group">{input}<span class="input-group-btn">' .
Html::submitButton('Search', ['class' => 'btn btn-default']) .
'</span></div>',
])->textInput(['placeholder' => 'Find friends by username or email']);
?>
<?php ActiveForm::end(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'username',
[
'label' => 'View Profile',
'format' => 'raw',
'value'=>function ($data) {
return Html::a(Html::encode("View Profile"),'/site/redirectprofile/'.$data->id);
},
],
[
'label' => 'Follow',
'format' => 'raw',
'value'=>function ($data) {
return Html::a(Html::encode(Follow::find()->where(['user_id' => $data->id, 'follower_id' => Yii::$app->user->Id])->exists() ? 'Unfollow' : 'Follow'),'/site/follow/'.$data->id.'/'.$data->username);
},
],
],
'summary'=>'',
]); ?>
<?php Pjax::end(); ?>
You must call needed javascript triggers/functions after every pjax reload like:
$('#form-pjax').on('pjax:success', function() {
/*call Your functions here, or copy from your generated page js code,
that bind event to pjax form,
at end of generated html You should have something like: */
jQuery(document).on('submit', "#form-pjax form[data-pjax]", function (event) {
jQuery.pjax.submit(event, '#form-pjax', {"push":true,"replace":false,"timeout":000,"scrollTo":false});
});
});
This is not yii2 issue, it's how javascript works, when You add dynamically content it's needed to bind triggers/events for every added element.

Yii Framework: Pagation Callback Method

I have a CListView on a page and every time i navigate to page 2 or any other page I want to call a method that formats the view. But it do not seem to work every time I navigate to another page.
The Javascript method I want to call is called updateDivs()
Here is my list view widget
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'itemView' => '_customview',
'id' => 'bannerslist',
'ajaxUpdate' => true,
'afterAjaxUpdate' => ' updateDivs()',
'enablePagination' => true,
'itemsCssClass' => 'row banners-list',
'summaryText' => 'Total ' . $pages->itemCount . ' Results Found',
'pager' => array(
'header' => '',
'prevPageLabel' => '<',
'nextPageLabel' => '>',
),
));
Please try this to test, it should works well.
'afterAjaxUpdate' => 'js:function(id,data){alert("test");}'
So if this code will works perfect then you should search problem in your updateDivs() function
Your problem is that, you missed to write js:function(id, data) which is significant to call a method.
You can use as follows :
'afterAjaxUpdate'=>'js:function(id, data) {updateDivs();}',
Look here for more info.

Yii 2 Render Page Inside Tab Content

I have a Tabs widget:
echo Tabs::widget([
'items' => [
[
'label' => 'Add Staff',
'icon' => 'user',
'content' => "Add Staff page loaded here",
'active' => true
],
[
'label' => 'Store Configuration',
'content' => 'Store Configuration page loaded here',
//'headerOptions' => [...],
'options' => ['id' => 'myveryownID'],
],
[
'label' => 'Transaction',
'items' => [
[
'label' => 'Add Transaction',
'content' => 'Add Transaction page loaded here',
],
[
'label' => 'View Transaction',
'content' => 'View Transaction page loaded here',
],
],
],
],
]);
How do I render a page (without reloading the entire page, if possible) inside a Tab content? Is it possible? I tried this:
'content' => "<?= $this->render('createbizstaff', ['searchModel' => $searchModel,'dataProvider' => $dataProvider,]); =>"
But it only generates this error:
Getting unknown property: yii\web\View::render
If you have any idea how to deal with this, please let me know.
You are trying to pass a PHP expression where a string is required. yii\web\View::render() returns a string so your code should read:
'content' => $this->render('createbizstaff', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]),
try using Pjax widget. you can load a part of that content without reloading the whole page. but it will reload the whole page when it reach its timeout and it is still loading.
check this guy's tutorial. http://blog.neattutorials.com/yii2-pjax-tutorial/

Categories