引导程序 3 确认删除 cakephp 中的模式

2023-01-18php开发问题
5

本文介绍了引导程序 3 确认删除 cakephp 中的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个记录表,其中每一行都有一个删除链接.您会找到用于删除操作的 cakephp :

公共函数删除($id){如果 ($this->request->is('get')) {抛出新的 MethodNotAllowedException();}如果 ($this->Category->delete($id)) {$this->Session->setFlash('Votre élément a été supprimé.','default',array(),'success');返回 $this->redirect(array('action' =>'index'));}}

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

<table class="table table-striped table-condensed table-bordered"><tr><th>title</th><th>动作</th></tr><?php foreach ($categorys as $category): ?><tr><td><?php echo $category['Category']['title'];?></td><td><?phpecho $this->Html->link('View',数组('控制器' => '类别', '动作' => '视图', $category['Category']['id']),数组('class' => 'btn btn-info btn-sm active'));?><?phpecho $this->Html->link('编辑', array('action' => 'edit', $category['Category']['id']),数组('class' => 'btn btn-primary btn-sm active'));?><?phpecho $this->Form->postLink('删除',array('action' => 'delete', $category['Category']['id']),array('confirm' => '你真的要删除这个元素吗?','class' => 'btn btn-danger btn-sm active'));?></td></tr><?php endforeach;?><?php unset($category);?>

所以对于我想要的后链接,当我点击链接时,它会向我显示一个引导程序确认模式,如下所示:

 <div class="modalfade" 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">类别删除</h4>

<div class="modal-body">你真的要删除这个元素吗?

<div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">关闭</button><a class="btn btn-danger 危险">确认</a>

谁能帮我使用cake php的jshelper来创建一个引导模式对话框而不是默认的对话框.

谢谢.

解决方案

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

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

Html->link($this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-trash')),'#',大批('class'='btn btn-danger btn-confirm','数据切换'=>'模态','数据目标' =>'#确认删除','数据动作'=>路由器::网址(数组('action'=>'delete',$category['Category']['id'])),'逃脱' =>错误的),错误的);?>

在你的模态中添加 postLink 没有确认消息,而不是把消息放在 false:

Form->postLink('确认',数组('动作' => '删除'),array('class' => 'btn btn-danger btn-sm active'),错误的,));?>

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

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.

解决方案

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

这篇关于引导程序 3 确认删除 cakephp 中的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用

PHP实现DeepL翻译API调用

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法

PHP通过phpspreadsheet导入Excel日期数据处理方法

PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件

mediatemple - 无法使用 codeigniter 发送电子邮件

mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误

Laravel Gmail 配置错误

Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题

将 PHPMailer 用于 SMTP 的问题

Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题

Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17

热门文章

1nohup:忽略输入并将输出附加到“nohup.out" 2在控制台中出错:无法加载资源:net::ERR_CONNECTION_RESET 3如何将 LDAP 时间戳转换为 Unix 时间戳 4不推荐使用常量 FILTER_SANITIZE_STRING 5APACHE 崩溃:父进程:子进程以状态 3221225477 退出 -- 正在重新启动 6PHP通过phpspreadsheet导入Excel日期数据处理方法 7Analytics API 返回:错误请求 - invalid_grant 8“tlsv1 警报内部错误"握手时

热门精品源码

最新VIP资源

1多功能实用站长工具箱html功能模板 2多风格简历在线生成程序网页模板 3论文相似度查询系统源码 4响应式旅游景点宣传推广页面模板 5在线起名宣传推广网站源码 6酷黑微信小程序网站开发宣传页模板 7房产销售交易中介网站模板 8小学作业自动生成程序