webmcp
view framework/env/ui/field/select.lua @ 51:791849c58105
class_prototype.object:try_save() can create rows with DEFAULT VALUES
| author | jbe | 
|---|---|
| date | Fri Jun 10 14:23:35 2011 +0200 (2011-06-10) | 
| parents | 9b93943e2e6e | 
| children | faedbc8615e0 | 
 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   disabled_records = disabled_records  -- table of record keys that should be disabled (NOTE: Preliminary. Argument name may change in future releases.)
     9 }
    11 This function inserts a select field in the active slot. For description of the generic field helper arguments, see help for ui.autofield{...}.
    13 --]]--
    15 -- TODO: Find better name for "disabled_records"
    17 function ui.field.select(args)
    18   ui.form_element(args, {fetch_value = true}, function(args)
    19     local foreign_records = args.foreign_records
    20     if type(foreign_records) == "function" then
    21       foreign_records = foreign_records(args.record)
    22     end
    23     if args.readonly then
    24       local name
    25       for idx, record in ipairs(foreign_records) do
    26         if record[args.foreign_id] == args.value then
    27           name = record[args.foreign_name]
    28           break
    29         end
    30       end
    31       ui.tag{
    32         tag     = args.tag,
    33         attr    = args.attr, 
    34         content = format.string(name, args.format_options)
    35       }
    36     else
    37       local attr = table.new(args.attr)
    38       attr.name  = args.html_name
    39       ui.tag{
    40         tag     = "select",
    41         attr    = attr,
    42         content = function()
    43           if args.nil_as then
    44             ui.tag{
    45               tag     = "option",
    46               attr    = { value = "" },
    47               content = format.string(
    48                 args.nil_as,
    49                 args.format_options
    50               )
    51             }
    52           end
    53           for idx, record in ipairs(foreign_records) do
    54             local key = record[args.foreign_id]
    55             ui.tag{
    56               tag     = "option",
    57               attr    = {
    58                 value    = key,
    59                 disabled = ((args.disabled_records and args.disabled_records[key] and "1" ) or nil),
    60                 selected = ((key == args.value) and "selected" or nil)
    61               },
    62               content = format.string(
    63                 record[args.foreign_name],
    64                 args.format_options
    65               )
    66             }
    67           end
    68         end
    69       }
    70     end
    71   end)
    72 end
