webmcp

view framework/env/ui/paginate.lua @ 2:72860d232f32

Version 1.0.2

Fixed bug with explicit garbage collection (requests > 256kB caused an error)

Views prefixed with an underscore can't be called externally

ui.paginate now displays the last page, if the selected page number is too high.
author jbe/bsw
date Thu Dec 10 12:00:00 2009 +0100 (2009-12-10)
parents 985024b16520
children f02e14d1e69e
line source
1 --[[--
2 ui.paginate{
3 selector = selector, -- a selector for items from the database
4 per_page = per_page, -- items per page, defaults to 10
5 name = name, -- name of the CGI get variable, defaults to "page"
6 page = page, -- directly specify a page, and ignore 'name' parameter
7 content = function()
8 ... -- code block which should be encapsulated with page selection links
9 end
10 }
12 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 cgi.params, and not via the param.get(...) function, in order to pass page selections automatically to sub-views.
14 --]]--
16 function ui.paginate(args)
17 local selector = args.selector
18 local per_page = args.per_page or 10
19 local name = args.name or 'page'
20 local content = args.content
21 local count_selector = selector:get_db_conn():new_selector()
22 count_selector:add_field('count(1)')
23 count_selector:add_from(selector)
24 count_selector:single_object_mode()
25 local count = count_selector:exec().count
26 local page_count = math.floor((count - 1) / per_page) + 1
27 local current_page = atom.integer:load(cgi.params[name]) or 1
28 if current_page > page_count then
29 current_page = page_count
30 end
31 selector:limit(per_page)
32 selector:offset((current_page - 1) * per_page)
33 local id = param.get_id_cgi()
34 local params = param.get_all_cgi()
35 local function pagination_elements()
36 if page_count > 1 then
37 for page = 1, page_count do
38 if page > 1 then
39 slot.put(" ")
40 end
41 params[name] = page
42 local attr = {}
43 if current_page == page then
44 attr.class = "active"
45 end
46 ui.link{
47 attr = attr,
48 module = request.get_module(),
49 view = request.get_view(),
50 id = id,
51 params = params,
52 text = tostring(page)
53 }
54 end
55 end
56 end
57 ui.container{
58 attr = { class = 'ui_paginate' },
59 content = function()
60 ui.container{
61 attr = { class = 'ui_paginate_head ui_paginate_select' },
62 content = pagination_elements
63 }
64 ui.container{
65 attr = { class = 'ui_paginate_content' },
66 content = content
67 }
68 ui.container{
69 attr = { class = 'ui_paginate_foot ui_paginate_select' },
70 content = pagination_elements
71 }
72 end
73 }
74 end

Impressum / About Us