Answer the question
In order to leave comments, you need to log in
Implementation of YII2 pagination, to display elements of an array of images from a given folder, how to implement?
Hello everyone, I want to implement the output of images with pagination with a given number per page, the pagination helper does not work, I tried to convert an array into an object, it also does not work,
public function actionIndex() {
$dir = 'uploads';
$query = \yii\helpers\FileHelper::findFiles($dir,['only'=>['*.jpg','*.png']]); // получаю массив картинок
// dd($query );
//echo ArrayHelper::isIndexed($files); //проверяю тип массива
//echo gettype($query ); //массив
$pages = new Pagination([
'totalCount' => $query->count(), // count() - не работает, если задать в ручную тоже Call to a member function offset() on array
'pageSize' => Yii::$app->params['pageSize'],
'forcePageParam' => false,
'pageSizeParam' => false
]);
$files = $query->offset($pages->offset) //Call to a member function offset() on array
->limit($pages->limit)
->asArray()
->all();
return $this->render('index', compact('files','pages'));
}
Answer the question
In order to leave comments, you need to log in
In general, I myself solved this problem with a minimum of code, this is the use of Class yii\data\ArrayDataProvider
Controller:
public function actionIndex() {
$dir = 'uploads';
$query = \yii\helpers\FileHelper::findFiles($dir, ['only' => ['*.jpg', '*.png']]);
$data = $query;
$provider = new ArrayDataProvider([
'allModels' => $data,
'pagination' => [
'pageSize' => 12,
],
/* 'sort' => [
'attributes' => ['id', 'name'],
], */
]);
// получает строки для текущей запрошенной странице
$files = $provider->getModels();
return $this->render('index', compact('files','provider'));
}
echo yii\grid\GridView::widget([
'dataProvider' => $provider,
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question