更改银条中另一个字段选择的列表框选项


Change Listbox options on another field selection in Silverstripe

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

在我的 Silverstripe 3 后端,我有两个ListBoxField。更改类别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;

默认情况下不是。

你可以试试这个模块:https://github.com/unclecheese/silverstripe-display-logic

我正在回应@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)
);