webmcp
view framework/env/ui/paginate.lua @ 89:24ed2cd053aa
Bugfix for :get_current() methods in atom library (missing import of "os" functions)
| author | jbe | 
|---|---|
| date | Tue Aug 21 00:55:26 2012 +0200 (2012-08-21) | 
| parents | ca3f7b001014 | 
| children | 2f8d8edd1836 | 
 line source
     1 --[[--
     2 ui.paginate{
     3   selector       = selector,       -- a selector for items from the database (will be modified)
     4   anchor         = anchor,         -- optional name of anchor in document to jump to
     5   per_page       = per_page,       -- items per page, defaults to 10
     6   container_attr = container_attr  -- html attr for the container element
     7   name           = name,           -- name of the CGI get variable, defaults to "page"
     8   page           = page,           -- directly specify a page, and ignore 'name' parameter
     9   content        = function()
    10     ...                            -- code block which should be encapsulated with page selection links
    11   end
    12 }
    14 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.
    16 --]]--
    18 function ui.paginate(args)
    19   local selector = args.selector
    20   local per_page = args.per_page or 10
    21   local name     = args.name or 'page'
    22   local content  = args.content
    23   local count_selector = selector:get_db_conn():new_selector()
    24   count_selector:add_field('count(1)')
    25   count_selector:add_from(selector)
    26   count_selector:single_object_mode()
    27   local count = count_selector:exec().count
    28   local page_count = 1
    29   if count > 0 then
    30     page_count = math.floor((count - 1) / per_page) + 1
    31   end
    32   local current_page = atom.integer:load(cgi.params[name]) or 1
    33   if current_page > page_count then
    34     current_page = page_count
    35   end
    36   selector:limit(per_page)
    37   selector:offset((current_page - 1) * per_page)
    38   local id     = param.get_id_cgi()
    39   local params = param.get_all_cgi()
    40   local function pagination_elements()
    41     if page_count > 1 then
    42       for page = 1, page_count do
    43         if page > 1 then
    44           slot.put(" ")
    45         end
    46         params[name] = page
    47         local attr = {}
    48         if current_page == page then
    49           attr.class = "active"
    50         end
    51         local partial
    52         if ui.is_partial_loading_enabled() then
    53           partial = {
    54             params = {
    55               [name] = tostring(page)
    56             }
    57           }
    58         end
    59         ui.link{
    60           attr   = attr,
    61           module = request.get_module(),
    62           view   = request.get_view(),
    63           id     = id,
    64           params = params,
    65           anchor = args.anchor,
    66           text   = tostring(page),
    67           partial = partial
    68         }
    69       end
    70     end
    71   end
    72   ui.container{
    73     attr = args.container_attr or { class = 'ui_paginate' },
    74     content = function()
    75       ui.container{
    76         attr = { class = 'ui_paginate_head ui_paginate_select' },
    77         content = pagination_elements
    78       }
    79       ui.container{
    80         attr = { class = 'ui_paginate_content' },
    81         content = content
    82       }
    83       ui.container{
    84         attr = { class = 'ui_paginate_foot ui_paginate_select' },
    85         content = pagination_elements
    86       }
    87     end
    88   }
    89 end
