webmcp

view framework/env/ui/field/select.lua @ 75:faedbc8615e0

Allow particular records to be selected and/or disabled in ui.field.select{...}, even if they share the same key, and never select more than one record
author jbe
date Wed Jun 20 00:35:02 2012 +0200 (2012-06-20)
parents 9b93943e2e6e
children a6a00add617b
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 -- TODO: Find better name for "disabled_records"
18 function ui.field.select(args)
19 ui.form_element(args, {fetch_value = true}, function(args)
20 local foreign_records = args.foreign_records
21 if type(foreign_records) == "function" then
22 foreign_records = foreign_records(args.record)
23 end
24 if args.readonly then
25 local name
26 for idx, record in ipairs(foreign_records) do
27 if record[args.foreign_id] == args.value then
28 name = record[args.foreign_name]
29 break
30 end
31 end
32 ui.tag{
33 tag = args.tag,
34 attr = args.attr,
35 content = format.string(name, args.format_options)
36 }
37 else
38 local attr = table.new(args.attr)
39 attr.name = args.html_name
40 ui.tag{
41 tag = "select",
42 attr = attr,
43 content = function()
44 if args.nil_as then
45 ui.tag{
46 tag = "option",
47 attr = { value = "" },
48 content = format.string(
49 args.nil_as,
50 args.format_options
51 )
52 }
53 end
54 local one_selected = false
55 for idx, record in ipairs(foreign_records) do
56 local key = record[args.foreign_id]
57 local selected = false
58 if not one_selected then
59 if args.selected_record == nil then
60 if args.value == key then
61 selected = true
62 end
63 else
64 if args.selected_record == record or args.selected_record == key then
65 selected = true
66 end
67 end
68 one_selected = selected
69 end
70 local disabled = false
71 if args.disabled_records then
72 if args.disabled_records[record] or args.disabled_records[key] then
73 disabled = true
74 end
75 end
76 ui.tag{
77 tag = "option",
78 attr = {
79 value = key,
80 disabled = disabled and "disabled" or nil,
81 selected = selected and "selected" or nil
82 },
83 content = format.string(
84 record[args.foreign_name],
85 args.format_options
86 )
87 }
88 end
89 end
90 }
91 end
92 end)
93 end

Impressum / About Us