Php SilverStripe-通过jQuery.ajax将数据从控制器传递到html表单
我正在制作一个表单,它有两个下拉列表:请求的服务和位置。每当用户选择一项服务时,“位置”下拉列表中应填充为该服务选择的位置。我有一个用于服务和位置的管理模型,下面是一个用于服务的模型,以显示我如何将位置模型数据映射到它:Php SilverStripe-通过jQuery.ajax将数据从控制器传递到html表单,php,jquery,silverstripe,Php,Jquery,Silverstripe,我正在制作一个表单,它有两个下拉列表:请求的服务和位置。每当用户选择一项服务时,“位置”下拉列表中应填充为该服务选择的位置。我有一个用于服务和位置的管理模型,下面是一个用于服务的模型,以显示我如何将位置模型数据映射到它: <?php class Service extends DataObject { private static $db = array( 'Name' => 'varchar', ); private static $be
<?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;
}
}
最后是getLocationsByService的函数,它使用ajax调用中的服务id来检索该服务的位置:
public function getLocationsByService(){
$serviceid = $this->getRequest()->getVar('serviceid');
$service = Service::get()->byId($serviceid);
$locations = Service::Locations();
foreach ($locations as $location){
return json_encode($locations);//not sure if this will be needed
}
}
我现在对如何解析检索到的位置数据感到困惑,因为它可以返回到表单中,以便在位置下拉字段中使用。我猜可能需要json,但这是我所能做到的 这可能不是您想要的答案,但我建议您使用一个很棒的模块来实现这一点 这是提供的示例
// 1. Create a callable function that returns an array of options for the DependentDropdownField.
// When the value of the field it depends on changes, this function is called passing the
// updated value as the first parameter ($val)
$datesSource = function($val) {
if ($val == 'one') {
// return appropriate options array if the value is one.
}
if ($val == 'two') {
// return appropriate options array if the value is two.
}
};
$fields = FieldList::create(
// 2. Add your first field to your field list,
$fieldOne = DropdownField::create('FieldOne', 'Field One', array('one' => 'One', 'two' => 'Two')),
// 3. Add your DependentDropdownField, setting the source as the callable function
// you created and setting the field it depends on to the appropriate field
DependentDropdownField::create('FieldTwo', 'Field Two', $datesSource)->setDepends($fieldOne)
);
1.我认为ajax应该有“success”参数,用于处理返回数据。另外,当你有foreach循环和'return'时,循环只会持续1圈,不会重复,因为你用'return'打破它,我建议uou tu返回整个数组($locations),并在AJAX成功函数中用JS处理:)我见过这个插件,但我认为在这种情况下它对我不起作用。每个服务都可以有自己的位置列表,总共有30多个服务。如果我必须使用这个插件为每一个应用程序编写自己选择的服务代码,那么最终不会为我节省太多时间。您可以定义一个php函数,从一个下拉列表中获取所选的值,然后返回第二个下拉列表的选项。关键是,所有ajax内容都是为您编写的,您只需处理一个php函数,即“如果选择了此服务,则返回这些位置”,或者根据您的具体要求,处理一个php函数,反之亦然。您是否介意编辑您的答案并提供一个示例?仅链接的答案被认为对stackoverflow无效,可能会被删除。
// 1. Create a callable function that returns an array of options for the DependentDropdownField.
// When the value of the field it depends on changes, this function is called passing the
// updated value as the first parameter ($val)
$datesSource = function($val) {
if ($val == 'one') {
// return appropriate options array if the value is one.
}
if ($val == 'two') {
// return appropriate options array if the value is two.
}
};
$fields = FieldList::create(
// 2. Add your first field to your field list,
$fieldOne = DropdownField::create('FieldOne', 'Field One', array('one' => 'One', 'two' => 'Two')),
// 3. Add your DependentDropdownField, setting the source as the callable function
// you created and setting the field it depends on to the appropriate field
DependentDropdownField::create('FieldTwo', 'Field Two', $datesSource)->setDepends($fieldOne)
);