jbe/bsw@0: --[[-- jbe/bsw@0: ui.paginate{ jbe@569: selector = selector, -- a selector for items from the database (will be modified) jbe@569: anchor = anchor, -- optional name of anchor in document to jump to jbe@569: per_page = per_page, -- items per page, defaults to 10 jbe@569: container_attr = container_attr, -- html attr for the container element jbe@569: link_attr = link_attr, -- html attr for each page link jbe@569: active_link_attr = active_link_attr, -- alternative html attr for the active page link jbe@569: name = name, -- name of the CGI get variable, defaults to "page" jbe@569: position = position, -- position of page links relative to content: "before", "after", or "both" (default) jbe@569: content = function() jbe@569: ... -- code block which should be encapsulated with page selection links jbe/bsw@0: end jbe/bsw@0: } jbe/bsw@0: jbe@223: 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: jbe/bsw@0: --]]-- jbe/bsw@0: jbe/bsw@0: function ui.paginate(args) jbe@569: local selector = args.selector jbe@569: local anchor = args.anchor jbe@569: local per_page = args.per_page or 10 jbe@569: local container_attr = args.container_attr or { class = 'ui_paginate' } jbe@569: local link_attr = args.link_attr or {} jbe@569: local active_link_attr = args.active_link_attr or link_attr jbe@569: local name = args.name or 'page' jbe@569: local position = args.position or 'both' jbe@569: local content = args.content jbe/bsw@0: local count_selector = selector:get_db_conn():new_selector() jbe/bsw@0: count_selector:add_field('count(1)') jbe/bsw@0: count_selector:add_from(selector) jbe/bsw@0: count_selector:single_object_mode() jbe/bsw@0: local count = count_selector:exec().count jbe@8: local page_count = 1 jbe@8: if count > 0 then jbe@8: page_count = math.floor((count - 1) / per_page) + 1 jbe@8: end jbe@223: local current_page = atom.integer:load(request.get_param{name=name}) or 1 jbe/bsw@2: if current_page > page_count then jbe/bsw@2: current_page = page_count jbe/bsw@2: end jbe/bsw@0: selector:limit(per_page) jbe/bsw@0: selector:offset((current_page - 1) * per_page) jbe@92: local id = request.get_id_string() jbe@92: local params = request.get_param_strings() jbe/bsw@0: local function pagination_elements() jbe/bsw@0: if page_count > 1 then jbe/bsw@0: for page = 1, page_count do jbe/bsw@0: if page > 1 then jbe/bsw@0: slot.put(" ") jbe/bsw@0: end jbe/bsw@0: params[name] = page jbe@569: local attr jbe/bsw@0: if current_page == page then jbe@569: attr = table.new(active_link_attr) jbe@568: if attr.class then jbe@568: attr.class = attr.class .. " active" jbe@568: else jbe@568: attr.class = "active" jbe@568: end jbe@569: else jbe@569: attr = table.new(link_attr) jbe/bsw@0: end jbe/bsw@0: ui.link{ jbe/bsw@11: attr = attr, jbe/bsw@0: module = request.get_module(), jbe/bsw@0: view = request.get_view(), jbe/bsw@0: id = id, jbe/bsw@0: params = params, jbe@568: anchor = anchor, jbe@223: text = tostring(page) jbe/bsw@0: } jbe/bsw@0: end jbe/bsw@0: end jbe/bsw@0: end jbe/bsw@0: ui.container{ jbe@568: attr = container_attr, jbe/bsw@0: content = function() jbe@568: if position == 'before' or position == 'both' then jbe@568: ui.container{ jbe@568: attr = { class = 'ui_paginate_head ui_paginate_select' }, jbe@568: content = pagination_elements jbe@568: } jbe@568: end jbe/bsw@0: ui.container{ jbe/bsw@0: attr = { class = 'ui_paginate_content' }, jbe/bsw@0: content = content jbe/bsw@0: } jbe@568: if position == 'after' or position == 'both' then jbe@568: ui.container{ jbe@568: attr = { class = 'ui_paginate_foot ui_paginate_select' }, jbe@568: content = pagination_elements jbe@568: } jbe@568: end jbe/bsw@0: end jbe/bsw@0: } jbe/bsw@0: end