@shyann
Yii is a popular PHP web application framework that provides built-in support for pagination. Here are the steps to create pagination in Yii:
1 2 3 4 5 6 |
$dataProvider = new ActiveDataProvider([ 'query' => Post::find(), 'pagination' => [ 'pageSize' => 10, ], ]); |
In this example, we are creating a new ActiveDataProvider
object that fetches data from the Post
model using the find()
method. We have also set the page size to 10, which means that each page will display 10 records.
1 2 3 4 5 6 |
<?= ListView::widget([ 'dataProvider' => $dataProvider, 'itemView' => '_post', 'layout' => "{items} {pager}", ]); ?> |
In this example, we are creating a ListView
widget and passing the $dataProvider
object to it. We have also set the itemView
property to _post
, which is the name of the view file that will be used to render each item. Finally, we have set the layout
property to {items}
{pager}
, which means that the items will be rendered first, followed by the pager.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?= ListView::widget([ 'dataProvider' => $dataProvider, 'itemView' => '_post', 'layout' => "{items} {pager}", 'pager' => [ 'class' => 'yiiwidgetsLinkPager', 'options' => [ 'class' => 'pagination', ], 'prevPageLabel' => '«', 'nextPageLabel' => '»', ], ]); ?> |
In this example, we have created a custom pager using the LinkPager
widget. We have also set the prevPageLabel
and nextPageLabel
properties to «
and »
, respectively, to use arrows instead of the default text labels.