2014-11-25 44 views
2

是否有任何方法可以使ListBoxField通過選擇另一個ListBoxField自動更改其值?第二個ListBox應該依賴於第一個ListBox選擇。在Silverstripe的另一個字段選項上更改列表框選項

在我的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; 
+0

存在對相關的下拉列表中的一個插件:https://github.com/sheadawson/silverstripe-dependentdropdownfield但是這一點也適用DropdownFields,不ListboxFields在你的代碼(所以沒有「setMultiple」這裏的選項) – schellmax 2014-11-25 13:35:58

回答

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) 
); 
相關問題