Silverstripe 从 CheckboxSetField 值创建无序列表 (many_many)
Silverstripe create unordered list from CheckboxSetField values (many_many)
我正在学习,但我又碰壁了,所以我还有一个问题...
所以我创建了一个包含公司所有计划功能的数据对象,并且每当创建新的会员计划类型时,我都会将所有这些功能显示为带有 CheckboxSetField
的复选框,这样用户就不会必须处理 HTML 编辑器字段。
所有设置和工作正常,唯一的麻烦是当我要求复选框的值以显示计划功能列表时,我得到一个逗号分隔的列表...这不好我需要能够获取每个选定的功能并将其显示为列表项 (<li> ... </li>
)。
现在它正在返回 1 ,2 和 2 ,3 等列表,它们是所选计划功能的 ID。我对将复选框的值设置为计划功能的文本字符串犹豫不决,所以我打算使用这些 ID 来引用 PlanFeature
table 中的记录。有更好的方法吗?我想我至少需要一个控件来展开此列表...
我花了整整几个小时来尝试(但都失败了)让它正常工作,我想我需要一些帮助。因为我不知道如何从这个列表中取出项目,所以我还没有开始探索如何在模板中显示内容,所以我可能也需要一些帮助。提前致谢:)
这是我的代码。
PlanFeature.php
<?php
class PlanFeature extends DataObject {
private static $db = array (
'PlanFeatureText' => 'Varchar',
);
private static $belongs_many_many = array (
'PlanFeature' => 'Pricing',
);
private static $summary_fields = array (
'ID' => 'ID',
'PlanFeatureText' => 'Plan Feature',
);
public function getCMSFields() {
$fields = FieldList::create(
TextField::create('PlanFeatureText')
);
return $fields;
}
}
ServicePlan.php 在 getCMSFields()
..//
CheckboxSetField::create(
'PlanFeatures',
'Choose Your Plan Features',
PlanFeature::get()->map('ID', 'PlanFeatureText')
),
..//
Pricing.php
<?php
class Pricing extends Page {
private static $has_many = array (
'ServicePlans' => 'ServicePlan'
);
private static $many_many = array (
'PlanFeatures' => 'PlanFeature'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Plans', GridField::create(
'ServicePlans',
'Orenda Force Service Plan Information',
$this->ServicePlans(),
GridFieldConfig_RecordEditor::create()
));
$fields->addFieldsToTab('Root.Plans', GridField::create(
'PlanFeatures',
'Manage Plan Features',
$this->PlanFeatures(),
GridFieldConfig_RecordEditor::create()
));
return $fields;
}
}
class Pricing_Controller extends Page_Controller {
}
这就是我完全迷路的地方......所以我确定none是正确的:(
Page.php(在Page_Controller)
function ExplodedFeatures() {
$set = new DataObjectSet();
if($Features = Pricing::get()->First()->PlanFeatures) {
foreach(explode(',',$Features) as $key => $value) {
$set->push(new ArrayData(array('Value' => $value)));
}
}
echo $set;
}
public function Features() {
$Feature = Pricing::get();
$Feature = $Feature->PlanFeatures()->getOptions();
return $Feature;
//return $Feature;
}
Pricing.ss
<section class="body pricing">
<div class="content">
<h2 class="title-02 text-monochrome">Pricing</h2>
<% loop $ServicePlans %>
<div class="col-1-3 card pricing">
<div class="pricing__header $PlanColor">
<p class="pricing__plan-name">$PlanName</p>
<p class="pricing__price">$PlanPrice<sub>$PlanRenewal</sub></p>
</div>
<div class="card__contents">
<h3 class="title-06">Plan details</h3>
<ul>
<% loop $PlanFeatures %>
<li>$PlanFeatureText</li>
<% end_loop %>
</ul>
</div>
</div>
<% end_loop %>
$Content
</div>
</section>
您不需要在 Page_Controller
中编写任何代码,因为 Pricing
和 PlanFeature
之间已经存在关系。
在 Pricing
的模板中(我注意到它是一个页面类型),您应该能够使用以下方法生成 PlanFeature
s 的值列表:
<ul>
<% loop $PlanFeatures %>
<li>$PlanFeatureText</li>
<% end_loop %>
</ul>
听起来结构与以下相似。也许这些就是您正在寻找的 relationships/template 结构?
- https://github.com/assertchris/so-29584213/blob/master/mysite/code/Pricing.php
- https://github.com/assertchris/so-29584213/blob/master/mysite/code/models/Plan.php
- https://github.com/assertchris/so-29584213/blob/master/mysite/code/models/Feature.php
- https://github.com/assertchris/so-29584213/blob/master/themes/simple/templates/Layout/Pricing.ss
我正在学习,但我又碰壁了,所以我还有一个问题...
所以我创建了一个包含公司所有计划功能的数据对象,并且每当创建新的会员计划类型时,我都会将所有这些功能显示为带有 CheckboxSetField
的复选框,这样用户就不会必须处理 HTML 编辑器字段。
所有设置和工作正常,唯一的麻烦是当我要求复选框的值以显示计划功能列表时,我得到一个逗号分隔的列表...这不好我需要能够获取每个选定的功能并将其显示为列表项 (<li> ... </li>
)。
现在它正在返回 1 ,2 和 2 ,3 等列表,它们是所选计划功能的 ID。我对将复选框的值设置为计划功能的文本字符串犹豫不决,所以我打算使用这些 ID 来引用 PlanFeature
table 中的记录。有更好的方法吗?我想我至少需要一个控件来展开此列表...
我花了整整几个小时来尝试(但都失败了)让它正常工作,我想我需要一些帮助。因为我不知道如何从这个列表中取出项目,所以我还没有开始探索如何在模板中显示内容,所以我可能也需要一些帮助。提前致谢:)
这是我的代码。
PlanFeature.php
<?php
class PlanFeature extends DataObject {
private static $db = array (
'PlanFeatureText' => 'Varchar',
);
private static $belongs_many_many = array (
'PlanFeature' => 'Pricing',
);
private static $summary_fields = array (
'ID' => 'ID',
'PlanFeatureText' => 'Plan Feature',
);
public function getCMSFields() {
$fields = FieldList::create(
TextField::create('PlanFeatureText')
);
return $fields;
}
}
ServicePlan.php 在 getCMSFields()
..//
CheckboxSetField::create(
'PlanFeatures',
'Choose Your Plan Features',
PlanFeature::get()->map('ID', 'PlanFeatureText')
),
..//
Pricing.php
<?php
class Pricing extends Page {
private static $has_many = array (
'ServicePlans' => 'ServicePlan'
);
private static $many_many = array (
'PlanFeatures' => 'PlanFeature'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Plans', GridField::create(
'ServicePlans',
'Orenda Force Service Plan Information',
$this->ServicePlans(),
GridFieldConfig_RecordEditor::create()
));
$fields->addFieldsToTab('Root.Plans', GridField::create(
'PlanFeatures',
'Manage Plan Features',
$this->PlanFeatures(),
GridFieldConfig_RecordEditor::create()
));
return $fields;
}
}
class Pricing_Controller extends Page_Controller {
}
这就是我完全迷路的地方......所以我确定none是正确的:(
Page.php(在Page_Controller)
function ExplodedFeatures() {
$set = new DataObjectSet();
if($Features = Pricing::get()->First()->PlanFeatures) {
foreach(explode(',',$Features) as $key => $value) {
$set->push(new ArrayData(array('Value' => $value)));
}
}
echo $set;
}
public function Features() {
$Feature = Pricing::get();
$Feature = $Feature->PlanFeatures()->getOptions();
return $Feature;
//return $Feature;
}
Pricing.ss
<section class="body pricing">
<div class="content">
<h2 class="title-02 text-monochrome">Pricing</h2>
<% loop $ServicePlans %>
<div class="col-1-3 card pricing">
<div class="pricing__header $PlanColor">
<p class="pricing__plan-name">$PlanName</p>
<p class="pricing__price">$PlanPrice<sub>$PlanRenewal</sub></p>
</div>
<div class="card__contents">
<h3 class="title-06">Plan details</h3>
<ul>
<% loop $PlanFeatures %>
<li>$PlanFeatureText</li>
<% end_loop %>
</ul>
</div>
</div>
<% end_loop %>
$Content
</div>
</section>
您不需要在 Page_Controller
中编写任何代码,因为 Pricing
和 PlanFeature
之间已经存在关系。
在 Pricing
的模板中(我注意到它是一个页面类型),您应该能够使用以下方法生成 PlanFeature
s 的值列表:
<ul>
<% loop $PlanFeatures %>
<li>$PlanFeatureText</li>
<% end_loop %>
</ul>
听起来结构与以下相似。也许这些就是您正在寻找的 relationships/template 结构?
- https://github.com/assertchris/so-29584213/blob/master/mysite/code/Pricing.php
- https://github.com/assertchris/so-29584213/blob/master/mysite/code/models/Plan.php
- https://github.com/assertchris/so-29584213/blob/master/mysite/code/models/Feature.php
- https://github.com/assertchris/so-29584213/blob/master/themes/simple/templates/Layout/Pricing.ss