webmcp

annotate framework/env/ui/paginate.lua @ 568:c43f251262d8

More layout options for ui.paginate{...} and removed doc of non-implemented option
author jbe
date Wed Oct 13 16:48:34 2021 +0200 (2021-10-13)
parents 8cf6d927d074
children 5b19007574de
rev   line source
jbe/bsw@0 1 --[[--
jbe/bsw@0 2 ui.paginate{
jbe@349 3 selector = selector, -- a selector for items from the database (will be modified)
jbe@349 4 anchor = anchor, -- optional name of anchor in document to jump to
jbe@349 5 per_page = per_page, -- items per page, defaults to 10
jbe@349 6 container_attr = container_attr, -- html attr for the container element
jbe@568 7 link_attr = link_attr, -- html attr for each page link
jbe@349 8 name = name, -- name of the CGI get variable, defaults to "page"
jbe@568 9 position = position, -- position of page links relative to content: "before", "after", or "both" (default)
jbe@48 10 content = function()
jbe@349 11 ... -- code block which should be encapsulated with page selection links
jbe/bsw@0 12 end
jbe/bsw@0 13 }
jbe/bsw@0 14
jbe@223 15 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 request.get_param{...}, and not via the param.get(...) function, in order to pass page selections automatically to sub-views.
jbe@1 16
jbe/bsw@0 17 --]]--
jbe/bsw@0 18
jbe/bsw@0 19 function ui.paginate(args)
jbe@568 20 local selector = args.selector
jbe@568 21 local anchor = args.anchor
jbe@568 22 local per_page = args.per_page or 10
jbe@568 23 local container_attr = args.container_attr or { class = 'ui_paginate' }
jbe@568 24 local link_attr = args.link_attr or {}
jbe@568 25 local name = args.name or 'page'
jbe@568 26 local position = args.position or 'both'
jbe@568 27 local content = args.content
jbe/bsw@0 28 local count_selector = selector:get_db_conn():new_selector()
jbe/bsw@0 29 count_selector:add_field('count(1)')
jbe/bsw@0 30 count_selector:add_from(selector)
jbe/bsw@0 31 count_selector:single_object_mode()
jbe/bsw@0 32 local count = count_selector:exec().count
jbe@8 33 local page_count = 1
jbe@8 34 if count > 0 then
jbe@8 35 page_count = math.floor((count - 1) / per_page) + 1
jbe@8 36 end
jbe@223 37 local current_page = atom.integer:load(request.get_param{name=name}) or 1
jbe/bsw@2 38 if current_page > page_count then
jbe/bsw@2 39 current_page = page_count
jbe/bsw@2 40 end
jbe/bsw@0 41 selector:limit(per_page)
jbe/bsw@0 42 selector:offset((current_page - 1) * per_page)
jbe@92 43 local id = request.get_id_string()
jbe@92 44 local params = request.get_param_strings()
jbe/bsw@0 45 local function pagination_elements()
jbe/bsw@0 46 if page_count > 1 then
jbe/bsw@0 47 for page = 1, page_count do
jbe/bsw@0 48 if page > 1 then
jbe/bsw@0 49 slot.put(" ")
jbe/bsw@0 50 end
jbe/bsw@0 51 params[name] = page
jbe@568 52 local attr = table.new(link_attr)
jbe/bsw@0 53 if current_page == page then
jbe@568 54 if attr.class then
jbe@568 55 attr.class = attr.class .. " active"
jbe@568 56 else
jbe@568 57 attr.class = "active"
jbe@568 58 end
jbe/bsw@0 59 end
jbe/bsw@0 60 ui.link{
jbe/bsw@11 61 attr = attr,
jbe/bsw@0 62 module = request.get_module(),
jbe/bsw@0 63 view = request.get_view(),
jbe/bsw@0 64 id = id,
jbe/bsw@0 65 params = params,
jbe@568 66 anchor = anchor,
jbe@223 67 text = tostring(page)
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{
jbe@568 73 attr = container_attr,
jbe/bsw@0 74 content = function()
jbe@568 75 if position == 'before' or position == 'both' then
jbe@568 76 ui.container{
jbe@568 77 attr = { class = 'ui_paginate_head ui_paginate_select' },
jbe@568 78 content = pagination_elements
jbe@568 79 }
jbe@568 80 end
jbe/bsw@0 81 ui.container{
jbe/bsw@0 82 attr = { class = 'ui_paginate_content' },
jbe/bsw@0 83 content = content
jbe/bsw@0 84 }
jbe@568 85 if position == 'after' or position == 'both' then
jbe@568 86 ui.container{
jbe@568 87 attr = { class = 'ui_paginate_foot ui_paginate_select' },
jbe@568 88 content = pagination_elements
jbe@568 89 }
jbe@568 90 end
jbe/bsw@0 91 end
jbe/bsw@0 92 }
jbe/bsw@0 93 end

Impressum / About Us