Zend_Form :带有复选框的数据表

发布于 2024-11-29 03:15:02 字数 1534 浏览 0 评论 0原文

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

+-----+-------------------------+-----------------------+
|     | 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: HTML table 中的数据库记录带复选框

Marc

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

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

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.

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

Thanks. Ask for more info if needed.

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

Marc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心不设防 2024-12-06 03:15:02

好的,这将是一个更长的表格

答案

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

只接受 key =

$element->setMultiOptions(

>值对,因此您想要在键值对之外执行的任何操作都会有点奇怪。如果您的程序允许您可以将另一个变量传递给视图,则该数组使用与 multiCheckbox 相同的键,因此

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

然后在视图中的 foreach 中使用

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

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

the Form

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

Controller

<?php
class MyController extends Zend_Controller_Action
{
  public function myTestAction()
  {
    $form = new Form_MyTest();

    // ... processing logics

    $this->view->assign('form', $form);
  }
}

View

<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"':''; ?>

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

$element->setOptions(

or

$element->setMultiOptions(

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'[, ...])));

and then in the foreach in the view use

$this->more_datums[$key]['col_1']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文