webmcp

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

Impressum / About Us