Zend framework2 动态下拉菜单进入zf2?
我想把我的项目是在zf2的下拉列表。。。我浪费了一整天,但我只得到了一个静态下拉列表,而不是动态。有人能帮我解决这个问题吗 UserForm.phpZend framework2 动态下拉菜单进入zf2?,zend-framework2,Zend Framework2,我想把我的项目是在zf2的下拉列表。。。我浪费了一整天,但我只得到了一个静态下拉列表,而不是动态。有人能帮我解决这个问题吗 UserForm.php $this->add(array( 'name' => 'group_name', 'type' => 'select', 'attributes' => array( 'id'=>'group_name',
$this->add(array(
'name' => 'group_name',
'type' => 'select',
'attributes' => array(
'id'=>'group_name',
'class'=>'large',
'options' => array('1=>php','2'=>'java'),
),
'options' => array(
'label' => '',
),
));
提前感谢您的宝贵回答。试试以下方法:
$this->add(array(
'name' => 'group_name',
'type' => 'select',
'attributes' => array(
'id'=>'group_name',
'class'=>'large',
),
'options' => array(
'label' => '',
'value_options' => array(
'1' => 'php',
'2' => 'java'
),
),
));
这就是我所做的:
在我的窗体的构造函数中
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'color',
'options' => array(
'empty_option' => 'Select a Color',
'value_options' => self::getColors(),
'label' => 'Color',
),
));
在form类中,我创建了以下方法:
public static function getColors() {
// access database here
//example return
return array(
'blue' => 'Blue',
'red' => 'Red',
);
}
在我的视图脚本中:
<div class="form_element">
<?php $element = $form->get('color'); ?>
<label>
<?php echo $element->getOption('label'); ?>
</label>
<?php echo $this->formSelect($element); ?>
</div>
从抽象的角度来思考
- 你有一张表格
- 表单需要来自外部的数据
依赖关系
。从中我们了解到,有两种类型的依赖注入
akaDI
。Setter注入和构造函数注入。就个人而言(!)我在这些情况下使用其中一种:
构造函数注入如果依赖项是功能工作的绝对要求
Setter Injection如果依赖项或多或少是可选的,可以扩展已经工作的内容
在您的表单中,它是一个必需的依赖项(因为没有它就没有填充的Select元素),因此我将为您提供一个构造函数注入的示例
控制器的某些操作:
$sl = $this->getServiceLocator();
$dbA = $sl->get('Zend\Db\Adapter\Adapter');
$form = new SomeForm($dbA);
$view->form = new SearchForm();
$customers = $view->form->get('customer')->getValueOptions();
$customers[] = 'Kunde1';
$customers[] = 'Kunde2';
$customers[] = 'Kunde3';
$customers[] = 'Kunde4';
$view->form->get('customer')->setValueOptions($customers);
这就是表格的全部内容。人口现在发生在您的表单中。这只是一个示例,可能需要一些微调,但您会明白:
class SomeForm extends \Zend\Form
{
public function __construct(\Zend\Db\Adapter\Adapter $dbA)
{
parent::__construct('my-form-name');
// Create all the form elements and stuff
// Get Population data
$popData = array();
$result = $dbA->query('SELECT id, title FROM Categories', $dbA::QUERY_MODE_EXECUTE)->toArray();
foreach ($result as $cat) {
$popData[$cat['id'] = $cat['title'];
}
$selectElement = $this->getElement('select-element-name');
$selectElement->setValueOptions($popData);
}
}
重要:我不知道
Zend\Db
上面的代码只是我认为它将如何工作!这可能是需要一些优化的部分。但总的来说,你会知道它是如何完成的。在你的控制器中,你可以做如下的事情在我的第一个示例中,假设您有一个组表。然后我们将获取组表中的所有数据; 我们需要在选择选项中显示id和名称
public function indexAction()
{
$groupTable = new GroupTable();
$groupList = $groupTable->fetchAll();
$groups = array();
foreach ($groupList as $list) {
$groups[$list->getId()] = $list->getName();
}
$form = new UserForm();
$form->get('group_name')->setAttributes(array(
'options' => $groups,
));
}
或
在本例中,组列表是硬编码的
public function indexAction()
{
$groupList = array('1' => 'PHP', '2' => 'JAVA', '3' => 'C#');
$groups = array();
foreach ($groupList as $id => $list) {
$groups[$id] = $list;
}
$form = new UserForm();
$form->get('group_name')->setAttributes(array(
'options' => $groups,
));
}
然后在您的视图脚本中
<?php
$form = $this->form;
echo $this->formRow($form->get('group_name'));
?>
或者您可以找到控制器帮助程序,您可以检查此链接刚刚遇到相同的问题,必须查看zf2源代码。 下面是一个更为面向对象的解决方案: 在表单构造函数中:
$this->add(array(
'name' => 'customer',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'options' => array(
0 => 'Kunde',
)
),
'options' => array(
'label' => 'Kunde'
)));
控制器内部:
$sl = $this->getServiceLocator();
$dbA = $sl->get('Zend\Db\Adapter\Adapter');
$form = new SomeForm($dbA);
$view->form = new SearchForm();
$customers = $view->form->get('customer')->getValueOptions();
$customers[] = 'Kunde1';
$customers[] = 'Kunde2';
$customers[] = 'Kunde3';
$customers[] = 'Kunde4';
$view->form->get('customer')->setValueOptions($customers);
不,这是只显示静态下拉列表,但我想要动态的,从数据库中获取值并显示它。你们尝试了什么?我在你的问题中没有看到任何与数据库相关的代码,也没有变量
$options
。嘿,布拉姆,如果你对从第一步到最后一步的动态下拉列表有任何建议,请帮助我。。我是zf2新手。它并不比从数据库中检索所需的值(使用PDO、MysqlI、ZendDB、Doctrine 2或您正在使用的任何东西)然后将结果转换为选择列表所需的数据结构复杂。这是一个具有键/值对的简单数组。