【发布时间】:2016-07-01 15:52:46
【问题描述】:
我有一个文章列表,可以根据年份的下拉列表按年份排序。我问了这个问题并得到了一个惊人的答案,但现在我注意到一个问题:每当用户单击“返回”时,文章列表总是重置为默认视图(即显示所有文章)。但是,下拉列表“记住”用户所做的选择。我希望文章列表与下拉列表中的选择年份相匹配,以使用户保持一致和轻松(我不希望他们每次点击文章上的后退按钮时都必须重新选择最初选择的年份) .
我知道我可以使用会话变量,但我想知道这是否真的有必要?或者考虑到代码的设置方式,这可能是更容易采取的方法?
这是我的 NewsReleasesPage 模板的代码:
<?php
class NewsReleasesPage extends Page
{
private static $db = array();
private static $has_one = array();
}
class NewsReleasesPage_Controller extends Page_Controller
{
private static $allowed_actions = array(
'PaginatedReleases',
'YearFilterForm',
'handleYearRequest',
'doFilter',
'year',
'index'
);
public function init()
{
parent::init();
}
public function handleYearRequest(SS_HTTPRequest $request)
{
$year = $request->param('ID');
$data = array(
'Year' => $year,
'PaginatedReleases' => $this->PaginatedReleases($year)
);
if ($request->isAjax()) {
// in case of an ajax request, render only the partial template
return $this->renderWith('ArticleList', $data);
} else {
// returning an array will cause the page to render normally
return $data;
}
}
//creates a form to filter through news releases by year
public function YearFilterForm()
{
// get an array of all distinct years
$list = SQLSelect::create()
->addFrom('NewsReleaseArticlePage')
->selectField('YEAR("ArticleDate")', 'Year')
->setOrderBy('Year', 'DESC')
->addGroupBy('"Year"')->execute()->column('Year');
// create an associative array with years as keys & values
$values = array_combine($list, $list);
// our fields just contain the dropdown, which uses the year values
$fields = FieldList::create(array(
DropdownField::create(
'Year',
'',
$values,
$this->getRequest()->param('ID')
)->setHasEmptyDefault(true)->setEmptyString('Show all')
));
$actions = FieldList::create(array(
FormAction::create('doFilter', 'Submit')
));
return Form::create($this, 'YearFilterForm', $fields, $actions);
}
public function year()
{
return $this->handleYearRequest($this->request);
}
public function index()
{
return $this->handleYearRequest($this->request);
}
//redirects to the proper url depending on which year is selected for sorting news
public function doFilter($data, $form)
{
if (empty($data['Year'])) {
return $this->redirect($this->Link());
} else {
return $this->redirect($this->Link('year/' . $data['Year']));
}
}
//created a paginated list of news released by year
public function PaginatedReleases($year = null)
{
$list = NewsReleaseArticlePage::get()->sort('ArticleDate', 'DESC');
if ($year) {
$list = $list->where(array('YEAR("ArticleDate") = ?' => $year));
}
return PaginatedList::create($list, $this->getRequest())->setLimitItems(0);
}
}
这是与此相关的jQuery:
(function($) {
$(function(){
// hide form actions, as we want to trigger form submittal
// automatically when dropdown changes
$("#Form_YearFilterForm").find(".Actions").hide();
// bind a change event on the dropdown to automatically submit
$("#Form_YearFilterForm").on("change", "select", function (e) {
$("#Form_YearFilterForm").submit();
});
// handle form submit events
$("#Form_YearFilterForm").on("submit", function(e){
e.preventDefault();
var form = $(this);
$("#ArticleList").addClass("loading");
// submit form via ajax
$.post(
form.attr("action"),
form.serialize(),
function(data, status, xhr){
$("#ArticleList").replaceWith($(data));
}
);
return false;
});
// handle pagination clicks
$("body").on("click", "a.pagination", function (e) {
e.preventDefault();
$("#ArticleList").addClass("loading");
$.get(
$(this).attr("href"),
function(data, status, xhr){
$("#ArticleList").replaceWith($(data));
}
);
return false;
});
});
})(jQuery);
然后是呈现列表的 SilverStripe 模板文件:
<div id="box3" class="clearfix">
<div id="Box-Main-Right" class="clearfix">
<p class="Text-Intro">$H1</p>
$YearFilterForm
</div>
</div>
<div class="Box-LEADING-BRANDS clearfix">
<div class="Box-Main-Right1 clearfix">
<% include ArticleList %>
</div>
</div>
$Form
【问题讨论】:
标签: php jquery silverstripe