javascript - bootstrap 3确认删除cakephp中的模式

标签 javascript php jquery twitter-bootstrap cakephp

嗨,我有一个记录表,其中每一行都有一个删除链接。您将在其中找到 cakephp 的删除操作:

public function delete($id){

        if ($this->request->is('get')) {
            throw new MethodNotAllowedException();
        }

        if ($this->Category->delete($id)) {
            $this->Session->setFlash( 'Votre élément a été supprimé.','default',array(),'success');
            return $this->redirect(array('action' => 'index'));
        }

    }

因此,当我单击删除按钮时,会显示一个原始 JavaScript 确认对话框,以确认 View 中的删除操作。这是包含删除链接的index.ctp:

<!--table content-->
  <table class="table table-striped table-condensed table-bordered">
    <tr>
        <th>title</th>
        <th>Actions</th>
    </tr>

    <?php foreach ($categorys as $category): ?>
    <tr>
        <td><?php echo $category['Category']['title']; ?></td>
        <td>
            <?php
            echo $this->Html->link('View',
                                   array('controller' => 'categories', 'action' => 'view', $category['Category']['id']),
                                   array('class' => 'btn btn-info btn-sm active')
                                   ); ?>
            <?php
            echo $this->Html->link(
                'Edit', array('action' => 'edit', $category['Category']['id']),
                          array('class' => 'btn btn-primary btn-sm active')
            );
            ?>
            <?php
            echo $this->Form->postLink(
                'Delete',
                array('action' => 'delete', $category['Category']['id']),
                array('confirm' => 'Do you want really to delete thi element?','class' => 'btn btn-danger btn-sm active')
            );
            ?>
        </td>
    </tr>
    <?php endforeach; ?>
    <?php unset($category); ?>
  </table>

因此,对于我想要的后链接,当我单击链接时,它将向我显示一个 Bootstrap 确认模式,如下所示:

 <!-- Modal -->
    <div class="modal fade" id="ConfirmDelete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Category deletion</h4>
                </div>
                <div class="modal-body">
                    Do you  really want  to delete thi element?
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <a  class="btn btn-danger danger">Confirm</a>
                </div>
            </div>
        </div>
    </div>

有人可以帮我使用cake php的jshelper来创建一个 Bootstrap 模式对话框而不是默认的吗?

谢谢。

最佳答案

我编辑我的答案并改进代码

在索引页上创建一个调用模式的按钮或链接,而不是 postLink,即

<?php 
echo $this->Html->link(
    $this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-trash')), 
    '#', 
    array(
       'class'=>'btn btn-danger btn-confirm',
       'data-toggle'=> 'modal',
       'data-target' => '#ConfirmDelete',
       'data-action'=> Router::url(
          array('action'=>'delete',$category['Category']['id'])
       ),
       'escape' => false), 
false);
?>

在您的模式中添加没有确认消息的 postLink,而不是将消息设置为 false:

<?php
    echo $this->Form->postLink(
         'Confirm',
            array('action' => 'delete'),
            array('class' => 'btn btn-danger btn-sm active'),
            false,
         )
        );
        ?>

在 bootstrap.js 之后添加此 js 代码

$(document).ready(function() {
  $(".btn-confirm").on("click", function () {
     var action = $(this).attr('data-action');
     $("form").attr('action',action);
});
});

或按照user1655410的建议添加此js代码

$('#ConfirmDelete').on('show.bs.modal', function(e) {
    $(this).find('form').attr('action', $(e.relatedTarget).data('action'));
});

关于javascript - bootstrap 3确认删除cakephp中的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23980702/

相关文章:

javascript - 如何显示进度图标直到所选图像完全加载?

jquery - 如何使用 jquery 检查下拉列表中可用的选项

javascript - 从特定样式表而不是代码的每个部分使 css 内联

javascript - Rails 2.3.11 中未出现 CSV 下载框

如果不知道索引,PHP如何访问多维数组的所有元素?

php - 为什么 PHP CLI 被视为 SAPI 的一种?

javascript - 如何从页面中获取所有 div 属性 id?

jquery - 居中定位 div 不是很居中

javascript - 元素的 href 属性已被 javascript 删除,但它仍然存在于页面源代码中

php - 在 Laravel Eloquent 中使用 “with()” 函数连接列