Yii ClistView Pagination set number of page shown in Pager - yii

Hi think this is a quite easy problem, but I can't find the solution in the docs. I'm using Yii ClistView to paginate my results and everything works great, simply (as I could have a lot of results) I would like to limit non the result but the page shown in the navigation (that are ten by default) to let's say 4. But I won't limit the number of my results, just when I reach the 4th page the navigation will slide (as its default behavior) to show page 5, 6, ecc...
Is this possible? Does anyone have advice?
Thanks in advance!

How about setting pager's maxButtonCount like this:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
// ...
'pager' => array('maxButtonCount' => 4),
));

Related

Yii2 Gridview get all selected row for all pagination

I wrapped my gridview with Pjax widget like this
\yii\widgets\Pjax::begin();
gridview
\yii\widgets\Pjax::end();
in order to make the gridview make ajax request when I click on each pagination.
I also use ['class' => 'yii\grid\CheckboxColumn'], in column as well.
and I find that when I'm on first pagination I checked some rows and then go to second page and check some rows but when I go back to first page what I've checked is gone.
My question is how can I keep all checkedrow for all pagination
With current conditions (Pjax, multiple pages, yii\grid\CheckboxColumn) it's impossible because of the way it works.
When you click on the pagination links all GridView html content is replaced by new one that comes from the AJAX response.
So obviously all selected checkboxes on the previous page are gone.
Few possible ways to solve that:
1) Write custom javascript and server side logic.
As one of the options, you can send AJAX request to server with parameter meaning that user has chosen to select all data for the bulk delete operation (or use separate controller action for bulk deletion). In this case actually we don't need to get the selected data from user, because we can simply get them from database (credits - Seng).
2) Increase number of displayed rows per page.
3) Use infinite scroll extension, for example this.
4) Break desired action in several iterations:
select needed rows on first page, do action (for example, delete).
repeat this again for other pages.
You can get selected rows like that:
$('#your-grid-view').yiiGridView('getSelectedRows');
[infinite scroll] : http://kop.github.io/yii2-scroll-pager/ will work good if you do not have any pjax filters. If you have filters also in play, do not use this plugin as it does not support pjax filters with it. For rest of the applications it is perfect to use.
Update1 : it seems to be straight forward than expected, here is the how I accomplished it
Add following lines to the checkbox column
'checkboxOptions' => function($data){
return ['id' => $data->id, 'onClick' => 'selectedRow(this)'];
}
Now add following JS to the common js file you will have in your project of the page where this datagrid resides
var selectedItems=[]; //global variable
/**
* Store the value of the selected row or delete it if it is unselected
*
* #param {checkbox} ele
*/
function selectedRow(ele){
if($(ele).is(':checked')) {
//push the element
if(!selectedItems.includes($(ele).attr('id'))) {
selectedItems.push($(ele).attr('id'));
}
} else {
//pop the element
if(selectedItems.includes($(ele).attr('id'))) {
selectedItems.pop($(ele).attr('id'));
}
}
}
Above function will store the selected row ids in the global variable array
Now add following lines to pjax:end event handler
$(document).on('pjax:end', function () {
//Select the already selected items on the grid view
if(!empty(selectedItems)){
$.each(selectedItems, function (index,value) {
$("#"+value).attr('checked',true);
});
}
});
Hope it helps.
I just solved this problem and it works properly with Pjax.
You may use my CheckboxColumn. I hope this can help. The checked items are recorded with cookies.
You can read the part with //add by hezll to understand how to fix it, because I didn't provide a complete general one.
Hope it works for you.
https://owncloud.xiwangkt.com/index.php/s/dGH3fezC5MGCx4H

How to set items per page dropdown in grid view for yii

I want to set items per page dropdown in yii gridview.
What changes i need to do this? Has anyone had implemented this type of functionality before?
You have to add summaryText entry to the gridview as below.
$pageSize=Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider_countries,
'summaryText'=>'Displaying {start}-{end} of {count} result(s). ' .
CHtml::dropDownList(
'pageSize',
$pageSize,
Yii::app()->params['pageSizeOptions'],
array('class'=>'change-pageSize')) .' rows per page',
Result is similar to following.

Yii CListView pager stretches to much

I used Yii CListView widget to paginate thru results.
I have a container div with position:relative and float:left.
I have 2 div's, each with float:left inside the container.
The problem is that if I have to many pages with results, the seccond div falls under the first div.
I need them both, on the same line, as ishown in image 2.
Does yii's clistview have a property that sets the maximum number of pages to be echoed in the pager?
I don't need the clistview to print the pages from 60 to 69; from 60 to 65 is enought, so a limit of 6 should be enought.
Any ideas?
You have to set maxButtonCount attribute of CLinkPager to 5. The CLinkPager attributes can be specified through pager attribute of CListView
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_item_view',
'pager'=>array(
'class'=>'CLinkPager',
// This attribute species how many buttons are displayed
'maxButtonCount'=>5
),
.....,
.....
));
you could just pass it in the pager property and it'd override the default value :'pager' => array(
'maxButtonCount' => 5
),

Hidden Pagination summary text

I use CGridView in Yii to create tables.
I would like to show my table with pagination but hide the summary text at the top, which indicates the number of page restance (Displaying 1-4 of 4 results.)
Is that possible?
thank you
Sorry for my English
There is a template option. By default it equals {summary}\n{items}\n{pager}
If you override it in your gridview config, you'l be able to remove summary section:
$this->widget(
'zii.widgets.CGridView',
array(
Your options here ...
'template' => '{items}\n{pager}',
)
);
Another option is to set the CGridView summaryText value to false
Because YII uses CListView, it also add a certain summary class CSS in the asset folder. So to combat this, simply override the css.
Add this to your CSS.
#yw0>.summary{
display:none;
}

Change the Row Color Based on the Column value in CGridView

In Yii, CGridView has it's own background color in the row. But what I want to do is highlight particular row based on the value of one of the column.
For Instance, I have three column, id, name, status. Now, If the Value of status is Inactive or 0, I should highlight the row with some color.
I read the class reference briefly and searched this site as well. But could not find the relevant solution. If some example or some direction toward the right solution, that would be much appreciated.
Thanks,
Ujjwal
CGridView 'rowCssClassExpression' is the way to get what you want.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'rowCssClassExpression'=>'($data->myFlag==0)?"normal":"especial"',
'columns'=>array(
...
),
));
You can also call a custom php function, and pass the $data variable to it. That function should return the class name for the given row :)
Use rowCssClass and rowCssClassExpression for your functionality. I did not tested this code but the trick you can use to get your solution.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'rowCssClass'=>array('odd','even'),
'rowCssClassExpression'=>($data->status==0)?even:odd,
'columns'=>array(
),
));