更改Silverstripe中其他字段选择的列表框选项

时间:2014-11-25 11:07:30

标签: php silverstripe

有没有办法让ListBoxField通过选择其他ListBoxField来自动更改其值?第二个ListBox应取决于第一个ListBox选择。

在我的Silverstripe 3后端,我有两个ListBoxField s。更改类别 Listbox后,位置 Listbox应更改可供选择的选项。

$fields = new FieldList(
    TextField::create('Title', 'Title'),
    UploadField::create('File', 'File')->setFolderName('Uploads/Files')->setAllowedExtensions(array('odt', 'jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pdf')),
    ListboxField::create('Categories', 'Categories')->setMultiple(true)->setSource(Category::get()->map('ID', 'Title'))->setAttribute('data-placeholder', 'Click to select'),
    ListboxField::create('Locations', 'Locations')->setMultiple(true)->setSource(Location::get()->map('ID', 'Title'))->setAttribute('data-placeholder', 'Click to select')
);
return $fields;

2 个答案:

答案 0 :(得分:0)

默认不是。

您可以尝试使用此模块:https://github.com/unclecheese/silverstripe-display-logic

答案 1 :(得分:0)

我正在回应@schellmax评论使用...

  • github.com/sheadawson/silverstripe-dependentdropdownfield

这允许一个下拉菜单自动更新另一个dorp的更改。您创建一个使用所选值调用的简单函数,该函数返回依赖下拉列表的值。

// 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)
);