【问题标题】:CakePHP 3.0 Confirm Delete Modal in Bake TemplateCakePHP 3.0 确认在烘焙模板中删除模态
【发布时间】:2016-01-28 12:12:36
【问题描述】:

我正在尝试制作一个集成 twitter bootstrap 的烘焙主题。并使用引导模式对话框来确认删除数据。

我正在关注此链接here 并使用CakePHP 3 Bootstrap Helpers

我正在我的plugins/themename/src/Template/Bake/Template/index.ctpview.ctp 中创建主题

这是我在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]) ?>

得到错误:

错误:找不到类“路由器”

查看 /bookmarks 索引页面时

我在index.ctp 的顶部使用了Cake\Routing\Router;

然后我的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]) %>

这似乎有效,但生成的数据操作是 /Tags/delete/1,T 上的大写字母 - 不确定我是否会遇到问题?

除了 index.ctp 中的 Router::url 之外,其他一切都正常工作

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

在模态中改变 postLink 动作的 jQuery 代码

jQuery.noConflict();

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

【问题讨论】:

    标签: php twitter-bootstrap cakephp-3.0


    【解决方案1】:

    这似乎可行,除非这不是真正的“CakePHP 方式”。

    index.ctp

    Router::url() 替换为$this-&gt;request-&gt;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]) ?>
    

    view.ctp

    &lt;%= $details['controller'] %&gt; 包装在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]) %>
    

    有来自更高级 Cake 用户的意见吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多