douqiang7462 2015-10-28 15:58
浏览 34
已采纳

CakePHP 3.0确认在烘焙模板中删除模式

I am trying to make a bake theme that integrates twitter bootstrap. And uses the bootstrap modal dialog to confirm deletion of data.

I am following this link here and also using CakePHP 3 Bootstrap Helpers.

I am creating the theme in my plugins/themename/src/Template/Bake/Template/index.ctp & view.ctp

Here is my code for the delete button in index.ctp

<?= $this->html->link(__($this->Html->faIcon('close').' Delete'),
['action' => '#'],
['escape' => false,
'class' => 'btn btn-sm btn-danger btn-confirm',
'data-toggle'=> 'modal',
'data-target' => '#ConfirmDelete',
'data-action'=> Router::url(['delete', <%= $pk %>]),
false]) ?>

Getting the error:

Error: Class 'Router' not found

when viewing the /bookmarks index page

I have use Cake\Routing\Router; at the top of my index.ctp

Then my view.ctp

<?= $this->Html->link(__($this->Html->faIcon('close')),
['action' => '#'],
['escape' => false,
'class' => 'btn btn-sm btn-danger btn-confirm',
'data-toggle'=> 'modal',
'data-target' => '#ConfirmDelete',
'data-action'=> ['/<%= $details['controller'] %>/delete/'.<%= $otherPk %>],
false]) %>

This seems to be working, but the resulting data-action is /Tags/delete/1 with a capital on the T - not sure if I will run into issues down the road?

Everything else is working, except for the Router::url in the index.ctp

Modal Code in plugins/themename/src/Template/Layout.default.ctp

<?php
$content = 'Are you sure you want to delete this element?';
echo $this->Modal->create(['id' => 'ConfirmDelete']) ;
echo $this->Modal->header('Confirm Delete', ['close' => false]) ;
echo $this->Modal->body($content, ['class' => 'my-body-class']) ;
echo $this->Modal->footer([
    $this->Form->button('Close', ['data-dismiss' => 'modal']),
    $this->Form->postLink(__('Delete'), ['action' => 'delete'],         
['class' => 'btn btn-danger'], false)]) ;
echo $this->Modal->end() ;
?>

jQuery code to change the postLink action in the modal

jQuery.noConflict();

(function($) {
    $(document).ready(function () {
        $(".btn-confirm").on("click", function () {
            var action = $(this).attr('data-action');
            $("#ConfirmDelete form").attr('action', action);
        });
    })
})(jQuery);
  • 写回答

1条回答 默认 最新

  • dongsui8162 2015-10-28 18:01
    关注

    This seems to work, unless this is not the true "CakePHP Way".

    In index.ctp

    Replacing Router::url() with $this->request->here

    <?= $this->html->link(__($this->Html->faIcon('close')),
    ['action' => '#'],
    ['escape' => false,
    'class' => 'btn btn-danger btn-confirm',
    'data-toggle'=> 'modal',
    'data-target' => '#ConfirmDelete',
    'data-action'=> $this->request->here.'/delete/'.<%= $pk %>,
     false]) ?>
    

    In view.ctp

    Wrapping <%= $details['controller'] %> in strtolower()

    <?= $this->Html->link(__($this->Html->faIcon('close')),
    ['action' => '#'],
    ['escape' => false,
    'class' => 'btn btn-danger btn-confirm',
    'data-toggle'=> 'modal',
    'data-target' => '#ConfirmDelete',
    'data-action'=> '/'.strtolower('<%= $details['controller'] %>').'/delete/'.<%= $otherPk %>,
    false]) %>
    

    Any input from more advanced Cake users?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口