webmcp
view framework/env/ui/field/select.lua @ 3:795b764629ca
Version 1.0.3
Important bugfix related to internal forwards (Bug was introduced by the restriction of views with underscore prefix in Version 1.0.2)
Important bugfix related to internal forwards (Bug was introduced by the restriction of views with underscore prefix in Version 1.0.2)
| author | jbe | 
|---|---|
| date | Thu Dec 10 12:00:00 2009 +0100 (2009-12-10) | 
| parents | 9fdfb27f8e67 | 
| children | 6e08067e66c1 | 
 line source
     1 --[[--
     2 ui.field.select{
     3   ...                                 -- generic ui.field.* arguments, as described for ui.autofield{...}
     4   foreign_records = foreign_records,  -- list of records to be chosen from, or function returning such a list
     5   foreign_id      = foreign_id,       -- name of id field in foreign records
     6   foreign_name    = foreign_name,     -- name of field to be used as name in foreign records
     7   format_options  = format_options    -- format options for format.string
     8 }
    10 This function inserts a select field in the active slot. For description of the generic field helper arguments, see help for ui.autofield{...}.
    12 --]]--
    14 function ui.field.select(args)
    15   ui.form_element(args, {fetch_value = true}, function(args)
    16     local foreign_records = args.foreign_records
    17     if type(foreign_records) == "function" then
    18       foreign_records = foreign_records(args.record)
    19     end
    20     if args.readonly then
    21       local name
    22       for idx, record in ipairs(foreign_records) do
    23         if record[args.foreign_id] == args.value then
    24           name = record[args.foreign_name]
    25           break
    26         end
    27       end
    28       ui.tag{
    29         tag     = args.tag,
    30         attr    = args.attr, 
    31         content = format.string(name, args.format_options)
    32       }
    33     else
    34       local attr = table.new(args.attr)
    35       attr.name  = args.html_name
    36       ui.tag{
    37         tag     = "select",
    38         attr    = attr,
    39         content = function()
    40           if args.nil_as then
    41             ui.tag{
    42               tag     = "option",
    43               attr    = { value = "" },
    44               content = format.string(
    45                 args.nil_as,
    46                 args.format_options
    47               )
    48             }
    49           end
    50           for idx, record in ipairs(foreign_records) do
    51             local key = record[args.foreign_id]
    52             ui.tag{
    53               tag     = "option",
    54               attr    = {
    55                 value    = key,
    56                 selected = ((key == args.value) and "selected" or nil)
    57               },
    58               content = format.string(
    59                 record[args.foreign_name],
    60                 args.format_options
    61               )
    62             }
    63           end
    64         end
    65       }
    66     end
    67   end)
    68 end
