Hello! The api.post_info method has 4 returns (as far as I know), namely¹:
ok (boolean, 1 if success or nothing if error),
result (list of records returned by the API query, or nothing if error),
resultInfo (contains data about the pagination of the returned records!, or error code in case of an error), and
errorInfo (is only returned if an error occurs during the API query).
¹: the returns don't actually have any names, I named them for illustration purposes.
So since we don't know how to use the html.paging() method yet, let's settle for what is returned in resultInfo to create our own pagination.
The code is a little complex so you can ask if you don't understand any part.
Code: Select all
local post_list_html = [=[
<h1>%id% - %title%</h1>
<p>%content%</p>
]=]
local post_list_paging = [=[
<nav arial-label="pagination">
<ul class="pagination">
FIRST PREV CURRENT NEXT LAST
</ul>
</nav>
]=]
local page_number = tonumber(req.get.page_post) or 1
local post_params = {
limit = 1,
order = "DESC",
page = page_number
}
local post_ok, -- Checks whether the API query was successful
post_list, -- List the result of the API query
post_info, -- Contains information about the API query, useful for pagination! [Note 1]
post_error -- Message returned in case of error
= api.post_info(post_params)
if post_ok then
local total_records = tonumber(post_info.total)
local last_page = tonumber(post_info.pagenum)
local previous_page = page_number - 1
local next_page = page_number + 1
if (total_records > 1) then
if (page_number == 1) then -- First page: First and Prev links are disabled
post_list_paging = post_list_paging:gsub("(%w+)%s*", {
["FIRST"] = '<li class="page-item disabled">«</li>\n',
["PREV"] = '<li class="page-item disabled">‹</li>\n',
["CURRENT"] = '<li class="page-item active">1</li>\n',
["NEXT"] = '<li class="page-item"><a href="?page_post=' .. next_page .. '">Next ›</a></li>\n',
["LAST"] = '<li class="page-item"><a href="?page_post=' .. last_page .. '">Last »</a></li>\n'
})
elseif (page_number >= last_page) then -- Last page: Last and Next links are disabled
post_list_paging = post_list_paging:gsub("(%w+)%s*", {
["FIRST"] = '<li class="page-item"><a href="?page_post=1">« First</a></li>\n',
["PREV"] = '<li class="page-item"><a href="?page_post=' .. previous_page .. '">‹ Prev</a></li>\n',
["CURRENT"] = '<li class="page-item active">' .. last_page .. '</li>\n',
["NEXT"] = '<li class="page-item disabled">›</li>\n',
["LAST"] = '<li class="page-item disabled">»</li>\n'
})
else -- Current page is anywhere between the first and last, all links are available
post_list_paging = post_list_paging:gsub("(%w+)%s*", {
["FIRST"] = '<li class="page-item"><a href="?page_post=1">« First</a></li>\n',
["PREV"] = '<li class="page-item"><a href="?page_post=' .. previous_page .. '">‹ Prev</a></li>\n',
["CURRENT"] = '<li class="page-item active">' .. page_number .. '</li>\n',
["NEXT"] = '<li class="page-item"><a href="?page_post=' .. next_page .. '">Next ›</a></li>\n',
["LAST"] = '<li class="page-item"><a href="?page_post=' .. last_page .. '">Last »</a></li>\n'
})
end
else -- Only one record returned in the API query, pagination is completely disabled
post_list_paging = post_list_paging:gsub("(%w+)%s*", {
["FIRST"] = '<li class="page-item disabled">«</li>\n',
["PREV"] = '<li class="page-item disabled">‹</li>\n',
["CURRENT"] = '<li class="page-item disabled">1</li>\n',
["NEXT"] = '<li class="page-item disabled">›</li>\n',
["LAST"] = '<li class="page-item disabled">»</li>\n'
})
end
print(html.render_tag(post_list_html, post_list, true));
print(post_list_paging)
else
print("Unable to query posts. Error received: ", post_error)
end
https://riptutorial.com/lua/example/205 ... b-function
Note 1 — The resultInfo/post_info return would be something like this (see it doesn't have a name):
Code: Select all
{
"total": 8,
"count": 1,
"pagenum": 8
}
Where
total is the total number of records returned by the API query,
count is the number of records currently being displayed, and
pagenum is the number of the last page of records (total / count).
— In this example, my site only has 8 posts and I have set the limit to 1 per page.
Try
print(api.post_info(post_params)) to see everything that is returned, I always do this when I want to test something that isn't yet fully documented.