【问题标题】:ZF2 Howto pass model object to Bootstrap ModalZF2 如何将模型对象传递给 Bootstrap Modal
【发布时间】:2015-03-27 15:28:16
【问题描述】:

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

<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 传递标签。

谁能帮我在点击后如何获取模型对象并将其传递给模态?

【问题讨论】:

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


    【解决方案1】:

    这是因为你的部分调用不在你的循环中;)

    你可能应该这样做:

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

    【讨论】:

    • 谢谢,我在生成的源代码中更进一步,它现在显示了使用正确内容生成的多个部分。但是,如果模态打开它不会显示正确的内容,它会打开一个模态,显示我在模态中开始操作的索引。您知道如何确保它打开正确的对话框吗?
    • 对于每个模式,您必须创建一个目标:data-target="#delete-label-modal" 并且您的总是相同的。尝试类似:data-target="#delete-label-modal-getId(); ?>"
    猜你喜欢
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    相关资源
    最近更新 更多