How to create Paging using Lua?

Ask anything you want to know
Post Reply
leedalgoo
Posts: 11
Joined: Sun Mar 19, 2023 7:18 am

How to create Paging using Lua?

Post by leedalgoo »

I take a guess that html.paging() is used to create paging right?
But how about the parameters and tags for paging function? I try to treat it like html.render_tag() but it's not working.
leedalgoo
Posts: 11
Joined: Sun Mar 19, 2023 7:18 am

Re: How to create Paging using Lua?

Post by leedalgoo »

Somehow, I found a temporary solution to make paging with lua. The code is super messy, if you know another way, this thread is open for discussion.

Code: Select all

if (req.get.page == nil) then
pagenumber = "1" else pagenumber = req.get.page
end

local param_paging = {
page = pagenumber + 1,
limit = 10,
order = "ASC"
}

local is_ok, paging = api.post_info(param_paging)

if (paging[0] == nil) then next_page = "style=\"pointer-events:none;\"" else next_page = "" end
if (pagenumber == "1") then prev_page = "style=\"pointer-events:none;\"" else prev_page="" end

local paging_html = [=[
	<nav aria-label="Page navigation example">
		<ul class="pagination justify-content-center">
			<li class="page-item"><a class="page-link" href="?page=%s" %s>&#9664; Previous page</a></li>
			<li class="page-item"><a class="page-link" href="?page=%s" %s>Next &#9658;</a></li>
		</ul>
	</nav>]=]

print(string.format(paging_html, pagenumber - 1, prev_page, pagenumber + 1, next_page));
User avatar
francisco
Posts: 62
Joined: Tue Mar 07, 2023 1:48 pm
Location: Brazil
Contact:

Re: How to create Paging using Lua?

Post by francisco »

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.
😴
Administrator
Site Admin
Posts: 34
Joined: Tue Mar 07, 2023 7:56 am

Re: How to create Paging using Lua?

Post by Administrator »

try something like this

Code: Select all

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)
post_info.url = "?page="  ---> like old way pagination link
post_info.page = req.get.page or 1;   ---> current page

global.html.paging(post_info);  ---> output same as tag code
User avatar
francisco
Posts: 62
Joined: Tue Mar 07, 2023 1:48 pm
Location: Brazil
Contact:

Re: How to create Paging using Lua?

Post by francisco »

Administrator wrote: Mon Feb 26, 2024 4:35 pm try something like this

Code: Select all

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)
post_info.url = "?page="  ---> like old way pagination link
post_info.page = req.get.page or 1;   ---> current page

global.html.paging(post_info);  ---> output same as tag code
Thank you very much!


@leedalgoo mistery solved 😂 try this:

Code: Select all

print(global.html.paging("%first% %prev% %num% %next% %last%", post_info))
😴
Post Reply