Блог: Вместо пагинации сделать ссылку "Загрузить еще"

parent 7ec50275
...@@ -5,9 +5,11 @@ namespace common\modules\blog\controllers; ...@@ -5,9 +5,11 @@ namespace common\modules\blog\controllers;
use Yii; use Yii;
use common\components\BaseController; use common\components\BaseController;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\web\Response;
use common\modules\blog\models\Post; use common\modules\blog\models\Post;
use common\modules\blog\models\PostTag; use common\modules\blog\models\PostTag;
use common\modules\blog\models\PostTagAssign;
/** /**
* PostController implements the CRUD actions for Post model. * PostController implements the CRUD actions for Post model.
...@@ -18,6 +20,7 @@ class PostController extends BaseController ...@@ -18,6 +20,7 @@ class PostController extends BaseController
{ {
return [ return [
'Index' => 'Блог', 'Index' => 'Блог',
'Load' => 'Подгрузка записей',
'Tag' => 'Просмотр тега', 'Tag' => 'Просмотр тега',
'View' => 'Просмотр записи', 'View' => 'Просмотр записи',
]; ];
...@@ -31,13 +34,51 @@ class PostController extends BaseController ...@@ -31,13 +34,51 @@ class PostController extends BaseController
{ {
$this->meta_title = Yii::t('menu', 'Blog') . ' - ' . \Yii::$app->params['name']; $this->meta_title = Yii::t('menu', 'Blog') . ' - ' . \Yii::$app->params['name'];
$models = Post::find()->where(['active' => 1])->all(); $query = Post::find()->where(['active' => 1])->limit(Post::PAGE_SIZE)->orderBy('created_at DESC');
return $this->render('index', [ return $this->render('index', [
'models' => $models, 'models' => $query->all(),
'count' => $query->count(),
]); ]);
} }
/**
* Displays all Post models.
* @return mixed
*/
public function actionLoad()
{
if(Yii::$app->request->isAjax)
{
$offset = Yii::$app->request->post('offset');
$tag = Yii::$app->request->post('tag');
Yii::$app->response->format = Response::FORMAT_JSON;
$query = Post::find()->where(['active' => 1])->orderBy(Post::tableName().'.created_at DESC');
if($tag)
{
$query = $query->joinWith('postTagAssigns')->andWhere([PostTagAssign::tableName().'.tag_id' => $tag]);
}
$models = $query->limit(Post::PAGE_SIZE)->offset($offset)->all();
$count = $query->count();
return [
'posts' => $this->renderPartial('_load', [
'models' => $models,
]),
'count' => (int)$count,
'offset' => $offset + Post::PAGE_SIZE
];
}
else
{
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/** /**
* Displays a single Post model. * Displays a single Post model.
* @param string $url * @param string $url
...@@ -56,6 +97,7 @@ class PostController extends BaseController ...@@ -56,6 +97,7 @@ class PostController extends BaseController
return $this->render('tag', [ return $this->render('tag', [
'model' => $model, 'model' => $model,
'count' => $model->getAllPosts()->count()
]); ]);
} }
......
...@@ -28,6 +28,8 @@ class Post extends \common\components\ActiveRecordModel ...@@ -28,6 +28,8 @@ class Post extends \common\components\ActiveRecordModel
const ACTIVE_FALSE = 0; const ACTIVE_FALSE = 0;
const ACTIVE_TRUE = 1; const ACTIVE_TRUE = 1;
const PAGE_SIZE = 2;
public static $active_title = [ public static $active_title = [
self::ACTIVE_FALSE => 'Скрыта', self::ACTIVE_FALSE => 'Скрыта',
self::ACTIVE_TRUE => 'Доступна', self::ACTIVE_TRUE => 'Доступна',
......
...@@ -65,7 +65,22 @@ class PostTag extends \common\components\ActiveRecordModel ...@@ -65,7 +65,22 @@ class PostTag extends \common\components\ActiveRecordModel
*/ */
public function getPosts() public function getPosts()
{ {
return $this->hasMany(Post::className(), ['id' => 'post_id'])->viaTable('posts_tags_assign', ['tag_id' => 'id'])->where(['active' => 1]); return $this->hasMany(Post::className(), ['id' => 'post_id'])
->viaTable('posts_tags_assign', ['tag_id' => 'id'])
->where(['active' => 1])
->orderBy(Post::tableName().'.created_at DESC')
->limit(Post::PAGE_SIZE);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAllPosts()
{
return $this->hasMany(Post::className(), ['id' => 'post_id'])
->viaTable('posts_tags_assign', ['tag_id' => 'id'])
->where(['active' => 1])
->orderBy(Post::tableName().'.created_at DESC');
} }
/** /**
......
<?php
use yii\helpers\Html;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $model common\modules\blog\models\Post */
?>
<?php foreach ($models as $model) : ?>
<article class="article_short">
<a href="<?=Url::to(['/blog/'.$model->url])?>" class="article_short_title"><?=$model->lang->title?></a>
<div class="article_short_head">
<span class="article_short_date"><?=date('d.m.Y', $model->created_at)?></span>
<!-- <span class="article_short_view">
180
<div class="blog_toltip_left">Количество просмотров</div>
</span> -->
<!-- <ul class="article_short_social">
<li>
<a href="#"><img src="/images/icon/sh_social_vk.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_fb.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_tw.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_gp.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_t.png" height="30" width="30" alt=""></a>
</li>
</ul> -->
</div>
<div class="article_short_tags">
<?php foreach ($model->postTags as $tag) : ?>
<a href="<?=$tag->url;?>"># <?=$tag->name?></a>
<?php endforeach; ?>
</div>
<div class="article_short_txt">
<?php if($model->preview) :
echo Html::img($model->preview);
endif; ?>
<?=$model->lang->text?>
</div>
</article>
<?php endforeach; ?>
\ No newline at end of file
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
use yii\helpers\Html; use yii\helpers\Html;
use yii\helpers\Url; use yii\helpers\Url;
use common\modules\blog\models\Post;
/* @var $this yii\web\View */ /* @var $this yii\web\View */
/* @var $model common\modules\blog\models\Post */ /* @var $model common\modules\blog\models\Post */
...@@ -16,46 +18,21 @@ use yii\helpers\Url; ...@@ -16,46 +18,21 @@ use yii\helpers\Url;
<h1><?=Yii::t('menu', 'Blog')?></h1> <h1><?=Yii::t('menu', 'Blog')?></h1>
<?php foreach ($models as $model) : ?> <?=$this->render('_load', ['models' => $models])?>
<article class="article_short">
<a href="<?=Url::to(['/blog/'.$model->url])?>" class="article_short_title"><?=$model->lang->title?></a>
<div class="article_short_head">
<span class="article_short_date"><?=date('d.m.Y', $model->created_at)?></span>
<!-- <span class="article_short_view">
180
<div class="blog_toltip_left">Количество просмотров</div>
</span> -->
<!-- <ul class="article_short_social">
<li>
<a href="#"><img src="/images/icon/sh_social_vk.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_fb.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_tw.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_gp.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_t.png" height="30" width="30" alt=""></a>
</li>
</ul> -->
</div>
<div class="article_short_tags">
<?php foreach ($model->postTags as $tag) : ?>
<a href="<?=$tag->url;?>"># <?=$tag->name?></a> <?php if($count > count($models)) : ?>
<?php endforeach; ?> <div class="loaded">
</div> </div>
<div class="article_short_txt"> <div class="load-box">
<a href="#" class="sidebar_btn" id="load-post" data-offset="<?=Post::PAGE_SIZE?>" style="display:block; margin: 0 auto;">Загрузить еще</a>
<?php if($model->preview) : <div class="loading-post"></div>
echo Html::img($model->preview);
endif; ?>
<?=$model->lang->text?>
</div> </div>
</article>
<?php endforeach; ?> <br><br><br>
<?php endif; ?>
<?=$this->render('_subscribe', ['title' => 'Страница Блог'])?> <?=$this->render('_subscribe', ['title' => 'Страница Блог'])?>
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
use yii\helpers\Html; use yii\helpers\Html;
use yii\helpers\Url; use yii\helpers\Url;
use common\modules\blog\models\Post;
/* @var $this yii\web\View */ /* @var $this yii\web\View */
/* @var $model common\modules\blog\models\Post */ /* @var $model common\modules\blog\models\Post */
...@@ -16,46 +18,21 @@ use yii\helpers\Url; ...@@ -16,46 +18,21 @@ use yii\helpers\Url;
<h1><?=Yii::t('app', 'Tag')?>: <?=$model->name;?></h1> <h1><?=Yii::t('app', 'Tag')?>: <?=$model->name;?></h1>
<?php foreach ($model->posts as $post) : ?> <?=$this->render('_load', ['models' => $model->posts])?>
<article class="article_short">
<a href="<?=Url::to(['/blog/'.$post->url])?>" class="article_short_title"><?=$post->lang->title?></a>
<div class="article_short_head">
<span class="article_short_date"><?=date('d.m.Y', $post->created_at)?></span>
<!-- <span class="article_short_view">
180
<div class="blog_toltip_left">Количество просмотров</div>
</span> -->
<!-- <ul class="article_short_social">
<li>
<a href="#"><img src="/images/icon/sh_social_vk.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_fb.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_tw.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_gp.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_t.png" height="30" width="30" alt=""></a>
</li>
</ul> -->
</div>
<div class="article_short_tags">
<?php foreach ($post->postTags as $tag) : ?>
<a href="<?=$tag->url;?>"># <?=$tag->name?></a> <?php if($count > count($model->posts)) : ?>
<?php endforeach; ?> <div class="loaded">
</div> </div>
<div class="article_short_txt"> <div class="load-box">
<a href="#" class="sidebar_btn" id="load-post" data-offset="<?=Post::PAGE_SIZE?>" data-tag="<?=$model->id?>" style="display:block; margin: 0 auto;">Загрузить еще</a>
<?php if($post->preview) : <div class="loading-post"></div>
echo Html::img($post->preview);
endif; ?>
<?=$post->lang->text?>
</div> </div>
</article>
<?php endforeach; ?> <br><br><br>
<?php endif; ?>
<?=$this->render('_subscribe', ['title' => 'Тег: ' . $model->name])?> <?=$this->render('_subscribe', ['title' => 'Тег: ' . $model->name])?>
......
...@@ -174,6 +174,13 @@ a.toggle_bottom:hover .icon-arrowDown2:after, a.toggle_bottom:active .icon-arrow ...@@ -174,6 +174,13 @@ a.toggle_bottom:hover .icon-arrowDown2:after, a.toggle_bottom:active .icon-arrow
.bx-wrapper .bx-pager.bx-default-pager a.active { .bx-wrapper .bx-pager.bx-default-pager a.active {
background: url(../images/elips_active.png) no-repeat center center; background: url(../images/elips_active.png) no-repeat center center;
} }
.loading-post {
background: url(../images/post-loader.gif) no-repeat;
width: 32px;
height: 32px;
margin: 0 auto;
display: none;
}
@media (min-width: 970px) { @media (min-width: 970px) {
.article_short_txt img { .article_short_txt img {
......
...@@ -6,6 +6,42 @@ $(document).ready(function() { ...@@ -6,6 +6,42 @@ $(document).ready(function() {
return false; return false;
}); });
$('a#load-post').click(function() {
var button = $(this), loading = $('.loading-post'), offset = button.data('offset'), tag = button.data('tag');
button.hide();
loading.show();
$.ajax({
method: 'POST',
url: "/blog/post/load",
data: {
offset: offset,
tag: tag
},
success: function(response)
{
$('.loaded').append(response.posts);
button.data('offset', response.offset);
if(response.count <= response.offset)
{
button.hide();
}
else
{
button.show();
}
loading.hide();
}
});
return false;
});
$.ajax({ $.ajax({
url: "/sessions/default/add" url: "/sessions/default/add"
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment