我的最终加载目标是,当页面接近页面底部时,从数据库中加载更多项。我创建了从数据库加载6个图表开始的页面。我写了这个jQuery JavaScript
<script>
(function(){
$(window).scroll(function () {
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 0) {
loadmore();
}
});
divID = $('.chartArea>div:last-child').find('div').attr('id');
divID = divID.substr(6);
divID = divID-6;
function loadmore() {
$.ajax({`
`url: "<?php echo $this->Url->build(['action'=>'chartsTest']);?>",
type: "POST",
dataType: 'text',
data: {'id': divID },
success: function(data){
console.log(data);
$('.chartArea').html(data);
}
});
};
})();
</script>
除了$('.chartArea').html(data);
以外,其他所有方法都有效,<!DOCTYPE html>
返回<div class="lay2col chartArea">
<?php foreach($items as $chart) {
$card = new \App\Model\Entity\Card();
$card->content_type = (object)[ 'name' => 'chart' ];
$card->content_id = $chart->slug;
$card->chart = $chart;
?>
<div>
<div id="chart-<?=$chart->id?>" class="card-chart card-half card content" title="<?=$card->override('title') ?: $card->chart->title?>">
<?=$this->element('cards/chart', ['card' => $card])?>
</div>
</div>
<?php } ?>
的整个HTML文档并将其插入div chartArea
页面的此部分受第一个数据库调用和JavaScript的影响
HTML
public function chartsTest() {
$this->loadModel('Charts');
if($this->request->is('ajax')) {
$chartID = $this->request->data['id'];
$items = $this->Charts->find('all')->where(['Charts.active'=>true, 'Charts.id >=' => $chartID]);
$items->order(['Charts.id' => 'desc']);
$this->autoFilter($items);
$moreCharts='';
foreach($items as $chart) {
$card = new \App\Model\Entity\Card();
$card->content_type = (object)[ 'name' => 'chart' ];
$card->content_id = $chart->slug;
$card->chart = $chart;
$moreCharts. = '<div>
<div id="chart-'.$chart->id.'" class="card-chart card-half card content" title="'.$card->chart->title.'">
<a href="/the-palce/charts-and-graphs/'.$card->chart->slug.'">
<img class="twox" src="'.$card->image_path.'" alt="'.$card->altTag.'">
</div>
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">'.$card->chart->title.'</p>
</div>
</a>
</div>
</div>';
};
$this->set([
'success' => true,
'data' => $moreCharts
]);
} else {
$items = $this->Charts->find('all')->where(['Charts.active'=>true])->limit(6);
$items->order(['Charts.id' => 'desc']);
$this->set([
'page_title' => 'Charts and Graphs',
'page' => 'charts and graphs',
'items' => $items
]);
}
}
这就是我的控制器中的内容
$('.chartArea').html(data);
因此,我必须丢失一些内容,因为当我仅希望变量<!DOCTYPE html>
中的数据时,我的JavaScript函数中的$moreCharts
返回了整个$dataForView = [
'success' => true,
'data' => '
<div>
<div id="chart-73" class="card-chart card-half card content" title="title">
<a href="/the-place/slug">
<img class="twox" src="" alt="">
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing.</p>
</div>
</a>
</div>
<div>
<div id="chart-72" class="card-chart card-half card content" title="title">
<a href="/the-place/slug">
<img class="twox" src="" alt="">
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing.</p>
</div>
</a>
</div>
<div>
<div id="chart-71" class="card-chart card-half card content" title="title">
<a href="/the-place/slug">
<img class="twox" src="" alt="">
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing.</p>
</div>
</a>
</div>
<div>
<div id="chart-70" class="card-chart card-half card content" title="title">
<a href="/the-place/slug">
<img class="twox" src="" alt="">
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing.</p>
</div>
</a>
</div>
<div>
<div id="chart-69" class="card-chart card-half card content" title="title">
<a href="/the-place/slug">
<img class="twox" src="" alt="">
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing.</p>
</div>
</a>
</div>
<div>
<div id="chart-68" class="card-chart card-half card content" title="title">
<a href="/the-place/slug">
<img class="twox" src="" alt="">
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing.</p>
</div>
</a>
</div>
<div>
<div id="chart-67" class="card-chart card-half card content" title="title">
<a href="/the-place/slug">
<img class="twox" src="" alt="">
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing.</p>
</div>
</a>
</div>
<div>
<div id="chart-66" class="card-chart card-half card content" title="title">
<a href="/the-place/slug">
<img class="twox" src="" alt="">
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing.</p>
</div>
</a>
</div>
<div>
<div id="chart-65" class="card-chart card-half card content" title="title">
<a href="/the-place/slug">
<img class="twox" src="" alt="">
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing.</p>
</div>
</a>
</div>
<div>
<div id="chart-664quot; class="card-chart card-half card content" title="title">
<a href="/the-place/slug">
<img class="twox" src="" alt="">
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing.</p>
</div>
</a>
</div>
<div>
<div id="chart-63" class="card-chart card-half card content" title="title">
<a href="/the-place/slug">
<img class="twox" src="" alt="">
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing.</p>
</div>
</a>
</div>
<div>
<div id="chart-62" class="card-chart card-half card content" title="title">
<a href="/the-place/slug">
<img class="twox" src="" alt="">
<div class="graphic-pill">
<span class="pill">Chart</span>
</div>
<div class="btm-info">
<p class="summary">Lorem ipsum dolor sit amet, consectetur adipiscing.</p>
</div>
</a>
</div>
。奇怪的是,如果检查我可以看到的控制台
__m512i
所以我可以从控制台中看到它正在抓取下6个图表,但我需要知道要隔离它们,然后在div chartArea中添加/替换那些图表。