SilverStripe 3:如何按祖父页面对排序数组进行分组
SilverStripe 3: How to group a sorted array by grandparent pages
我正在尝试遍历所有 ProductPage
,同时按祖父母的头衔对它们进行分组(因为这是产品类别)。我还想按 ProductReleaseDate
降序对每个组下的产品页面进行排序。最后,如果可能的话,任何没有 ProductReleaseDate
的都列在所有内容之前。
我的页面控制器中有这个功能可以抓取所有产品页面:
function ProductPages() {
$productPages = ProductPage::get();
return $productPages ? $productPages : false;
}
然后在我的模板中:
<% loop $ProductPages.Sort(ProductReleaseDate, DESC) %>
$Title
<% end_loop %>
这会按给定的产品发布日期降序显示我所有的产品页面标题。他们现在需要分组。
我一直在努力寻找,但找不到正确的文档或示例来解决这个问题。也许我需要 groupBy?我不确定它是否需要在控制器或模板中。
这可能有帮助,但我需要帮助:http://docs.silverstripe.org/en/developer_guides/model/how_tos/grouping_dataobject_sets/
在 SilverStripe 3.1 中,我们可以使用您在问题中链接到的 GroupedList
来做到这一点。
要设置它,我们首先需要一些东西来对项目进行分组。 return 值的变量或函数。
在您的情况下,我们将设置一个获取函数,该函数 return 是 parent 的大标题。
ProductPage.php
class ProductPage extends SiteTree {
public function getGrandParentTitle() {
$parent = $this->Parent();
if ($parent->Exists()) {
$grandParent = $parent->Parent();
if ($grandParent->Exists()){
return $grandParent->Title;
}
}
return '';
}
}
然后我们需要添加一个函数,它将return一个GroupedList
。
Page.php
class Page extends SiteTree {
public function getGroupedProducts() {
return GroupedList::create(ProductPage::get()->sort('ProductReleaseDate', 'DESC'));
}
}
最后,在我们的模板中,我们调用了 GroupedList
函数并告诉它根据什么对项目进行分组。
您的模板
<% loop $GroupedProducts.GroupedBy(GrandParentTitle) %>
<h3>$GrandParentTitle</h3>
<ul>
<% loop $Children %>
<li>$Title</li>
<% end_loop %>
</ul>
<% end_loop %>
按Parent标题分组
或者,如果您想先按 parent 页面标题排序,我们将设置一个 return 获取 parent 标题的函数。
ProductPage.php
class ProductPage extends SiteTree {
public function getParentTitle() {
$parent = $this->Parent();
if ($parent->Exists()) {
return $parent->Title;
}
return '';
}
}
然后在我们的模板中调用我们之前创建的 GroupedList
函数,但这次将 GroupedBy
设置为 ParentTitle
。
您的模板
<% loop $GroupedProducts.GroupedBy(ParentTitle) %>
<h3>$ParentTitle</h3>
<ul>
<% loop $Children %>
<li>$Title</li>
<% end_loop %>
</ul>
<% end_loop %>
我正在尝试遍历所有 ProductPage
,同时按祖父母的头衔对它们进行分组(因为这是产品类别)。我还想按 ProductReleaseDate
降序对每个组下的产品页面进行排序。最后,如果可能的话,任何没有 ProductReleaseDate
的都列在所有内容之前。
我的页面控制器中有这个功能可以抓取所有产品页面:
function ProductPages() {
$productPages = ProductPage::get();
return $productPages ? $productPages : false;
}
然后在我的模板中:
<% loop $ProductPages.Sort(ProductReleaseDate, DESC) %>
$Title
<% end_loop %>
这会按给定的产品发布日期降序显示我所有的产品页面标题。他们现在需要分组。
我一直在努力寻找,但找不到正确的文档或示例来解决这个问题。也许我需要 groupBy?我不确定它是否需要在控制器或模板中。
这可能有帮助,但我需要帮助:http://docs.silverstripe.org/en/developer_guides/model/how_tos/grouping_dataobject_sets/
在 SilverStripe 3.1 中,我们可以使用您在问题中链接到的 GroupedList
来做到这一点。
要设置它,我们首先需要一些东西来对项目进行分组。 return 值的变量或函数。
在您的情况下,我们将设置一个获取函数,该函数 return 是 parent 的大标题。
ProductPage.php
class ProductPage extends SiteTree {
public function getGrandParentTitle() {
$parent = $this->Parent();
if ($parent->Exists()) {
$grandParent = $parent->Parent();
if ($grandParent->Exists()){
return $grandParent->Title;
}
}
return '';
}
}
然后我们需要添加一个函数,它将return一个GroupedList
。
Page.php
class Page extends SiteTree {
public function getGroupedProducts() {
return GroupedList::create(ProductPage::get()->sort('ProductReleaseDate', 'DESC'));
}
}
最后,在我们的模板中,我们调用了 GroupedList
函数并告诉它根据什么对项目进行分组。
您的模板
<% loop $GroupedProducts.GroupedBy(GrandParentTitle) %>
<h3>$GrandParentTitle</h3>
<ul>
<% loop $Children %>
<li>$Title</li>
<% end_loop %>
</ul>
<% end_loop %>
按Parent标题分组
或者,如果您想先按 parent 页面标题排序,我们将设置一个 return 获取 parent 标题的函数。
ProductPage.php
class ProductPage extends SiteTree {
public function getParentTitle() {
$parent = $this->Parent();
if ($parent->Exists()) {
return $parent->Title;
}
return '';
}
}
然后在我们的模板中调用我们之前创建的 GroupedList
函数,但这次将 GroupedBy
设置为 ParentTitle
。
您的模板
<% loop $GroupedProducts.GroupedBy(ParentTitle) %>
<h3>$ParentTitle</h3>
<ul>
<% loop $Children %>
<li>$Title</li>
<% end_loop %>
</ul>
<% end_loop %>