webmcp

view framework/env/ui/paginate.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children 985024b16520
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 content = function()
7 ... -- code block which should be encapsulated with page selection links
8 end
9 }
11 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.
12 --]]--
14 function ui.paginate(args)
15 local selector = args.selector
16 local per_page = args.per_page or 10
17 local name = args.name or 'page'
18 local content = args.content
19 local count_selector = selector:get_db_conn():new_selector()
20 count_selector:add_field('count(1)')
21 count_selector:add_from(selector)
22 count_selector:single_object_mode()
23 local count = count_selector:exec().count
24 local page_count = math.floor((count - 1) / per_page) + 1
25 local current_page = param.get(name, atom.integer) or 1
26 selector:limit(per_page)
27 selector:offset((current_page - 1) * per_page)
28 local id = param.get_id_cgi()
29 local params = param.get_all_cgi()
30 local function pagination_elements()
31 if page_count > 1 then
32 for page = 1, page_count do
33 if page > 1 then
34 slot.put(" ")
35 end
36 params[name] = page
37 local attr = {}
38 if current_page == page then
39 attr.class = "active"
40 end
41 ui.link{
42 attr = attr,
43 module = request.get_module(),
44 view = request.get_view(),
45 id = id,
46 params = params,
47 text = tostring(page)
48 }
49 end
50 end
51 end
52 ui.container{
53 attr = { class = 'ui_paginate' },
54 content = function()
55 ui.container{
56 attr = { class = 'ui_paginate_head ui_paginate_select' },
57 content = pagination_elements
58 }
59 ui.container{
60 attr = { class = 'ui_paginate_content' },
61 content = content
62 }
63 ui.container{
64 attr = { class = 'ui_paginate_foot ui_paginate_select' },
65 content = pagination_elements
66 }
67 end
68 }
69 end

Impressum / About Us