Here are the steps:
- In your standard template, add data attributes for designrole, designcompany and designlocation
<div class="interviews"
id="interviews"
data-page="<?= $page->url() ?>"
data-limit="<?= $limit ?>"
data-role="<?= $designrole ?>"
data-company="<?= $designcompany ?>"
data-location="<?= $designlocation ?>"
Note that you have to also adapt the controller to return these variables if they are set, so we use the long form instead of compact:
return [
'offset' => $offset,
'limit' => $limit,
'interviews' => $interviews,
'more' => $more,
'designcompany' => $designcompany ?? null,
'designrole' => $designrole,
'designlocation' => $designlocation,
];
- In your JS, get these attributes and send them with your request:
/*eslint-disable */
$(function() {
const element = $('#interviews');
const url = `${element.data('page')}.json`;
console.log(url);
const limit = parseInt(element.data('limit'));
const designlocation = element.data('location');
const designrole = element.data('role');
const designcompnay = element.data('company');
let offset = limit;
$('.load-more').on('click', function(e) {
$.get(url, { limit: limit, offset: offset, designrole: designrole, designcompany: designcompany, designlocation: designlocation }, function (data) {
if (data.more === false) {
$('.load-more').hide();
}
element
.children()
.last()
.after(data.html);
offset += limit;
});
});
});
The json controller needs some changes as well:
<?php
return function ($pages, $page) {
$interviews = $page->children()->listed()->flip();
if($designrole = get('designrole')) {
$interviews = $interviews->filterBy('role', str_replace('-',' ',$designrole));
}
if($designlocation = get('designlocation')) {
$interviews = $interviews->filterBy('location', str_replace('-',' ',$designlocation));
}
if($designcompany = get('designcompany')) {
$interviews = $interviews->filterBy('company', str_replace('-',' ',$designcompany));
}
$count = $interviews->count();
$offset = intval(get('offset'));
$limit = intval(get('limit'));
$interviews = $interviews->offset($offset)->limit($limit);
$more = $count > $offset + $limit;
return [
'offset' => $offset,
'limit' => $limit,
'interviews' => $interviews,
'more' => $more,
'designcompany' => $designcompany ?? null,
'designrole' => $designrole,
'designlocation' => $designlocation,
];
};
The important thing here is to use get() instead of param().