PHP code example of diiimonn / yii2-widget-next-button

1. Go to this page and download the library: Download diiimonn/yii2-widget-next-button library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

diiimonn / yii2-widget-next-button example snippets


...
use yii\data\Pagination;
...

class SiteController extends Controller
{
    ...
    public function actionIndex($id)
    {
        ...
        $query = MyModel::find();

        $queryCount = clone $query;

        $pagination = new Pagination([
            'totalCount' => $queryCount->count(),
            'pageSize' => 10,
            'page' => 0,
        ]);

        $isNext = $pagination->getPageCount() > 1;

        $query->offset($pagination->offset);
        $query->limit($pagination->limit);
        $models = $query->all();

        ...

        return $this->render('index', [
            ...
            'models' => $models,
            'isNext' => $isNext,
        ]);
    }

    ...

    public function actionNext($id, $page = 0)
    {
        Yii::$app->response->format = 'json';

        $json = new \stdClass();

        $query = MyModel::find();

        $queryCount = clone $query;

        $pagination = new Pagination([
            'totalCount' => $queryCount->count(),
            'pageSize' => 10,
            'page' => $page,
        ]);

        $nextPage = $page + 1;

        $json->page = $pagination->getPageCount() > $nextPage ? $nextPage : 0;

        $query->offset($pagination->offset);
        $query->limit($pagination->limit);
        $models = $query->all();

        ...

        $json->html = $this->renderPartial('_items', [
            ...
            'models' => $models,
        ]);

        return $json;
    }

    ...
}

...
use diiimonn\widgets\NextButton;
use yii\helpers\Url;

...

 $nextButton = NextButton::begin([
    'buttonContent' => Yii::t('app', 'Show next ...'),
    'buttonOptions' => [/* button tag options */],
    'isNext' => $isNext,
    'scriptOptions' => [
        'ajax' => [
            'url' => Url::toRoute(['next', /* other params */]), // Url for ajax request to actionNext in SiteController. Required parameter
        ],
    ],
    'options' => [/* widget tag options */],
    'containerOptions' => [/* container tag for items options */],
    'buttonContainerOptions' => [],
    'buttonBlockOptions' => [], // for example ['class' => 'row']
    'buttonWrapOptions' => [], // for example ['class' => 'col-md-6 col-md-offset-3']
]) 

├── site
│   ├── index.php
│   ├── _items.php
│   ├── ...