webmcp

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

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children 6e08067e66c1
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/ui/field/select.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,68 @@
     1.4 +--[[--
     1.5 +ui.field.select{
     1.6 +  ...                                 -- generic ui.field.* arguments, as described for ui.autofield{...}
     1.7 +  foreign_records = foreign_records,  -- list of records to be chosen from, or function returning such a list
     1.8 +  foreign_id      = foreign_id,       -- name of id field in foreign records
     1.9 +  foreign_name    = foreign_name,     -- name of field to be used as name in foreign records
    1.10 +  format_options  = format_options    -- format options for format.string
    1.11 +}
    1.12 +
    1.13 +This function inserts a select field in the active slot. For description of the generic field helper arguments, see help for ui.autofield{...}.
    1.14 +
    1.15 +--]]--
    1.16 +
    1.17 +function ui.field.select(args)
    1.18 +  ui.form_element(args, {fetch_value = true}, function(args)
    1.19 +    local foreign_records = args.foreign_records
    1.20 +    if type(foreign_records) == "function" then
    1.21 +      foreign_records = foreign_records(args.record)
    1.22 +    end
    1.23 +    if args.readonly then
    1.24 +      local name
    1.25 +      for idx, record in ipairs(foreign_records) do
    1.26 +        if record[args.foreign_id] == args.value then
    1.27 +          name = record[args.foreign_name]
    1.28 +          break
    1.29 +        end
    1.30 +      end
    1.31 +      ui.tag{
    1.32 +        tag     = args.tag,
    1.33 +        attr    = args.attr, 
    1.34 +        content = format.string(name, args.format_options)
    1.35 +      }
    1.36 +    else
    1.37 +      local attr = table.new(args.attr)
    1.38 +      attr.name  = args.html_name
    1.39 +      ui.tag{
    1.40 +        tag     = "select",
    1.41 +        attr    = attr,
    1.42 +        content = function()
    1.43 +          if args.nil_as then
    1.44 +            ui.tag{
    1.45 +              tag     = "option",
    1.46 +              attr    = { value = "" },
    1.47 +              content = format.string(
    1.48 +                args.nil_as,
    1.49 +                args.format_options
    1.50 +              )
    1.51 +            }
    1.52 +          end
    1.53 +          for idx, record in ipairs(foreign_records) do
    1.54 +            local key = record[args.foreign_id]
    1.55 +            ui.tag{
    1.56 +              tag     = "option",
    1.57 +              attr    = {
    1.58 +                value    = key,
    1.59 +                selected = ((key == args.value) and "selected" or nil)
    1.60 +              },
    1.61 +              content = format.string(
    1.62 +                record[args.foreign_name],
    1.63 +                args.format_options
    1.64 +              )
    1.65 +            }
    1.66 +          end
    1.67 +        end
    1.68 +      }
    1.69 +    end
    1.70 +  end)
    1.71 +end

Impressum / About Us