我正在使用Zend Framework 2开发一个应用程序,我正在使用两个选择多个框。第一个填充来自数据库的数据,第二个为空。
我打算在两个选择的第一个选项中将禁用属性设置为"禁用"。这样,第一个选项将不可用且不可单击。因此,用户在使用添加/删除按钮时将无法将这些第一个选项从一个选择传递到另一个选择。
选择 1
<select id="AttributesId" multiple="multiple" size="7" title="Select an Attribute" name="idAttributes[]">
<option value="0">Please Select an Attribute</option>
<option value="1"> Attribute 1</option>
<option value="2"> Attribute 2</option>
</select>
选择 2
<select id="SelectedAttributesId" multiple="multiple" size="7" title="Selected Attributes" name="selectedAttributes[]">
<option value="0">Current Selection</option>
</select>
在 ZF2 上生成两个选择的 php 代码是:
(...
public function __construct ($em = null)
{
parent::__construct("typeset");
$this->setAttribute("method", "post")
->setAttribute("class", "contact-form");
if(null !== $em)
{
$this->setEntityManager($em);
}
$em = $this->getEntityManager();
$query = $em->createQuery("SELECT a.idAttribute, a.internalName FROM ProductCatalogEntityAttributeAttribute a ORDER BY a.internalName ASC");
$attributes = $query->getResult();
$select = new ElementSelect('idAttributes');
$select->setAttribute('title', 'Select an Attribute')
->setAttribute('size', 7)
->setAttribute('multiple', 'multiple')
->setAttribute('id', 'AttributesId');
$selected = new ElementSelect('selectedAttributes');
$selected->setAttribute('title', 'Selected Attributes')
->setAttribute('size', 7)
->setAttribute('multiple', 'multiple')
->setAttribute('id', 'SelectedAttributesId');
$labelIdAttributes = 'Attributes List: ';
$labelSelectedAttributes = 'Selected Attributes List: ';
$options[0] = 'Please Select an Attribute';
此后续行不起作用,但您可以了解我需要什么$options[0]->setAttribute('deselect', 'deselect');
foreach ($attributes as $key => $value)
{
$options[$value['idAttribute']] = $value['internalName'];
}
$selectedOptions[0] = 'Current Selection';
$select->setLabel($labelIdAttributes)
->setValueOptions($options);
$selected->setLabel($labelSelectedAttributes)
->setValueOptions($selectedOptions);
$this->add($select);
$this->add($selected);
(...)
如果我
没记错的话,您需要稍微不同地格式化选项数组:
$options = array(
array('value' => '0', 'label' => ' Please Select an Attribute', 'disabled' => 'disabled'),
array('value' => '1', 'label' => ' Attribute 1'),
array('value' => '2', 'label' => ' Attribute 2')
);
禁用是有效属性之一。
只需获取表单元素并使用禁用属性
$form->get('select_element')->setAttribute('disabled', 'disabled');