gpt4 book ai didi

javascript - 使用ajax请求获取上一个和下一个对象

转载 作者:太空狗 更新时间:2023-10-29 15:05:21 26 4
gpt4 key购买 nike

我正在尝试创建一个 Ajax 函数,该函数将根据文章的创建日期创建文章导航,以便用户可以使用上一个(较旧的)和下一个(较新的)链接浏览文章。

<div class="content clear">

<div class="article">
Article contents...
</div>

<a href="#Prev" id="prev" class="leftarrow" title="Previous">EELMINE<span class="arrow_left"></span></a>
<a href="#" id="bactolist" onClick="history.go(-1); return false;">Tagasi nimekirja juurde<span class="arrow_middle"></span></a>
<a href="#Next" id="next" class="rightarrow" title="Next">JÄRGMINE<span class="arrow_right"></span></a>
</div> <!-- End content -->

<script type="text/javascript">

$.ajax({
url: '/admin/api/site/articles.json?per_page=100',
dataType: 'json',
success: function(articles) {
$(articles).each(function(index, article) {

console.log(article);

$('div.article').fadeOut(0);
$('div.article:first').fadeIn(500);
$('a.leftarrow, a.rightarrow').click( function (ev) {

//prevent browser jumping to top
ev.preventDefault();

//get current visible item
var $visibleItem = $('div.article:visible');

//get total item count
var total = $('div.article').length;

//get index of current visible item
var index = $visibleItem.prevAll().length;

//if we click next increment current index, else decrease index
$(this).attr('href') === '#Next' ? index++ : index--;

//if we are now past the beginning or end show the last or first item
if (index === -1){
index = total-1;
}
if (index === total){
index = 0
}

//hide current show item
$visibleItem.hide();

//fade in the relevant item
$('div.article:eq(' + index + ')').fadeIn(500);

});
});
}
});

我在构建负责根据日期值获取文章的函数时遇到困难。

使用 console.log(article),我得到以下值:

body: "..."
comments_count: 0
comments_url: "..."
created_at: "date and time ..."
excerpt: "..."
title: "..."
url: "..."

因此应该可以使用 created_at 变量进行导航,但我现在不知道如何操作。有什么想法吗?

使用的内容管理系统:Edicy注意: CMS 不支持 PHP。

编辑:文章列表sample在 CMS 开发人员文档提供的“博客”页面上。

最佳答案

更简单的方法是尽可能多地使用您的模型,并使用 sql "limit"我不知道你用的是什么数据库,所以我会用mysql数据库做一些步骤。我对您的代码感到困惑,为什么您不按任何按钮就调用 ajax?或者你隐藏了一些代码?

让我试试这段代码:假设您有一个容器 div 和 2 个静态导航按钮

<a href="#" id="next" data-page="2">Next</a>
<a href="#" id="back" data-page="0">Back</a>

<div id="container"></div>

<script>
$("#next, #back").click(function(){
//get page number
//0 means no page to load
var page = $(this).data("page");
if(page == 0)return false;
$.ajax({
url : "your/url/?page=" + page,
dataType: "json",
success : function(data){
var container = $("#container");
$.each(data, function(index, article){
container.append(article); // or do something you want
});
if($this.attr("id") == "next") $("#next, #prev").data("page", $this.data("page") + 1);
else $("#next, #prev").data("page", $this.data("page") - 1);
}
});
});
</script>

对于后端:

    <?php
$page = $_GET["page"];
$perPage = 100;
$start = ($page - 1) * $perPage; // minus 1, because limit start from 0

$sql = "SELECT * FROM table WHERE 1=1 LIMIT $start, $perPage";

//execute the sql and get the result (ex. result)

echo json_encode($result);

希望我的回答对您有所帮助。

关于javascript - 使用ajax请求获取上一个和下一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19358064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com