webmcp
view framework/env/ui/paginate.lua @ 1:985024b16520
Version 1.0.1
New feature: JSON requests
Changes in ui.paginate: Current page setting is directly fetched from CGI params, instead of view params
Changed behavior of load methods of atom library to accept nil as input
Bugfixes in mondelefant_atom_connector timestamp(tz) loaders
Added global constant _WEBMCP_VERSION containing a version string
New feature: JSON requests
Changes in ui.paginate: Current page setting is directly fetched from CGI params, instead of view params
Changed behavior of load methods of atom library to accept nil as input
Bugfixes in mondelefant_atom_connector timestamp(tz) loaders
Added global constant _WEBMCP_VERSION containing a version string
| author | jbe | 
|---|---|
| date | Tue Nov 17 12:00:00 2009 +0100 (2009-11-17) | 
| parents | 9fdfb27f8e67 | 
| children | 72860d232f32 | 
 line source
     1 --[[--
     2 ui.paginate{
     3   selector = selector,  -- a selector for items from the database
     4   per_page = per_page,  -- items per page, defaults to 10
     5   name     = name,      -- name of the CGI get variable, defaults to "page"
     6   page     = page,      -- directly specify a page, and ignore 'name' parameter
     7   content = function()
     8     ...                 -- code block which should be encapsulated with page selection links
     9   end
    10 }
    12 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.
    14 --]]--
    16 function ui.paginate(args)
    17   local selector = args.selector
    18   local per_page = args.per_page or 10
    19   local name     = args.name or 'page'
    20   local content  = args.content
    21   local count_selector = selector:get_db_conn():new_selector()
    22   count_selector:add_field('count(1)')
    23   count_selector:add_from(selector)
    24   count_selector:single_object_mode()
    25   local count = count_selector:exec().count
    26   local page_count = math.floor((count - 1) / per_page) + 1
    27   local current_page = atom.integer:load(cgi.params[name]) or 1
    28   selector:limit(per_page)
    29   selector:offset((current_page - 1) * per_page)
    30   local id     = param.get_id_cgi()
    31   local params = param.get_all_cgi()
    32   local function pagination_elements()
    33     if page_count > 1 then
    34       for page = 1, page_count do
    35         if page > 1 then
    36           slot.put(" ")
    37         end
    38         params[name] = page
    39         local attr = {}
    40         if current_page == page then
    41           attr.class = "active"
    42         end
    43         ui.link{
    44           attr = attr,
    45           module = request.get_module(),
    46           view   = request.get_view(),
    47           id     = id,
    48           params = params,
    49           text   = tostring(page)
    50         }
    51       end
    52     end
    53   end
    54   ui.container{
    55     attr = { class = 'ui_paginate' },
    56     content = function()
    57       ui.container{
    58         attr = { class = 'ui_paginate_head ui_paginate_select' },
    59         content = pagination_elements
    60       }
    61       ui.container{
    62         attr = { class = 'ui_paginate_content' },
    63         content = content
    64       }
    65       ui.container{
    66         attr = { class = 'ui_paginate_foot ui_paginate_select' },
    67         content = pagination_elements
    68       }
    69     end
    70   }
    71 end
