我有一个带有下拉字段的表单,其中包含服务列表。这些服务根据已输入管理模型"服务的内容显示在drodpown中。
下拉列表:
<select id="ServiceReq">
<option value="">Service Requested*</option>
<% if getServiceList %>
<% loop getServiceList %>
<option value="$Name">$Name</option>
<% end_loop %>
<% end_if %>
</select>
服务管理模型的代码:
<?php
class Service extends DataObject {
private static $db = array(
'Name' => 'varchar',
);
private static $belongs_many_many = array(
'Locations' => 'Location'
);
public static $summary_fields = array(
'Name' => 'Title',
);
private static $field_labels = array(
'Name'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
if ($this->ID) {
$fields->addFieldToTab('Root.Locations', CheckboxSetField::create(
'Locations',
'Locations',
Location::get()->filter(array(
'AcceptingAppointments' => '1'
))->map()
));
}
return $fields;
}
}
我想要做的是,当从ServiceReq下拉列表中选择服务时,我想从位于服务管理模型的位置选项卡中的复选框字段集中检索所有选定的位置。然后,这些位置将用于填写以下形式的位置下拉列表:
<select id="Location">
<option value="">Location/Hospital</option>
</select>
我知道我需要使用当前选择的服务ID,但我不确定如何设置该功能来执行此操作,但我很遗憾如何设置功能要传递给表单的服务器端。
答案 0 :(得分:1)
如果您尝试获取与当前服务相关的地理位置,则会$this->Locations()
,您仍然可以应用自己的过滤器$this->Locations()->filter(array('AcceptingAppointments' => '1'))->map()
。
如果它更像是一种实时更新类型的东西,你可能想看看纠缠。有一个good blog post for getting started with entwine here
答案 1 :(得分:1)
Dependent dropdown field module可以很好地轻松完成这项工作。
以下是使用依赖下拉字段的示例表单,该字段具有Service
下拉字段和Location
下拉字段,该字段填充了与该服务相关的位置。
public function ServiceForm() {
$locationSource = function($serviceID) {
$service = Service::get()->byID($serviceID);
return $service->Locations()
->filter('AcceptingAppointments', true)
->map('ID', 'Name')->toArray();
};
$servicesField = DropdownField::create(
'Service',
'Service',
Service::get()->map('ID', 'Name')->toArray()
)->setEmptyString('');
$locationsField = DependentDropdownField::create(
'Location',
'Location',
$locationSource
)->setDepends($servicesField);
$form = Form::create($this, 'ServiceForm',
FieldList::create(
$servicesField,
$locationsField
),
FieldList::create(
FormAction::create('processServiceForm', 'Submit')
),
RequiredFields::create(
'Service',
'Location'
)
);
return $form;
}
首先,我们有一个正常的下拉字段来选择服务:
$servicesField = DropdownField::create(
'Service',
'Service',
Service::get()->map('ID', 'Name')->toArray()
)->setEmptyString('');
接下来,我们添加一个从属下拉字段来选择服务:
$locationsField = DependentDropdownField::create(
'Location',
'Location',
$locationSource
)->setDepends($servicesField);
setDepends
函数定义将此字段链接到哪个字段,在本例中为services字段。 $locationSource
是一个函数,它将检索位置并返回要使用的字段的数组。
$locationSource = function($serviceID) {
$service = Service::get()->byID($serviceID);
return $service->Locations()
->filter('AcceptingAppointments', true)
->map('ID', 'Name')->toArray();
};