0

如何在不使用默认 ajax 助手的情况下在 cakephp 中设置 ajax?默认的 ajax 助手将 js 代码放在页面本身上,我不希望这样。我希望将其设置在单独的 js 文件(即general.js)中。我怎么做?我已经设置了分页。

4

1 回答 1

1

首先确保你已经设置了 jquery。在您的default.ctp( View/Layouts/default.ctp) 部分中添加以下行<head>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>

在您的AppController.php文件 ( Controller/AppController.php) 中添加以下行

function beforeRender() {

        if ($this->request->is('ajax')) {
            $this->layout = false;
        }
}

这会导致在对控制器操作进行 ajax 调用时仅加载视图本身而不是整个布局的行为。

在您的general.js文件 ( webroot/js/general.js) 中添加以下代码:

$(document).ready(function(){
        ajaxPagingNavigation();
});

function ajaxPagingNavigation() {
    $(".paging a").click(function(e) {
            $.ajax({
              url: $(this).attr('href'),
              cache: false
            }).done(function( html ) {
              $("#content").html(html);
              ajaxPagingNavigation();
            });

        e.preventDefault();
    });
}
于 2013-09-17T00:02:04.500 回答