webmcp

annotate framework/env/ui/paginate.lua @ 91:2f8d8edd1836

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

Impressum / About Us