gpt4 book ai didi

php - 如何在 Zend Framework 2 表单的第一个

转载 作者:可可西里 更新时间:2023-11-01 00:48:23 25 4
gpt4 key购买 nike

我正在使用 Zend Framework 2 开发一个应用程序,并且我正在使用两个多选框。第一个填充了数据库中的数据,第二个为空。

我打算在两个选择的第一个选项中将 disabled 属性设置为“disabled”。这样,第一个选项将无法使用且不可点击。因此,用户在使用添加/删除按钮时将无法将这些第一个选项从一个选择传递到另一个选择。

选择 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 ProductCatalog\Entity\Attribute\Attribute a ORDER BY a.internalName ASC");
$attributes = $query->getResult();

$select = new Element\Select('idAttributes');
$select->setAttribute('title', 'Select an Attribute')
->setAttribute('size', 7)
->setAttribute('multiple', 'multiple')
->setAttribute('id', 'AttributesId');

$selected = new Element\Select('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')
);

Disabled 是有效属性之一。

关于php - 如何在 Zend Framework 2 表单的第一个 <option> 上将禁用属性设置为禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12915689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com