, (*1)
Yii2 Alphapager performs alphabetic paging with for instance GridView
s or ListView
s., (*2)
Just use sjaakp\alphapager\ActiveDataProvider
in stead of your normal yii\dataActiveDataProvider
, and put an AlphaPager
widget in front of your GridView
, and you're running., (*3)
Likewise with sjaakp\alphapager\ArrayDataProvider
., (*4)
Alpha paging is compatible with normal pagination. You can use them together., (*5)
A demonstration of Yii2 Aphapager is here., (*6)
Important notice
Yii2 Alphapager's ActiveDataProvider is only proven to work with MySQL and MariaDB databases. It should also work with SQLite and Cubrid, but this is not tested., (*7)
In contrast to the Yii philosophy, Yii2 Alphapager is not transparent to the database, as I'm sorry to say. Presumably, ActiveDataProvider
can be made to work with Oracle, Postgres, and MSSQL as well. See the comments near $regex
in ActiveDataProvider.php
for details. If you can provide more information on using Yii2 Alphapager with other databases, I'd be glad to hear it., (*8)
Installation
The preferred way to install Yii2 Alphapager is through Composer. Either add the following to the require section of your composer.json
file:, (*9)
"sjaakp/yii2-alphapager": "*"
, (*10)
Or run:, (*11)
composer require sjaakp/yii2-alphapager "*"
, (*12)
You can manually install Yii2 Alphapager by downloading the source in ZIP-format., (*13)
Usage
Using Yii2 Alphapager is easy. A minimum usage scenario would look like the following. In PersonController.php
we would have something like:, (*14)
<?php
use sjaakp\alphapager\ActiveDataProvider;
class PersonController extends Controller
{
// ...
public function actionIndex() {
$dataProvider = new ActiveDataProvider([
'query' => Person::find()->orderBy('last_name, first_name'),
'alphaAttribute' => 'last_name'
]);
return $this->render('index', [
'dataProvider' => $dataProvider
]);
}
// ... more actions ...
}
The corresponding view file index.php
could look something like:, (*15)
<?php
use sjaakp\alphapager\AlphaPager;
?>
<?= AlphaPager::widget([
'dataProvider' => $dataProvider
]) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'last_name',
'first_name',
],
]); ?>
Classes
ActiveDataProvider and ActiveArrayProvider
These are extensions from their Yii-counterparts in yii\data
, and can be used in the same way. It is important to set attribute $alphaAttribute
., (*16)
$alphaAttribute
Set this to the name of the attribute which is used to define the pages. Must be set., (*17)
$alphaDigits
Setting for the way attribute values starting with a digit are handled. Can have the following values:, (*18)
-
false
: no special handling; digits are treated just like any other non-alphabetic symbol (default)
-
'full'
: separate pages for each digit
-
'compact'
: one page for all digits
-
array
of characters: alpha pager doesn't display alphabetic characters, but uses the characters in this array; useful for non-western alphabets
$alphaPages
Settings to modify alpha pagers operation. For normal use, this can remain the default value of []
(empty array). For more information, see the _AlphaTrait.php
source., (*19)
$alphaDefault
Default page value; this page opens when no explicit page is given. Default value: 'A'
., (*20)
$alphaParam
The alphapager HTML parameter name. Default value: 'alpha'
. Might be changed if there is a conflict with other functionality., (*21)
This is the widget that renders the actual alphapager. The attribute $dataProvider
must be set., (*22)
$dataProvider
The Yii2 Alphapager ActiveDataProvider
or ArrayDataProvider
that this pager is associated with. Must be set., (*23)
array
Page values of buttons which should appear left of the alphabetical buttons. Set this to []
if you don't want an 'all' button. Default: [ 'all' ]
., (*24)
$postButtons
array
Page values of buttons which should appear right of the alphabetical buttons. Set this to []
if you don't want an '#' (non-alphabetic) button. Default: [ 'symbol' ]
., (*25)
$lowerCase
boolean
Whether the alphabetic buttons are rendered in lower case. Default: false
., (*26)
$options
array
HTML options for the alphapager container tag. Default: [ 'class' => 'pagination' ]
, compatible with Bootstrap., (*27)
array
HTML options for the alphapager buttons. Default: [ 'class' => 'page-item' ]
, compatible with Bootstrap 4., (*28)
$linkOptions
array
HTML options for the alphapager links. Default: [ 'class' => 'page-link' ]
, compatible with Bootstrap 4., (*29)