webmcp

view framework/env/ui/paginate.lua @ 48:17435a7d252b

Removed excess indentation in env/ui/paginate.lua
author jbe
date Sat Oct 16 19:03:18 2010 +0200 (2010-10-16)
parents 81dfcfd960ed
children 3a6962b9121c
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 container_attr = container_attr -- html attr for the container element
6 name = name, -- name of the CGI get variable, defaults to "page"
7 page = page, -- directly specify a page, and ignore 'name' parameter
8 content = function()
9 ... -- code block which should be encapsulated with page selection links
10 end
11 }
13 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.
15 --]]--
17 function ui.paginate(args)
18 local selector = args.selector
19 local per_page = args.per_page or 10
20 local name = args.name or 'page'
21 local content = args.content
22 local count_selector = selector:get_db_conn():new_selector()
23 count_selector:add_field('count(1)')
24 count_selector:add_from(selector)
25 count_selector:single_object_mode()
26 local count = count_selector:exec().count
27 local page_count = 1
28 if count > 0 then
29 page_count = math.floor((count - 1) / per_page) + 1
30 end
31 local current_page = atom.integer:load(cgi.params[name]) or 1
32 if current_page > page_count then
33 current_page = page_count
34 end
35 selector:limit(per_page)
36 selector:offset((current_page - 1) * per_page)
37 local id = param.get_id_cgi()
38 local params = param.get_all_cgi()
39 local function pagination_elements()
40 if page_count > 1 then
41 for page = 1, page_count do
42 if page > 1 then
43 slot.put(" ")
44 end
45 params[name] = page
46 local attr = {}
47 if current_page == page then
48 attr.class = "active"
49 end
50 local partial
51 if ui.is_partial_loading_enabled() then
52 partial = {
53 params = {
54 [name] = tostring(page)
55 }
56 }
57 end
58 ui.link{
59 attr = attr,
60 module = request.get_module(),
61 view = request.get_view(),
62 id = id,
63 params = params,
64 text = tostring(page),
65 partial = partial
66 }
67 end
68 end
69 end
70 ui.container{
71 attr = args.container_attr or { class = 'ui_paginate' },
72 content = function()
73 ui.container{
74 attr = { class = 'ui_paginate_head ui_paginate_select' },
75 content = pagination_elements
76 }
77 ui.container{
78 attr = { class = 'ui_paginate_content' },
79 content = content
80 }
81 ui.container{
82 attr = { class = 'ui_paginate_foot ui_paginate_select' },
83 content = pagination_elements
84 }
85 end
86 }
87 end

Impressum / About Us