webmcp
view framework/env/ui/field/select.lua @ 375:fb98b17056e5
Proper support for mutable data types (JSON objects/arrays) in table columns
| author | jbe | 
|---|---|
| date | Sat Nov 14 15:44:53 2015 +0100 (2015-11-14) | 
| parents | a6a00add617b | 
| children | 
 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   selected_record  = selected_record   -- id of (or reference to) record which is selected (optional, overrides "value" argument when not nil)
     9   disabled_records = disabled_records  -- table with ids of (or references to) records that should be disabled (stored as table keys mapped to true)
    10 }
    12 This function inserts a select field in the active slot. For description of the generic field helper arguments, see help for ui.autofield{...}.
    14 --]]--
    16 function ui.field.select(args)
    17   ui.form_element(args, {fetch_value = true}, function(args)
    18     local foreign_records = args.foreign_records
    19     if type(foreign_records) == "function" then
    20       foreign_records = foreign_records(args.record)
    21     end
    22     if args.readonly then
    23       local name
    24       for idx, record in ipairs(foreign_records) do
    25         if record[args.foreign_id] == args.value then
    26           name = record[args.foreign_name]
    27           break
    28         end
    29       end
    30       ui.tag{
    31         tag     = args.tag,
    32         attr    = args.attr, 
    33         content = format.string(name, args.format_options)
    34       }
    35     else
    36       local attr = table.new(args.attr)
    37       attr.name  = args.html_name
    38       ui.tag{
    39         tag     = "select",
    40         attr    = attr,
    41         content = function()
    42           if args.nil_as then
    43             ui.tag{
    44               tag     = "option",
    45               attr    = { value = "" },
    46               content = format.string(
    47                 args.nil_as,
    48                 args.format_options
    49               )
    50             }
    51           end
    52           local one_selected = false
    53           for idx, record in ipairs(foreign_records) do
    54             local key = record[args.foreign_id]
    55             local selected = false
    56             if not one_selected then
    57               if args.selected_record == nil then
    58                 if args.value == key then
    59                   selected = true
    60                 end
    61               else
    62                 if args.selected_record == record or args.selected_record == key then
    63                   selected = true
    64                 end
    65               end
    66               one_selected = selected
    67             end
    68             local disabled = false
    69             if args.disabled_records then
    70               if args.disabled_records[record] or args.disabled_records[key] then
    71                 disabled = true
    72               end
    73             end
    74             ui.tag{
    75               tag     = "option",
    76               attr    = {
    77                 value    = key,
    78                 disabled = disabled and "disabled" or nil,
    79                 selected = selected and "selected" or nil
    80               },
    81               content = format.string(
    82                 record[args.foreign_name],
    83                 args.format_options
    84               )
    85             }
    86           end
    87         end
    88       }
    89     end
    90   end)
    91 end
