【问题标题】:Change Listbox options on another field selection in Silverstripe更改 Silverstripe 中另一个字段选择的列表框选项
【发布时间】:2014-11-25 11:07:29
【问题描述】:

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

在我的 Silverstripe 3 后端我有两个 ListBoxFields。当 Category Listbox 更改时,Locations 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;

【问题讨论】:

标签: php silverstripe


【解决方案1】:

默认情况下不是。

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

【讨论】:

  • 如果下拉列表非常静态并且您可以在单独的字段中生成一堆第二个标签,则可能会很方便。但你不能用它来切换标签字段的选项。
【解决方案2】:

我正在呼应@schellmax 评论以使用...

  • github.com/sheadawson/silverstripe-dependentdropdownfield

这允许一个下拉菜单在另一个 dorp down 更改时自动更新。您创建了一个使用选定值调用的简单函数,该函数返回相关下拉列表的值。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多