fromholdio / silverstripe-dependentgroupeddropdownfield
一个基于ajax填充选项的silverstripe分组下拉字段,其选项基于依赖字段的值
3.0.0
2023-08-10 20:47 UTC
Requires
- php: >=7.4
- silverstripe/framework: ~5.0
- silverstripe/vendor-plugin: ~2.0
README
一个通过ajax填充选项的SilverStripe分组下拉字段,其选项基于依赖的下拉字段。
注意:这基本上是将Shea Dawson的silverstripe-dependentdropdownfield直接移植,但将ajax应用于GroupedDropdownField
而不是DropdownField
。
需求
SilverStripe 4
安装
composer require fromholdio/silverstripe-dependentgroupeddropdownfield
这究竟是什么?
请参阅用户指南以查看使用截图。
使用示例
首先,创建一个DropdownField
,它的新字段的值将依赖于它。
$typeField = DropdownField::create( 'ProductType', 'Product Type', [ 'animal' => 'Animal', 'food' => 'Food' ] ); $typeField->setEmptyString('Please select a product type');
注意:原始下拉字段不需要为空字符串。如果没有,它的第一个值将被用来自动将optgroups/options加载到依赖字段中。
其次,创建一个可调用的函数,该函数返回一个适合GroupedDropdownField的$source参数的数组。(一个二维数组;第一层用于optgroup,第二层用于每个组的每个项。)
$productFieldSource = function($value) { if ($value === 'animal') { return [ 'Fun' => [ 'puppy' => 'Puppy', 'dog' => 'Dog' ], 'Evil' => [ 'cat' => 'Cat' ] ]; } if ($value === 'food') { return [ 'Fruit' => [ 'apple' => 'Apple', 'orange' => 'Orange' ], 'Vegetables' => [ 'carrot' => 'Carrot', 'celery' => 'Celery' ] ]; } return []; };
现在,创建您的DependentGroupedDropdownField
,将源设置为上面创建的可调用函数。
$productField = DependentGroupedDropdownField::create( 'Product', 'Product', $productFieldSource );
确保连接字段
$productField->setDepends($typeField);
现在您可以开始了。
$fields->addFieldsToTab( 'Root.Testing', [ $typeField, $productField ] );