Zend_Form :带复选框的数据表 [英] Zend_Form : data table with checkboxes

查看:18
本文介绍了Zend_Form :带复选框的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to have a table of data coming from the DB in my form element, looking like the following :

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

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

It would allow to select several rows, like the MultiCheckBox element.

这是我想要的标记类型:

Here is the kind of markup I would like to have:

<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 可以让我填充表单、轻松检索值并进行验证.我的表单中有其他普通元素.

I can do it by hand but using Zend_Form would allow me to populate the form, retrieve the values easily and to have validation. I have other normal elements in my form.

知道如何使用 Zend_Form 实现这一点吗?也许是自定义元素和装饰器?

Any idea on how to achieve this with Zend_Form ? Maybe a custom element and decorator ?

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

Thanks. Ask for more info if needed.

这个问题似乎是相关的:Zend_Form:数据库记录在带有复选框的 HTML 表格中

This question seems to be related: Zend_Form: Database records in HTML table with checkboxes

马克

推荐答案

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

ok, so this is going to be a longer sort of answer

表格<代码>

<?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>

这里正在发生一些事情.
我从表单对象中获取预先填充的值:
<代码>

A couple things are happening here.
I get the prepopulated values out of the form object:

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

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

I mark each checkbox as checked or not based on the array above

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

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

EDIT IN RESPONSE TO COMMENT B/C COMMENTS DON'T SUPPORT PRE BLOCKS the

$element->setOptions(

$element->setMultiOptions(

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

only accepts key => value pairs, so anything you want to do outside of key value pairs is going to be a little wierd. If your program allows you could pass another variable to the view, an array that uses the same keys as the multiCheckbox so

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

然后在foreach中的视图中使用

and then in the foreach in the view use

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

这篇关于Zend_Form :带复选框的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆