webmcp

diff framework/env/ui/paginate.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children 985024b16520
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/ui/paginate.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,69 @@
     1.4 +--[[--
     1.5 +ui.paginate{
     1.6 +  selector = selector,  -- a selector for items from the database
     1.7 +  per_page = per_page,  -- items per page, defaults to 10
     1.8 +  name     = name,      -- name of the CGI get variable, defaults to "page"
     1.9 +  content = function()
    1.10 +    ...                 -- code block which should be encapsulated with page selection links
    1.11 +  end
    1.12 +}
    1.13 +
    1.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.
    1.15 +--]]--
    1.16 +
    1.17 +function ui.paginate(args)
    1.18 +  local selector = args.selector
    1.19 +  local per_page = args.per_page or 10
    1.20 +  local name     = args.name or 'page'
    1.21 +  local content  = args.content
    1.22 +  local count_selector = selector:get_db_conn():new_selector()
    1.23 +  count_selector:add_field('count(1)')
    1.24 +  count_selector:add_from(selector)
    1.25 +  count_selector:single_object_mode()
    1.26 +  local count = count_selector:exec().count
    1.27 +  local page_count = math.floor((count - 1) / per_page) + 1
    1.28 +  local current_page = param.get(name, atom.integer) or 1
    1.29 +  selector:limit(per_page)
    1.30 +  selector:offset((current_page - 1) * per_page)
    1.31 +  local id     = param.get_id_cgi()
    1.32 +  local params = param.get_all_cgi()
    1.33 +  local function pagination_elements()
    1.34 +    if page_count > 1 then
    1.35 +      for page = 1, page_count do
    1.36 +        if page > 1 then
    1.37 +          slot.put(" ")
    1.38 +        end
    1.39 +        params[name] = page
    1.40 +        local attr = {}
    1.41 +        if current_page == page then
    1.42 +          attr.class = "active"
    1.43 +        end
    1.44 +        ui.link{
    1.45 +          attr = attr,
    1.46 +          module = request.get_module(),
    1.47 +          view   = request.get_view(),
    1.48 +          id     = id,
    1.49 +          params = params,
    1.50 +          text   = tostring(page)
    1.51 +        }
    1.52 +      end
    1.53 +    end
    1.54 +  end
    1.55 +  ui.container{
    1.56 +    attr = { class = 'ui_paginate' },
    1.57 +    content = function()
    1.58 +      ui.container{
    1.59 +        attr = { class = 'ui_paginate_head ui_paginate_select' },
    1.60 +        content = pagination_elements
    1.61 +      }
    1.62 +      ui.container{
    1.63 +        attr = { class = 'ui_paginate_content' },
    1.64 +        content = content
    1.65 +      }
    1.66 +      ui.container{
    1.67 +        attr = { class = 'ui_paginate_foot ui_paginate_select' },
    1.68 +        content = pagination_elements
    1.69 +      }
    1.70 +    end
    1.71 +  }
    1.72 +end

Impressum / About Us