runopencode / dm-related-selects-plugin
dmRelatedSelectsPlugin - 相关选择表单小部件
Requires
- composer/installers: dev-master
This package is auto-updated.
Last update: 2022-02-01 12:22:34 UTC
README
作者:TheCelavi
版本:0.5.0
稳定性:Alpha(测试和改进)
日期:2012年12月5日
由Run Open Code提供
许可:免费
描述
在你有一个或多个实体作为一对一关系相关联的情况下,例如
Category
-> Sub-category
-> Product
以及像PurchasedItem
这样的实体,其中PurchasedItem
与Product
存在一对一关系,用户输入的单个Product
可能很难在几十、几百等中选择正确的。
因此,这类用例场景通过相关选择进行筛选来解决。
用例场景可以解释为
- 用户从分类选择字段中选择产品的分类
- 子分类选择字段将填充所选分类的子分类
- 用户选择产品的子分类
- 产品选择字段将填充所选分类和子分类的产品
注意:这只是说明此插件解决的问题类型。
如何使用它?
此插件包含扩展sfWidgetFormDoctrineChoice
的sfWidgetFormDoctrineRelatedSelect
小部件类。它还包括两个JavaScript文件
- jquery.relatedselects.js
- launch.js
这两个文件是必需的,因此如果你在前面组件中使用此表单小部件,请记住添加以下代码
public function executeExample(dmWebRequest $request)
{
$this->form = new MyFormWithRelatedSelects();
// IMPORTANT: Adding JSs from FORM to response!
foreach ($jscripts = $this->form->getJavaScripts() as $js) {
$this->getResponse()->addJavaScript($js);
}
....
}
注意,jquery.relatedselects.js基于jQuery相关选择插件,但代码已被修改以适应此插件。
当你安装了此插件后,在你的表单中,只需添加sfWidgetFormDoctrineRelatedSelect
而不是普通的选择即可。
配置
该小部件扩展了sfWidgetFormDoctrineChoice
,因此它使用了其中的一些选项
model
:填充SELECTS的模型key_method
:从选项中获取值属性的默认方法为getPrimaryKey
method
:从选项中获取标签的默认方法为__toString
table_method
:根据提供的'model'选项填充SELECTS。然而,您可以在模型的table类中创建特定的table方法,该方法可以用于。
sfWidgetFormDoctrineRelatedSelect
使用上述选项并扩展了更多选项。
必需的选项有
group
:相关的选择项被分组。组名的重要性不如组名的唯一性。因此,对于相关的选择项,组名必须相同且与其他组名不同。index
:每个字段都有一个索引。最上面的字段索引值为0。下一个字段的索引值为1,依此类推。请注意,索引必须从0开始,逐个增加(例如:0,1,2...)。
可选选项包括:
parent
:如果相关的选择项有父类(例如:Product位于ProductCategory中),则模型必须声明,以便可以进行过滤。active_only
:在检索对象时,如果active_only
为true
,生成的查询将包含条件WHERE is_active = true
国际化支持
是的!此插件支持国际化。
示例
假设我们有以下实体
Category:
columns:
title: {type: string(255)}
SubCategory:
columns:
title: {type: string(255)}
category_id: {type: integer, notnull: true}
relations:
Category:
class: Category
local: category_id
foreignAlias: SubCategories
Product:
columns:
title: {type: string(255)}
sub_category_id: {type: integer, notnull: true}
relations:
SubCategory:
class: SubCategory
local: sub_category_id
foreignAlias: Products
PurchasedItem:
columns:
title: {type: string(255)}
product_id: {type: integer, notnull: true}
relations:
Product:
class: Product
local: product_id
foreignAlias: PurchasedItems
假设您想要创建一个用于创建/编辑PurchasedItem
的表单?购买项目将使用一个带有产品列表的SELECT控件。如果您有数百种不同的产品,正确选择产品就很困难。
因此,可以通过使用相关选择项过滤产品来解决这个问题
class MyPurchasedItemForm extends dmForm
{
public function configure()
{
$this->widgetSchema['category'] = new sfWidgetFormDoctrineRelatedSelect(array(
'add_empty' => 'Choose category',
'model' => 'Category',
'group' => 'my_group',
'index' => 0,
'active_only' => true
));
$this->validatorSchema['category'] = new sfValidatorDoctrineChoice(array(
'required' => false,
'model' => 'Category'
));
$this->widgetSchema['sub_category'] = new sfWidgetFormDoctrineRelatedSelect(array(
'add_empty' => 'Choose sub category',
'model' => 'SubCategory',
'group' => 'my_group',
'index' => 1,
'active_only' => true,
'parent' => 'Category'
));
$this->validatorSchema['sub_category'] = new sfValidatorDoctrineChoice(array(
'required' => false,
'model' => 'SubCategory'
));
$this->widgetSchema['product'] = new sfWidgetFormDoctrineRelatedSelect(array(
'add_empty' => 'Choose product',
'model' => 'Product',
'group' => 'my_group',
'index' => 2,
'active_only' => true,
'parent' => 'SubCategory'
));
$this->validatorSchema['product'] = new sfValidatorDoctrineChoice(array(
'required' => false,
'model' => 'Product'
));
// ... Some more code here ...
parent::configure();
}
}
因此,示例是自我解释的。请注意index
选项及其值,以及谁配置了parent
选项,谁没有配置。