【问题标题】:Zend_Form : data table with checkboxesZend_Form : 带有复选框的数据表
【发布时间】:2011-08-11 15:05:27
【问题描述】:

我想在我的表单元素中有一个来自数据库的数据表,如下所示:

+-----+-------------------------+-----------------------+
|     | Number                  | Name                  |
+-----+-------------------------+-----------------------+
| [ ] | 123                     | ABC                   |
+-----+-------------------------+-----------------------+
| [x] | 456                     | DEF                   |
+-----+-------------------------+-----------------------+
| [x] | 789                     | HIJ                   |
+-----+-------------------------+-----------------------+

它允许选择多行,例如 MultiCheckBox 元素。

这是我想要的标记类型:

<table>
<thead>
  <tr>
    <th>Select</th>

    <th>Number</th>

    <th>Name</th>
  </tr>
</thead>

<tr>
  <td><input type="checkbox" name="subscribers[]" value="1234"></td>

  <td>1234</td>

  <td>ABC</td>
</tr>

<tr>
  <td><input type="checkbox" name="subscribers[]" value="375950"></td>

  <td>375950</td>

  <td>DEF</td>
</tr>

<!-- and so on... -->

我可以手动完成,但使用 Zend_Form 可以让我填充表单、轻松检索值并进行验证。我的表单中还有其他正常元素。

关于如何使用 Zend_Form 实现这一点的任何想法?也许是自定义元素和装饰器?

谢谢。如果需要,请询问更多信息。

这个问题好像是相关的:Zend_Form: Database records in HTML table with checkboxes

马克

【问题讨论】:

    标签: zend-framework zend-form


    【解决方案1】:

    好的,所以这将是一个更长的答案

    表格

    <?php
    class Form_MyTest extends Zend_Form
    {
      public function init()
      {
        $element = $this->createElement('multiCheckbox', 'subscribers');
        $element->setOptions(array('value1' => 'label1', 'value2' => 'label2'));
        $this->addElement($element);
    
        // ... other elements
      }
    }
    

    控制器

    <?php
    class MyController extends Zend_Controller_Action
    {
      public function myTestAction()
      {
        $form = new Form_MyTest();
    
        // ... processing logics
    
        $this->view->assign('form', $form);
      }
    }
    

    查看

    <form action="<?php echo $this->form->getAction(); ?>" method="<?php echo $this->form->getMethod(); ?>">
    <table>
        <thead>
            <tr>
                <th>Select</th>
                <th>Number</th>
                <th>Name</th>
            </tr>
        </thead>
        <?php $values = $this->form->getElement('subscribers')->getValue(); ?>
        <?php foreach($this->form->getElement('subscribers')->getMultiOptions() as $key => $value) : ?>
    
        <tr>
          <td><input type="checkbox" name="subscribers[]" id="subscribers-<?php echo $key; ?>" value="<?php echo $key; ?>" <?php echo in_array($key, $values) ? 'checked="checked"':''; ?>/></td>
          <td><label for="subscribers-<?php echo $key; ?>"><?php echo $key; ?></label></td>
          <td><label for="subscribers-<?php echo $key; ?>"><?php echo $value; ?></label></td>
        </tr>
    <?php endforeach; ?>
    </table>
    <!-- rest of form -->
    </form>
    

    这里发生了几件事。
    我从表单对象中获取预填充的值:

    <?php $values = $this->form->getElement('subscribers')->getValue(); ?>
    

    我根据上面的数组将每个复选框标记为选中或未选中

    <?php echo in_array($key, $values) ? 'checked="checked"':''; ?>
    

    编辑回应评论 B/C 评论不支持前块

    $element->setOptions(

    $element->setMultiOptions(

    只接受 key => value 对,所以你想要在 key value 对之外做的任何事情都会有点奇怪。如果您的程序允许您可以将另一个变量传递给视图,一个使用与 multiCheckbox 相同的键的数组,所以

    $this->view->assign('more_datums', array('value1' => array('col_1' => 'col_1_val'[, ...])));

    然后在视图中的foreach中使用

    $this->more_datums[$key]['col_1']

    【讨论】:

    • 另外,我可以使用更多的查看器。我急于表达我的观点。 TLDR:手动写出你的 html
    • 非常感谢您这么长的回答!我还没有测试过,但是如果我必须在表中放置两列以上怎么办?我看到您使用 $key 和 $value 作为数字和名称。我想标签中的分隔符可以。
    • 我在答案底部添加了一段文字,请查看
    猜你喜欢
    • 2013-03-25
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多