【问题标题】:Cakephp pagination ajax messes up the viewCakephp分页ajax弄乱了视图
【发布时间】:2013-09-07 16:15:42
【问题描述】:

所以经过很长时间和很多麻烦后,我终于让我的 ajax 分页工作(有点)

现在我的 html 看起来像这样:

    <?php
$this->Paginator->options(array(
    'update' => '#content',
    'evalScripts' => true,
));
?>

<div class="portlet box green Report index" id="content">
<div class="portlet-title">
    <div class="caption"><i class="icon-globe"></i>Report Summary</div>
    <div class="tools">
        <a href="javascript:;" class="collapse"></a>
    </div>
   </div>
    <div class="portlet-body">
            <table class="table table-striped table-bordered table-hover table-full-width"
                <thead>
                <tr>
                    <th><?php echo $this->Paginator->sort('Offer.name','Offer'); ?></th>
                    <th><?php echo $this->Paginator->sort('Stat.clicks','Clicks'); ?></th>
                    <th><?php echo $this->Paginator->sort('Stat.conversions','Conversion'); ?></th>
                    <th><?php echo $this->Paginator->sort('Stat.payout','Payout'); ?></th>
                    <th><?php echo $this->Paginator->sort('Stat.ltr', 'What-isThis?'); ?></th>
                    <th><?php echo $this->Paginator->sort('Stat.cpc','Expected E-CPC'); ?></th>
                </tr>
                </thead>
                <tbody class="report_data">
                <?php foreach ($table['Report']['data']['data'] as $res): ?>
                    <tr>
                        <td><?php echo h($res['Offer']['name']); ?>&nbsp;</td>
                        <td><?php echo h($res['Stat']['clicks']); ?>&nbsp;</td>
                        <td><?php echo h($res['Stat']['conversions']); ?>&nbsp;</td>
                        <td><?php echo h($res['Stat']['payout']); ?>&nbsp;</td>
                        <td><?php echo h($res['Stat']['ltr']); ?>&nbsp;</td>
                        <td><?php echo h($res['Stat']['cpc']); ?>&nbsp;</td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            <ul>
                <?php if ($this->Paginator->hasPrev()): ?>
                    <li><?php echo $this->Paginator->first(__('First')) ?></li>
                    <li><?php echo $this->Paginator->prev(__('Prev'), array(), null, array('class' => 'prev disabled')) ?></li>
                <?php
                endif;

                echo $this->Paginator->numbers(array('currentTag' => 'span', 'currentClass' => 'active',
                    'separator' => false, 'tag' => 'li', 'modulus' => 5));

                if ($this->Paginator->hasNext()):
                    ?>
                    <li><?php echo $this->Paginator->next(__('Next'), array(), null, array('class' => 'next disabled')) ?></li>
                    <li><?php echo $this->Paginator->last(__('Last')) ?></li>
                <?php endif ?>
            </table>
            </ul>
        </div>
    </div>
</div>
<?php echo $this->Js->writeBuffer(); ?>

遗憾的是我不能给你看图片(它说我缺乏声誉)但似乎当我按下分页时它会将网站的内容放入 div 块中,这意味着视图被搞砸了!

谁能告诉我为什么会这样以及我该如何解决?

我尝试将 id='content' 移动到不同的 div 或 table 块,但没有成功

蛋糕 2.3 版

我的索引操作

    public function index()
{
    if($this->request->is('ajax'))
    {
        $this->layout('ajax');
    }else{

    $this->layout = 'client_layout';
    }
    $startDate = $this->request->data['startDateTime'];
    $endDate = $this->request->data['endDateTime'];
    if($startDate == null || $startDate == '' ){
        $startDate = date('y-m-d');
        $endDate = date('y-m-d');
    }
    array_push($this->paginate['conditions']['Stat.date']['values'], $startDate, $endDate);
    $this->set('table', $this->paginate());
}

更新

好的,所以在我的控制器中我添加了以下内容:

        if ($this->RequestHandler->isAjax()) {
        $this->autoLayout = true;
        $this->autoRender = true;
        $this->layout = 'ajax';
    }else{
    $this->layout = 'client_layout';
    }

现在这有所帮助,但它不是应该的 100%,现在它在表格顶部添加了一个 div 块,这里是一些 HTML 的样子:

    <div id="updateTable">
<div class="portlet box red"> <-- VERY BAD!!
<div class="portlet-title">
<div class="portlet-body">
</div>
<div id="updateTable">

请注意,我还将 id 更改为 updateTable

【问题讨论】:

    标签: php ajax cakephp pagination


    【解决方案1】:

    您的 html 标记无效。表格元素关闭太晚了,移动一下

    </table> 
    

    之前

    <ul>
    

    并删除最后一个 div 标记。

    据我所知,您不必手动检查 ajax 请求。如果组件中包含RequestHandler,则应自动选择相应的布局。

    只需将以下内容添加到您的 AppController:

    public $components = array('RequestHandler'); 
    

    【讨论】:

      【解决方案2】:

      在您的控制器操作中,分页链接将检查请求是否在 ajax 中。如果通过 ajax 发出请求,则告诉控制器不要添加标头和其他 css 代码。

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

      【讨论】:

      • 如果我将它添加到我的控制器中,我会收到一个内部错误(在 javascript 调试器中)我将我的索引添加到我的问题中
      • @Marc 你在视图布局文件夹中有 ajax.ctp 文件吗?
      • 是的,我做的是包含 fetch('content');?>
      • @Marc 你在重复DOM ID of updateTable...不要重复相同的ID
      • 有趣的是我没有在我的代码中重复它,它必须由 javascript 生成(或者我知道什么 :P :S)
      猜你喜欢
      • 2015-04-12
      • 1970-01-01
      • 2016-02-23
      • 2018-04-19
      • 2022-06-28
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多