webmcp
view framework/env/ui/paginate.lua @ 349:8cf6d927d074
Fixed commas in autodoc comments
| author | jbe | 
|---|---|
| date | Thu Mar 26 12:25:19 2015 +0100 (2015-03-26) | 
| parents | 32ec28229bb5 | 
| children | c43f251262d8 | 
 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 request.get_param{...}, 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(request.get_param{name=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     = request.get_id_string()
    39   local params = request.get_param_strings()
    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         ui.link{
    52           attr   = attr,
    53           module = request.get_module(),
    54           view   = request.get_view(),
    55           id     = id,
    56           params = params,
    57           anchor = args.anchor,
    58           text   = tostring(page)
    59         }
    60       end
    61     end
    62   end
    63   ui.container{
    64     attr = args.container_attr or { class = 'ui_paginate' },
    65     content = function()
    66       ui.container{
    67         attr = { class = 'ui_paginate_head ui_paginate_select' },
    68         content = pagination_elements
    69       }
    70       ui.container{
    71         attr = { class = 'ui_paginate_content' },
    72         content = content
    73       }
    74       ui.container{
    75         attr = { class = 'ui_paginate_foot ui_paginate_select' },
    76         content = pagination_elements
    77       }
    78     end
    79   }
    80 end
