webmcp

annotate framework/env/ui/paginate.lua @ 569:5b19007574de

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

Impressum / About Us