dongqiao8421 2014-06-01 14:08
浏览 58
已采纳

bootstrap 3确认cakephp中的删除模式

hi i have a table of records where there's a delete link for every row.Her you will find cakephp for the delete action :

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'));
        }

    }

so when i click on the delete button a raw javascript confirm dialog box is diplayed to confirm the action of the deletion in the view. here's an index.ctp containing the delete link :

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

so for the postlink i want when i click on the link it will show me a bootstrap confirmation modal like this :

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

can someone help me to use the jshelper of the cake php to create a bootstrap modal dialog instead of the default one.

Thank you.

  • 写回答

2条回答 默认 最新

  • dtt27783 2014-06-01 14:43
    关注

    I edit my answer and improve code

    On your index page instead postLink, create a button or link that will call the modal, ie

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

    In your modal add postLink without confirmation message, instead of the message put false:

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

    add this js code after bootstrap.js

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

    or as suggested by user1655410 add this js code

    $('#ConfirmDelete').on('show.bs.modal', function(e) {
        $(this).find('form').attr('action', $(e.relatedTarget).data('action'));
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?