jbe/bsw@0: --[[-- jbe/bsw@0: ui.paginate{ jbe/bsw@0: selector = selector, -- a selector for items from the database jbe/bsw@0: per_page = per_page, -- items per page, defaults to 10 jbe/bsw@0: name = name, -- name of the CGI get variable, defaults to "page" jbe@1: page = page, -- directly specify a page, and ignore 'name' parameter jbe/bsw@0: content = function() jbe/bsw@0: ... -- code block which should be encapsulated with page selection links jbe/bsw@0: end jbe/bsw@0: } jbe/bsw@0: jbe@1: 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: jbe/bsw@0: --]]-- jbe/bsw@0: jbe/bsw@0: function ui.paginate(args) jbe/bsw@0: local selector = args.selector jbe/bsw@0: local per_page = args.per_page or 10 jbe/bsw@0: local name = args.name or 'page' jbe/bsw@0: 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/bsw@0: local page_count = math.floor((count - 1) / per_page) + 1 jbe@1: local current_page = atom.integer:load(cgi.params[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/bsw@0: local id = param.get_id_cgi() jbe/bsw@0: local params = param.get_all_cgi() 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/bsw@0: local attr = {} jbe/bsw@0: if current_page == page then jbe/bsw@0: attr.class = "active" jbe/bsw@0: end jbe/bsw@0: ui.link{ jbe/bsw@0: 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/bsw@0: text = tostring(page) jbe/bsw@0: } jbe/bsw@0: end jbe/bsw@0: end jbe/bsw@0: end jbe/bsw@0: ui.container{ jbe/bsw@0: attr = { class = 'ui_paginate' }, jbe/bsw@0: content = function() jbe/bsw@0: ui.container{ jbe/bsw@0: attr = { class = 'ui_paginate_head ui_paginate_select' }, jbe/bsw@0: content = pagination_elements jbe/bsw@0: } jbe/bsw@0: ui.container{ jbe/bsw@0: attr = { class = 'ui_paginate_content' }, jbe/bsw@0: content = content jbe/bsw@0: } jbe/bsw@0: ui.container{ jbe/bsw@0: attr = { class = 'ui_paginate_foot ui_paginate_select' }, jbe/bsw@0: content = pagination_elements jbe/bsw@0: } jbe/bsw@0: end jbe/bsw@0: } jbe/bsw@0: end