格式化表格列中的

格式化表格列中的

本文介绍了使用 zend-decorator 格式化表格列中的 Zend_Form_Element_Radio,行中的其他 Zend_Form_Elements的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用装饰器将下面的 Zend_Form 格式化为表格,将描述放在第一列第二列中的 Zend_Form_Element_Radio 选项 在每一行中添加 2 个 select,您可以在后面的 html 示例中看到.

我需要一个具体/有效的例子.

表格

class My_Form 扩展 Zend_Form{const KIND_1 = 'dineer1';const KIND_2 = 'dineer2';const KIND_3 = 'dineer3';const KIND_4 = 'dineer4';const KIND_5 = 'dineer5';const KIND_6 = 'dineer6';公共静态 $KINDS = 数组(1 =>自我::KIND_1,2 =>自我::KIND_2,3 =>自我::KIND_3,4 =>自我::KIND_4,5 =>自我::KIND_5,6 =>自我::KIND_6,);const DRINK_C = 'c';const DRINK_M = 'm';const DRINK_W = 'w';公共静态 $DRINKS = 数组(self::DRINK_C =>油菜",self::DRINK_M =>牛奶",self::DRINK_W =>水",);const FOOD_B = 'b';const FOOD_F = 'f';const FOOD_M = 'm';const FOOD_P = 'p';const FOOD_V = 'v';const FOOD_W = 'w';公共静态 $FOODS = 数组(self::FOOD_B =>汉堡包",self::FOOD_F =>水果",self::FOOD_M =>肉",self::FOOD_P =>比萨",self::FOOD_V =>蔬菜",self::FOOD_W =>"乌斯特尔",);公共函数 init(){$_please_select = array("" => "请选择");$this->setMethod(Zend_Form::METHOD_POST);$input_lunch = new Zend_Form_Element_Radio('午餐');$input_lunch ->setMultiOptions(self::$KINDS) ;$this->addElement($input_lunch);foreach (self::$KINDS as $k => $_descriprion) {$input_drink = new Zend_Form_Element_Select('drink_' . $k);$input_drink->addMultiOptions(self::$DRINKS);$input_food = new Zend_Form_Element_Select('food_' . $k);$input_food->addMultiOptions($_please_select)->addMultiOptions(self::$FOODS);$this->addElement($input_drink);$this->addElement($input_food);}}}

预期的 HTML

解决方案

我会根据这里的答案提出一个解决方案 https://stackoverflow.com/a/8451723/212940

您的表格:-

class My_Form 扩展 Zend_Form{const KIND_1 = 'dineer1';const KIND_2 = 'dineer2';const KIND_3 = 'dineer3';const KIND_4 = 'dineer4';const KIND_5 = 'dineer5';const KIND_6 = 'dineer6';公共静态 $KINDS = 数组(1 =>自我::KIND_1,2 =>自我::KIND_2,3 =>自我::KIND_3,4 =>自我::KIND_4,5 =>自我::KIND_5,6 =>自我::KIND_6,);const DRINK_C = 'c';const DRINK_M = 'm';const DRINK_W = 'w';公共静态 $DRINKS = 数组(self::DRINK_C =>油菜",self::DRINK_M =>牛奶",self::DRINK_W =>水",);const FOOD_B = 'b';const FOOD_F = 'f';const FOOD_M = 'm';const FOOD_P = 'p';const FOOD_V = 'v';const FOOD_W = 'w';公共静态 $FOODS = 数组(self::FOOD_B =>汉堡包",self::FOOD_F =>水果",self::FOOD_M =>肉",self::FOOD_P =>比萨",self::FOOD_V =>蔬菜",self::FOOD_W =>"乌斯特尔",);公共函数 init(){$this->addDecorators(大批(数组('ViewScript', array('viewScript' => 'forms/_form_test.phtml'))));//添加为答案的一部分.请注意,所有默认装饰器仍然可用.$_please_select = array("" => "请选择");$this->setMethod(Zend_Form::METHOD_POST);$input_lunch = new Zend_Form_Element_Radio('午餐');$input_lunch ->setMultiOptions(self::$KINDS) ;$this->addElement($input_lunch);foreach (self::$KINDS as $k => $_descriprion) {$input_drink = new Zend_Form_Element_Select('drink_' . $k);$input_drink->addMultiOptions(self::$DRINKS);$input_food = new Zend_Form_Element_Select('food_' . $k);$input_food->addMultiOptions($_please_select)->addMultiOptions(self::$FOODS);$this->addElement($input_drink);$this->addElement($input_food);}$this->setElementDecorators(array('ViewHelper'));//作为答案的一部分添加}}

如您所见,它只需要两个小的更改.
然后您需要创建文件 scripts/forms/_form_test.phtml ,其中包含:-

<?php$elements = $this->element->getElements();$options = $this->element->lunch->getMultiOptions();foreach($options as $key => $option){echo "
";echo "<td>Description row $key</td>
";echo "<td><input type='radio' name='lunch' value='$option'</td>
";echo "<td>{$elements['drink_' . $key]}</td>
";echo "<td>{$elements['food_' . $key]}</td>
";echo "</tr>
";}?></tbody><表格></表单>

文件 _form_test.phtml 实际上是渲染表单的装饰器.这是使用 Zend_Form 的一种非常灵活的方式,我通过阅读 这篇文章学习了如何做到这一点

默认装饰器,例如error"应该仍然可以用于此方法.

在我的系统上,我得到了您要求的确切 html 输出.试试看.

I want use decorators to format as table the following Zend_Form, placing a description in the first column and the Zend_Form_Element_Radio's options in second column and add 2 select in every row as you can see in the html example later.

I need a concrete/working example.

class My_Form extends Zend_Form
{
    const KIND_1 = 'dineer1';
    const KIND_2 = 'dineer2';
    const KIND_3 = 'dineer3';
    const KIND_4 = 'dineer4';
    const KIND_5 = 'dineer5';
    const KIND_6 = 'dineer6';

    public static $KINDS = array(
        1 => self::KIND_1,
        2 => self::KIND_2,
        3 => self::KIND_3,
        4 => self::KIND_4,
        5 => self::KIND_5,
        6 => self::KIND_6,
    );

    const DRINK_C = 'c';
    const DRINK_M = 'm';
    const DRINK_W = 'w';

    public static $DRINKS = array(
        self::DRINK_C => "cole",
        self::DRINK_M => "milk",
        self::DRINK_W => "water",
    );

    const FOOD_B = 'b';
    const FOOD_F = 'f';
    const FOOD_M = 'm';
    const FOOD_P = 'p';
    const FOOD_V = 'v';
    const FOOD_W = 'w';

    public static $FOODS = array(
        self::FOOD_B => "burger",
        self::FOOD_F => "fruit",
        self::FOOD_M => "Meat",
        self::FOOD_P => "pizza",
        self::FOOD_V => "vegetables",
        self::FOOD_W => "Wursterl",
    );

    public function init()
    {
        $_please_select = array("" => " please select ");

        $this->setMethod(Zend_Form::METHOD_POST);

        $input_lunch = new Zend_Form_Element_Radio('lunch');
        $input_lunch ->setMultiOptions(self::$KINDS) ;
        $this->addElement($input_lunch );

        foreach (self::$KINDS as $k => $_descriprion) {
            $input_drink = new Zend_Form_Element_Select('drink_' . $k);
            $input_drink->addMultiOptions(self::$DRINKS);

            $input_food = new Zend_Form_Element_Select('food_' . $k);
            $input_food->addMultiOptions($_please_select)
                ->addMultiOptions(self::$FOODS);

            $this->addElement($input_drink);
            $this->addElement($input_food);
        }
    }
}
<html>
<body>
    <form action="/" method="POST">
    <table>
    <thead>
        <tr>
            <th></td>
            <th>kind</td>
            <th>drink</td>
            <th>food</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Description row 1</td>
            <td><input type="radio" name="lunch" value "dinner1"></td>
            <td>
                <select name="drink_1">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_1">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 2</td>
            <td><input type="radio" name="lunch" value "dinner2"></td>
            <td>
                <select name="drink_2">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_2">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 3</td>
            <td><input type="radio" name="lunch" value "dinner3"></td>
            <td>
                <select name="drink_3">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_3">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 4</td>
            <td><input type="radio" name="lunch" value "dinner4"></td>
            <td>
                <select name="drink_4">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_4">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 5</td>
            <td><input type="radio" name="lunch" value "dinner5"></td>
            <td>
                <select name="drink_5">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_5">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 6</td>
            <td><input type="radio" name="lunch" value "dinner6"></td>
            <td>
                <select name="drink_6">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_6">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        </tbody>
        <table>
    </form>
</body>
</html>
解决方案

I would suggest a solution based on this answer here https://stackoverflow.com/a/8451723/212940

Your form:-

class My_Form extends Zend_Form
{
    const KIND_1 = 'dineer1';
    const KIND_2 = 'dineer2';
    const KIND_3 = 'dineer3';
    const KIND_4 = 'dineer4';
    const KIND_5 = 'dineer5';
    const KIND_6 = 'dineer6';

    public static $KINDS = array(
        1 => self::KIND_1,
        2 => self::KIND_2,
        3 => self::KIND_3,
        4 => self::KIND_4,
        5 => self::KIND_5,
        6 => self::KIND_6,
    );

    const DRINK_C = 'c';
    const DRINK_M = 'm';
    const DRINK_W = 'w';

    public static $DRINKS = array(
        self::DRINK_C => "cole",
        self::DRINK_M => "milk",
        self::DRINK_W => "water",
    );

    const FOOD_B = 'b';
    const FOOD_F = 'f';
    const FOOD_M = 'm';
    const FOOD_P = 'p';
    const FOOD_V = 'v';
    const FOOD_W = 'w';

    public static $FOODS = array(
        self::FOOD_B => "burger",
        self::FOOD_F => "fruit",
        self::FOOD_M => "Meat",
        self::FOOD_P => "pizza",
        self::FOOD_V => "vegetables",
        self::FOOD_W => "Wursterl",
    );

    public function init()
    {
        $this->addDecorators(
            array(
                array('ViewScript', array('viewScript' => 'forms/_form_test.phtml'))
            )
        ); //added as part of answer. Note all default decorators are still available.

        $_please_select = array("" => " please select ");

        $this->setMethod(Zend_Form::METHOD_POST);

        $input_lunch = new Zend_Form_Element_Radio('lunch');
        $input_lunch ->setMultiOptions(self::$KINDS) ;
        $this->addElement($input_lunch );

        foreach (self::$KINDS as $k => $_descriprion) {
            $input_drink = new Zend_Form_Element_Select('drink_' . $k);
            $input_drink->addMultiOptions(self::$DRINKS);

            $input_food = new Zend_Form_Element_Select('food_' . $k);
            $input_food->addMultiOptions($_please_select)
                ->addMultiOptions(self::$FOODS);

            $this->addElement($input_drink);
            $this->addElement($input_food);
        }
        $this->setElementDecorators(array('ViewHelper'));//added as part of answer
    }
}

As you can see it requires only two small changes.
Then you need to create the file scripts/forms/_form_test.phtml which contains:-

<form
    id='contact'
    action='<?php echo $this->element->getAction(); ?>'
    method='<?php echo $this->element->getMethod(); ?>'
>
<table>
    <thead>
        <tr>
            <th></td>
            <th>kind</td>
            <th>drink</td>
            <th>food</td>
        </tr>
    </thead>
    <tbody>
        <?php
        $elements = $this->element->getElements();
        $options = $this->element->lunch->getMultiOptions();
        foreach($options as $key => $option){
            echo "<tr>
";
            echo "<td>Description row $key</td>
";
            echo "<td><input type='radio' name='lunch' value='$option'</td>
";
            echo "<td>{$elements['drink_' . $key]}</td>
";
            echo "<td>{$elements['food_' . $key]}</td>
";
            echo "</tr>
";
        }
        ?>
        </tbody>
        <table>
</form>

The file _form_test.phtml is effectively your decorator for rendering the form. This is a very flexible way of using Zend_Form and I learnt how to do this by reading this article here

The default decorators, such as 'error' should still be available with this method.

On my system I got the exact html output you asked for. Try it and see.

这篇关于使用 zend-decorator 格式化表格列中的 Zend_Form_Element_Radio,行中的其他 Zend_Form_Elements的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:06