Silverstripe 将筛选器添加到循环并按月筛选
我可以在筛选月份的日期中添加筛选器吗 我知道我可以使用过滤器,但这是一个特定日期的过滤器。我想做的是按月过滤,即 这可能吗Silverstripe 将筛选器添加到循环并按月筛选,silverstripe,Silverstripe,我可以在筛选月份的日期中添加筛选器吗 我知道我可以使用过滤器,但这是一个特定日期的过滤器。我想做的是按月过滤,即 这可能吗 Wesley对于更高级的过滤,我通常在模型上创建一个函数来处理过滤 /** *在给定月份进行的课程。 * *@param int$month *@returndatalist */ 公共功能课程按月$month { $year=日期'Y'; $day=日期'd'; $endDay=cal_days(以公历为单位,$monthCAL_,$month,$year); 返回$t
Wesley对于更高级的过滤,我通常在模型上创建一个函数来处理过滤 /** *在给定月份进行的课程。 * *@param int$month *@returndatalist */ 公共功能课程按月$month { $year=日期'Y'; $day=日期'd'; $endDay=cal_days(以公历为单位,$monthCAL_,$month,$year); 返回$this->Courses->filter [ '日期:大于'=>$year.'-'.$month.'-'.$day.'00:00', 'DateTo:LessThan'=>$year.-'.$month.-'.$endDay.'23:59:59', ] ; } 然后你可以像这样在模板中使用它
<% loop $CoursesByMonth(2) %>
我还没有实际测试函数本身,所以它很可能不起作用,但希望这能让您了解如何实现您正在尝试的功能。不知怎的,我没有让它起作用。我想这与我目前的设置有关。让我给你介绍一下这方面的背景 我在下面的mysite/code文件中有3个php文件。这些文件允许您创建具有不同课程日期的课程。这些课程应该按月分组,并显示在主页homepage.ss上,但不知何故,我无法做到这一点。因此,目前我已经在html中添加了一些过滤器,这些过滤器部分起作用。因此会显示月份,但课程不是每月分组,而是按课程名称分组。在这个问题的底部,您将看到我的解决方法 我错过了什么或做错了什么 一,。Courses.php 二,。Project.php 三,。ProjectHolder.php 四,。我的变通方法
模板中有太多的业务逻辑-您需要简化。 我不能从上面的代码中真正理解$Projects循环的来源,但我的方法类似于下面的方法,在PHP方面尽可能多地移动逻辑。因此,请将以下内容添加到项目类:
public function CoursesByMonth() {
$months = array();
foreach ($this->Courses() as $course) {
$month = $course->DateTo->Month();
if (!in_array($month, months)) {
$months[] = $month
}
}
// at this point you have all the distinct months, but the array it's not sorted by month, so let's sort it
$months_sorted = array();
foreach($months as $month) {
$m = date_parse($month);
$months_sorted[$m['month']] = $month;
}
ksort($months_sorted);
// at this point you have a simple PHP array, with all the distinct months from all the courses, in the proper order
// but this is useless in Silverstripe templates - so we need to do some other stuff here
$result = new ArrayList();
foreach ($months_sorted as $month) {
$courses_for_current_month = $this->Courses()->filter('DateTo.Month' => $month);
$result->push(new ArrayData(
'Month' => $month,
'Courses' => $courses_for_current_month;
));
}
return $result;
}
…然后在模板中有如下内容:
<div class="half">
<% loop $Projects %>
<% loop $CoursesByMonth %>
<div class="tab">
<input id="tab-{$Pos(1)}" type="checkbox" name="tabs">
<label for="tab-{$Pos(1)}">$Month</label>
<% end_loop %>
<div class="tab-content">
<ul style="line-height:1.8em; margin:10px 0;">
<% loop $CoursesByMonth.Courses.Sort('DateTo', 'ASC') %>
<li><a href="$URLSegment.Up">$DateTo.DayOfMonth $DateTo.Month $DateTo.ShortMonth - $DateFrom.DayOfMonth $DateFrom.ShortMonth: $Project.Title</a></li>
<% end_loop %>
</ul>
</div> <!--/ .tab-content -->
</div> <!--/ .tab -->
<% end_loop %>
</div> <!--/ .half -->
我没有测试所有这些,但据我所知,这是我可以想出的代码。即使它不起作用,它也会给你一些我很确定的想法。谢谢你的帮助,简·克鲁曼。今晚我会试试这个。把这些添加到问题中,而不是作为答案!
<?php
class ProjectHolder extends Page {
private static $allowed_children = array(
'Project'
);
}
class ProjectHolder_Controller extends Page_Controller {
}
<div class="half">
<% loop $Projects %>
<% loop $Courses.Limit(1) %>
<div class="tab">
<% if DateTo.Month == "January" %>
<input id="tab-one" type="checkbox" name="tabs">
<label for="tab-one">January</label>
<% else_if DateTo.Month == "February" %>
<input id="tab-two" type="checkbox" name="tabs">
<label for="tab-two">February</label>
<% else_if DateTo.Month == "March" %>
<input id="tab-three" type="checkbox" name="tabs">
<label for="tab-three">March</label>
<% else_if DateTo.Month == "April" %>
<input id="tab-four" type="checkbox" name="tabs">
<label for="tab-four">April</label>
<% else_if DateTo.Month == "May" %>
<input id="tab-five" type="checkbox" name="tabs">
<label for="tab-five">May</label>
<% else_if DateTo.Month == "June" %>
<input id="tab-six" type="checkbox" name="tabs">
<label for="tab-six">June</label>
<% else_if DateTo.Month == "July" %>
<input id="tab-seven" type="checkbox" name="tabs">
<label for="tab-seven">July</label>
<% else_if DateTo.Month == "August" %>
<input id="tab-eight" type="checkbox" name="tabs">
<label for="tab-eight">August</label>
<% else_if DateTo.Month == "September" %>
<input id="tab-nine" type="checkbox" name="tabs">
<label for="tab-nine">September</label>
<% else_if DateTo.Month == "October" %>
<input id="tab-ten" type="checkbox" name="tabs">
<label for="tab-ten">October</label>
<% else_if DateTo.Month == "November" %>
<input id="tab-eleven" type="checkbox" name="tabs">
<label for="tab-eleven">November</label>
<% else_if DateTo.Month == "December" %>
<input id="tab-twelve" type="checkbox" name="tabs">
<label for="tab-twelve">December</label>
<% end_if %>
<% end_loop %>
<div class="tab-content">
<ul style="line-height:1.8em; margin:10px 0;">
<% loop $Courses.Sort(DateTo, ASC) %>
<li><a href="$URLSegment.Up">$DateTo.DayOfMonth $DateTo.Month $DateTo.ShortMonth - $DateFrom.DayOfMonth $DateFrom.ShortMonth: $Project.Title</a></li>
<% end_loop %>
</ul>
</div>
</div>
<% end_loop %>
</div>
public function CoursesByMonth() {
$months = array();
foreach ($this->Courses() as $course) {
$month = $course->DateTo->Month();
if (!in_array($month, months)) {
$months[] = $month
}
}
// at this point you have all the distinct months, but the array it's not sorted by month, so let's sort it
$months_sorted = array();
foreach($months as $month) {
$m = date_parse($month);
$months_sorted[$m['month']] = $month;
}
ksort($months_sorted);
// at this point you have a simple PHP array, with all the distinct months from all the courses, in the proper order
// but this is useless in Silverstripe templates - so we need to do some other stuff here
$result = new ArrayList();
foreach ($months_sorted as $month) {
$courses_for_current_month = $this->Courses()->filter('DateTo.Month' => $month);
$result->push(new ArrayData(
'Month' => $month,
'Courses' => $courses_for_current_month;
));
}
return $result;
}
<div class="half">
<% loop $Projects %>
<% loop $CoursesByMonth %>
<div class="tab">
<input id="tab-{$Pos(1)}" type="checkbox" name="tabs">
<label for="tab-{$Pos(1)}">$Month</label>
<% end_loop %>
<div class="tab-content">
<ul style="line-height:1.8em; margin:10px 0;">
<% loop $CoursesByMonth.Courses.Sort('DateTo', 'ASC') %>
<li><a href="$URLSegment.Up">$DateTo.DayOfMonth $DateTo.Month $DateTo.ShortMonth - $DateFrom.DayOfMonth $DateFrom.ShortMonth: $Project.Title</a></li>
<% end_loop %>
</ul>
</div> <!--/ .tab-content -->
</div> <!--/ .tab -->
<% end_loop %>
</div> <!--/ .half -->