0

我想在 zf2 中制作的项目中放置一个下拉列表...我浪费了一整天,但我只有一个静态下拉列表,而不是动态下拉列表。谁能帮我解决这个问题??

用户窗体.php

$this->add(array(
            'name' => 'group_name',
            'type' => 'select',  
            'attributes' => array(
                'id'=>'group_name',
                'class'=>'large',
                'options' => array('1=>php','2'=>'java'),
            ),
            'options' => array(
                'label' => '',
            ),
        ));

提前感谢您的宝贵回答。

4

5 回答 5

0

这就是我所做的:

在我的表单构造函数中

    $this->add(array(
        'type' => 'Zend\Form\Element\Select',
        'name' => 'color',
        'options' => array(
            'empty_option' => 'Select a Color',
            'value_options' => self::getColors(),
            'label' => 'Color',
        ),
    ));

在表单类中,我创建了这个方法:

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>
于 2013-03-14T03:26:48.057 回答
0

尝试这个:

$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'
        ),
    ),
));
于 2013-03-13T11:37:39.073 回答
0

从抽象的层面考虑。

  • 你有一个表格
  • 表单需要来自外部的数据

所以最终你的 Form 有一个Dependency. 由于我们从官方文档中了解到,有两种类型的Dependency-Injectionaka DI。Setter 注入和构造函数注入。就个人而言(!)我在这些情况下使用一种或另一种:

如果依赖项是功能正常工作的绝对要求,则使用构造函数注入

如果依赖项或多或少是可选的以扩展已经工作的东西,则使用Setter-Injection

对于您的表单,它是必需的依赖项(因为没有它,就没有填充的 Select-Element),因此我将为您提供构造函数注入的示例。

您的控制器的一些操作:

$sl   = $this->getServiceLocator();
$dbA  = $sl->get('Zend\Db\Adapter\Adapter');
$form = new SomeForm($dbA);

这就是表格的全部内容。人口现在发生在您的表单中。这只是一个例子,可能需要一些微调,但你会明白的:

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,仅用于我认为文档将如何工作!这可能是需要一些优化的部分。但总而言之,您将了解它是如何完成的。

于 2013-03-14T10:55:01.393 回答
0

刚遇到同样的问题,不得不查看 zf2 源代码。这是一个更多的OOP解决方案:

在表单构造函数内部:

        $this->add(array(
        'name' => 'customer',
        'type' => 'Zend\Form\Element\Select',
        'attributes' => array(
            'options' => array(
                0 => 'Kunde',
            )
        ),
        'options' => array(
                'label' => 'Kunde'
    )));

控制器内部:

    $view->form = new SearchForm();
    $customers = $view->form->get('customer')->getValueOptions();
    $customers[] = 'Kunde1';
    $customers[] = 'Kunde2';
    $customers[] = 'Kunde3';
    $customers[] = 'Kunde4';
    $view->form->get('customer')->setValueOptions($customers);
于 2014-08-06T13:03:30.953 回答
0

在您的控制器中,您可以执行以下操作;

在我的第一个示例中,假设您有一个组表。然后我们将获取组表中的所有数据;我们需要在选择选项中显示 id 和 name;

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')); 
?>

或者你可以对一个控制器助手,你可以查看这个链接http://www.resourcemode.com/me/?p=327

于 2013-06-24T10:07:39.020 回答