webmcp

view framework/env/ui/field/select.lua @ 30:6e08067e66c1

allow disabled records in select widget

this allows the use record entries as seperators
author Daniel Poelzleithner <poelzi@poelzi.org>
date Mon Sep 20 20:11:29 2010 +0200 (2010-09-20)
parents 9fdfb27f8e67
children 9b93943e2e6e
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_list -- table of record keys that should be disabled
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 function ui.field.select(args)
16 ui.form_element(args, {fetch_value = true}, function(args)
17 local foreign_records = args.foreign_records
18 if type(foreign_records) == "function" then
19 foreign_records = foreign_records(args.record)
20 end
21 if args.readonly then
22 local name
23 for idx, record in ipairs(foreign_records) do
24 if record[args.foreign_id] == args.value then
25 name = record[args.foreign_name]
26 break
27 end
28 end
29 ui.tag{
30 tag = args.tag,
31 attr = args.attr,
32 content = format.string(name, args.format_options)
33 }
34 else
35 local attr = table.new(args.attr)
36 attr.name = args.html_name
37 ui.tag{
38 tag = "select",
39 attr = attr,
40 content = function()
41 if args.nil_as then
42 ui.tag{
43 tag = "option",
44 attr = { value = "" },
45 content = format.string(
46 args.nil_as,
47 args.format_options
48 )
49 }
50 end
51 for idx, record in ipairs(foreign_records) do
52 local key = record[args.foreign_id]
53 ui.tag{
54 tag = "option",
55 attr = {
56 value = key,
57 disabled = ((args.disabled_records and args.disabled_records[key] and "1" ) or nil),
58 selected = ((key == args.value) and "selected" or nil)
59 },
60 content = format.string(
61 record[args.foreign_name],
62 args.format_options
63 )
64 }
65 end
66 end
67 }
68 end
69 end)
70 end

Impressum / About Us