R
R
Ruslan Absalyamov2019-02-24 19:52:00
Yii
Ruslan Absalyamov, 2019-02-24 19:52:00

How can I reload the page with the output of the number of records per page?

I don't know what to do right. There is a page where the number of records is displayed and dropdown where it is indicated how much to show the number of records.
I tried to do something like this

<?php Pjax::begin([
            'id' => 'pjax-rows'
    ]) ?>
    <div>
        <?php foreach ($model as $posts): ?>
            <div><?= Html::img('/'.$posts->image_path) ?></div>
            <h3><?= $posts->title ?></h3>
            <div><?= $posts->description ?></div>
            <div><?= Html::a('Подробнее', \yii\helpers\Url::to(['site/post', 'id' => $posts->id])) ?></div>
            <hr/>
        <?php endforeach; ?>
    </div>
    <div><?= Html::dropDownList('countRow', null, [
            10 => 'Показат 10 страниц',
            25 => 'Показат 25 страниц',
            50 => 'Показат 50 страниц'
        ], [
                'class' => 'count-rows',
                'data-url' => \yii\helpers\Url::to(['site/index'])
        ]) ?>
    </div>
    <?php Pjax::end() ?>

<?php
$script = <<<JS
$('.count-rows').click(function() {
    let select = $(this);
    location.reload(select.data('url') + '&countRow=' + select.val());
})
JS;

$this->registerJs($script);
?>

And in the controller
public function actionIndex()
    {
        $query = News::find()->with('user')->where(['status' => News::NEWS_ACTIVE]);
        $request = Yii::$app->request->get('countRow');
        $count = $request ? $request : 1;
        $pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => $count]);
        $posts = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->all();
        return $this->render('index', [
            'model' => $posts,
            'pages' => $pages
        ]);
    }

Only so far it does not work out so that when 50 records are allowed, 50 records were shown. So far, reload does not work as I want.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
morricone85, 2019-02-24
@morricone85

So far, reload does not work as I want.

What does it show?

V
Vlad Nevlad, 2019-02-25
@vladnevlad

pjax has a standard method for reloading the contents of a container.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question