ZF2如何将模型对象传递给Bootstrap Modal

时间:2015-03-27 15:28:17

标签: jquery twitter-bootstrap zend-framework2 modal-dialog

我有一个索引视图,我在其中迭代一组模型对象。可以删除每个模型对象。单击删除按钮后,将使用模态。

<div class="panel-group ng-isolate-scope">
    <table class="table">
        <tr>
            <th>Id</th>
            <th>
                <a href="<?php echo $this->url('label', array('order_by' => 'name', 'order' => $url_order)); ?>">
                    Name 
                    <?php if ($order_by == 'name'): ?>
                        <i class="icon-chevron-<?php echo $url_order == 'ASC' ? 'down' : 'up' ?>"></i>
                    <?php endif; ?>
                </a>
            </th>
        </tr>
        <?php foreach ($paginator as $label) : ?>
            <tr>
                <td><?php echo $label->getId(); ?></td>
                <td><?php echo $label->getName(); ?></td>
                <td>
                    <a href="<?php echo $this->url('label', array('controller' => 'label', 'action' => 'edit', 'id' => $label->getId())); ?>">
                        Edit
                    </a>
                </td>
                <td>
                    <a data-toggle="modal" data-id="<?php echo $label->getId() ?>" data-target="#delete-label-modal" href="<?php echo $this->url('label', array('controller' => 'label', 'action' => 'delete', 'id' => $label->getId())); ?>">Delete</a></td>
            </tr>
        <?php endforeach; ?>
    </table>
</div>
<!--/ Delete Label Modal -->
<?= $this->partial('deleteLabel', array('label' => $label)) ?>

deleteLabel部分看起来像这样:

<div class="modal fade" id="delete-label-modal" tabindex="-1" role="dialog" aria-labelledby="delete-label-modal-label" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="delete-label-modal-label"><?php echo $this->escapeHtml($title); ?></h4>
            </div>
            <div class="modal-body">
                Are you sure that you want to delete
                '<?php echo $this->escapeHtml($label->getName()); ?>'?
            </div>

            <?php
            $url = $this->url('label', array(
                'action' => 'delete',
                'id' => $label->getId(),
            ));
            ?>
            <div class="modal-footer">
                <form action="<?php echo $url; ?>" method="post">
                    <div>
                        <input type="hidden" name="id" value="<?php echo (int) $label->getId(); ?>" />
                        <input type="submit" class="btn btn-primary" name="delete" value="Yes" />
                        <input type="submit" class="btn btn-default" name="delete" value="No" />
                    </div>
                </form>            
            </div>
        </div>
    </div>
</div>

初始代码的问题是它只在迭代后传递最后一个标签,当然它应该根据点击标签的data-id传递标签。

有没有人可以帮我解决如何在点击后获取模型对象并将其传递给模态?

1 个答案:

答案 0 :(得分:0)

这导致你的部分通话不在你的循环中;)

你应该做那样的事情:

<?php $renderPartial = ''; ?> 
<?php foreach ($paginator as $label) : ?>
            <tr>
                <td><?php echo $label->getId(); ?></td>
                <td><?php echo $label->getName(); ?></td>
                <td><a href="<?php echo $this->url('label', array('controller' => 'label', 'action' => 'edit', 'id' => $label->getId())); ?>">Edit</a></td>
                <td><a data-toggle="modal" data-id="<?php echo $label->getId() ?>" data-target="#delete-label-modal" href="<?php echo $this->url('label', array('controller' => 'label', 'action' => 'delete', 'id' => $label->getId())); ?>">Delete</a></td>
            </tr>
    <?php $renderPartial .= $this->partial('deleteLabel', array('label' => $label)) ?>
<?php endforeach; ?>
...
<?= $renderPartial; ?>