webmcp

view framework/env/ui/paginate.lua @ 568:c43f251262d8

More layout options for ui.paginate{...} and removed doc of non-implemented option
author jbe
date Wed Oct 13 16:48:34 2021 +0200 (2021-10-13)
parents 8cf6d927d074
children 5b19007574de
line source
1 --[[--
2 ui.paginate{
3 selector = selector, -- a selector for items from the database (will be modified)
4 anchor = anchor, -- optional name of anchor in document to jump to
5 per_page = per_page, -- items per page, defaults to 10
6 container_attr = container_attr, -- html attr for the container element
7 link_attr = link_attr, -- html attr for each page link
8 name = name, -- name of the CGI get variable, defaults to "page"
9 position = position, -- position of page links relative to content: "before", "after", or "both" (default)
10 content = function()
11 ... -- code block which should be encapsulated with page selection links
12 end
13 }
15 This function preceeds and appends the output of the given 'content' function with page selection links. The passed selector will be modified to show only a limited amount ('per_page') of items. The currently displayed page will be determined directly by request.get_param{...}, and not via the param.get(...) function, in order to pass page selections automatically to sub-views.
17 --]]--
19 function ui.paginate(args)
20 local selector = args.selector
21 local anchor = args.anchor
22 local per_page = args.per_page or 10
23 local container_attr = args.container_attr or { class = 'ui_paginate' }
24 local link_attr = args.link_attr or {}
25 local name = args.name or 'page'
26 local position = args.position or 'both'
27 local content = args.content
28 local count_selector = selector:get_db_conn():new_selector()
29 count_selector:add_field('count(1)')
30 count_selector:add_from(selector)
31 count_selector:single_object_mode()
32 local count = count_selector:exec().count
33 local page_count = 1
34 if count > 0 then
35 page_count = math.floor((count - 1) / per_page) + 1
36 end
37 local current_page = atom.integer:load(request.get_param{name=name}) or 1
38 if current_page > page_count then
39 current_page = page_count
40 end
41 selector:limit(per_page)
42 selector:offset((current_page - 1) * per_page)
43 local id = request.get_id_string()
44 local params = request.get_param_strings()
45 local function pagination_elements()
46 if page_count > 1 then
47 for page = 1, page_count do
48 if page > 1 then
49 slot.put(" ")
50 end
51 params[name] = page
52 local attr = table.new(link_attr)
53 if current_page == page then
54 if attr.class then
55 attr.class = attr.class .. " active"
56 else
57 attr.class = "active"
58 end
59 end
60 ui.link{
61 attr = attr,
62 module = request.get_module(),
63 view = request.get_view(),
64 id = id,
65 params = params,
66 anchor = anchor,
67 text = tostring(page)
68 }
69 end
70 end
71 end
72 ui.container{
73 attr = container_attr,
74 content = function()
75 if position == 'before' or position == 'both' then
76 ui.container{
77 attr = { class = 'ui_paginate_head ui_paginate_select' },
78 content = pagination_elements
79 }
80 end
81 ui.container{
82 attr = { class = 'ui_paginate_content' },
83 content = content
84 }
85 if position == 'after' or position == 'both' then
86 ui.container{
87 attr = { class = 'ui_paginate_foot ui_paginate_select' },
88 content = pagination_elements
89 }
90 end
91 end
92 }
93 end

Impressum / About Us