Hey guys, I have a problem with the tag cloud while using the load-more function at the same time. My wish would be that with active tag selection the click on the load-more-button resets the url. Actually the load-more-button doesn’t load all items.
Hope anyone can help me. Thank you in advance!
Cheers Martin
The Controller
<?php
return function($site, $pages, $page) {
$projects = $page->children()->visible()->flip();
$count = $projects->count();
$more = false;
// add the tag filter
if($tag = param('tag')) {
$projects = $projects->filterBy('tags', $tag, ',');
}
// fetch all tags
$tags = $projects->pluck('tags', ',', false);
// check if the request is an Ajax request and if the limit and offset keys are set
if(r::ajax() && get('offset') && get('limit')) {
// convert limit and offset values to integer
$offset = intval(get('offset'));
$limit = intval(get('limit'));
// limit projects using offset and limit values
$projects = $projects->offset($offset)->limit($limit);
// check if there are more projects left
$more = $count > $offset + $limit;
// otherwise set the number of projects initially displayed
} else {
$offset = 0;
$limit = 6;
$projects = $projects->limit($limit);
}
return compact('offset', 'limit', 'projects', 'tags', 'tag', 'more');
};
The Javascript
var element = $('.projects_container');
console.log(element);
var json = element.data('page') + '.json';
console.log(json);
var limit = parseInt(element.data('limit'));
console.log(limit);
var offset = limit;
console.log(offset);
$('.load-more').on('click', function(e) {
$.get(json, {limit: limit, offset: offset}, function(data) {
console.log(data);
if(data.more === false) {
$('.load-more').hide();
}
element.children().last().after(data.html);
offset += limit;
});
});
The Template
<div class="container">
<div class="row justify-content-center">
<div class="col-11 col-sm-12 col-lg-11 col-xl-10">
<div class="col-12">
<h4>Kategorien</h4>
<?php $tags = $page->children()->visible()->pluck('tags', ',', true); ?>
<?php $activeTag = param('tag'); ?>
<ul class="tags">
<?php foreach($tags as $tag): ?>
<li>
<a <?= $activeTag === $tag ? ' class="active"' : '' ?> href="<?php echo url($page->url() . '/' . url::paramsToString(['tag' => $tag])) ?>"><?php echo html($tag) ?>
</a>
</li>
<?php endforeach ?>
</ul>
</div>
<div class="mb-5 row g-lg-5 g-md-4 g-sm-3 g-2 justify-content-center referenzen projects projects_container" data-page="<?= $page->url() ?>" data-limit="<?= $limit ?>">
<?php foreach($projects as $project): ?>
<?php snippet('project', compact('project')) ?>
<?php endforeach ?>
</div>
<button class="btn btn-primary load-more mt-3">Mehr Referenzen <i class="bi bi-arrow-right"></i></button>
</div>
</div>
</div>
The Snippet
<div class="col-lg-4 col-md-6 col-12 case_project">
<a href="<?= $project->url() ?>" class="">
<div class="project_cover" <?php if($image = $project->coverimage()->toFile()): ?>style="background-image: url(<?php echo $image->crop(2000,1200,60)->url() ?>);<?php if($project->my_document_position() != ''): ?> background-position: <?= $project->my_document_position()->html() ?><?php endif ?>"<?php endif ?>>
<h2 class="f_b"><?= $project->title()->html() ?></h2>
<div class="project_cover_overlay"></div>
</div>
</a>
</div>