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