I am working on building out some pagination for a page on a SilverStripe site that is meant to show all articles at first by default, but the user can select which articles to view by selecting a year from a dropdown control.
Here is the template for the articles. Right now I have code in place that adds pagination when the page first loads or is reloaded and all articles are grabbed from the server:
<select id="SelectNewsYear">
<option value="">Select a year</option>
<% loop $GroupedNewsByDate.GroupedBy(PublishYear) %>
<option value="$PublishYear">$PublishYear</option>
<% end_loop %>
<option value="all">Show all</option>
</select>
<br /><br />
<div class="RecentNews">
<% loop $PaginatedReleases %>
$ArticleDate.format("F j, Y"), <a href="$URLSegment">$H1</a><br />
<% end_loop %>
<% if $PaginatedReleases.MoreThanOnePage %>
<% if $PaginatedReleases.NotFirstPage %>
<a class="prev" href="$PaginatedReleases.PrevLink">Prev</a>
<% end_if %>
<% loop $PaginatedReleases.Pages %>
<% if $CurrentBool %>
$PageNum
<% else %>
<% if $Link %>
<a href="$Link">$PageNum</a>
<% else %>
...
<% end_if %>
<% end_if %>
<% end_loop %>
<% if $PaginatedReleases.NotLastPage %>
<a class="next" href="$PaginatedReleases.NextLink">Next</a>
<% end_if %>
<% end_if %>
</div>
The PaginatedReleases function in Page.php:
//Returns a paginted list of news releases
public function PaginatedReleases(){
$newslist = NewsReleaseArticlePage::get()->sort('ArticleDate', "DESC");
return new PaginatedList($newslist, $this->getRequest());
}
The problem now is figuring out how to maintain the pagination feature whenever a year is selected from the dropdown. Initially, I did not concern myself with pagination as I was more concerned with the functionality of the dropdown list. This is the jQuery and AJAX code I have set up currently that grabs the year value from the dropdown list and passes it to the server to the appropriate function:
(function($) {
$(document).ready(function() {
var SelectNewsYear = $('#SelectNewsYear');
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
SelectNewsYear.change(function() {
if (SelectNewsYear.val() != "" && SelectNewsYear.val() != null && SelectNewsYear.find('option:selected').attr('value') !="all") {
sendYear();
}
else{
showAll();
}
});
//get all articles by the year selected
function sendYear(){
var year = SelectNewsYear.find('option:selected').attr('value');
$.ajax({
type: "POST",
url: "/home/getNewsByYear/"+year,
dataType: "json"
}).done(function (response) {
var list = '';
var newsSection = $('.RecentNewsByYear');
for (var i=0;i<response.length;i++){
var newsDate = new Date(response[i].date);
var monthFullName = month[newsDate.getUTCMonth()];
list += monthFullName + " " + newsDate.getUTCDate() +", " +newsDate.getFullYear() + ', ' + '<a href="' + response[i].article + '"target="_blank">' + response[i].title +"</a> <br />";
}
newsSection.empty();
newsSection.append(list);
});
}
});
}(jQuery));
$ = jQuery.noConflict();
And the getNewsByYear function from Page.php:
//Get all recent news by year based on selected year from dropdown
public function getNewsByYear(){
//Get the year selected by the dropdown
$newsReleaseYear = $this->getRequest()->param('ID');
//Group together all news that are associated with that selected year
$newsReleases = NewsReleaseArticlePage::get();
$return = array();
//put the news releases into the array that match the selected year
foreach($newsReleases as $newsRelease){
$newsDate = date("Y", strtotime($newsRelease->ArticleDate));
if($newsDate == $newsReleaseYear){
$return[] = array(
'title' => $newsRelease->H1,
'date' => $newsRelease->ArticleDate,
'article' => $newsRelease->URLSegment
);
}
}
return json_encode($return);
}
The getNewsByYear function works fine as is, but I am not sure how to incorporate the SilverStripe PaginationList feature here. I am wondering if there is a way to return the selected articles without relying on json encoded data?