webmcp
view framework/env/ui_deprecated/list.lua @ 147:da5ab2c226dc
Fixes and documentation in json_setnull and json_length functions of JSON library
| author | jbe | 
|---|---|
| date | Wed Jul 30 22:19:34 2014 +0200 (2014-07-30) | 
| parents | 9fdfb27f8e67 | 
| children | 
 line source
     1 --
     2 -- Creates a list view of a collection
     3 --
     4 -- Example:
     5 --
     6 --   ui_deprecated.list({ 
     7 --     label = 'Point table',
     8 --     collection = mycollection,
     9 --     type = 'table' -- 'table', 'ulli'
    10 --     cols = {
    11 --       { 
    12 --         label = _'Id',
    13 --         field = 'id',
    14 --         width = 100,
    15 --         link = { module = 'mymodule', view = 'show' },
    16 --              sort = true,
    17 --       },
    18 --       {
    19 --         label = _'Name',
    20 --         field = 'name',
    21 --         width = 200,
    22 --         link = { module = 'mymodule', view = 'show' },
    23 --              sort = true,
    24 --       },
    25 --      {
    26 --        label = _'Points',
    27 --        func = function(record)
    28 --          return record.points_a + record.points_b + record.points_c
    29 --        end,
    30 --        width = 300
    31 --      }
    32 --     }
    33 --   })
    34 --
    36 function ui_deprecated.list(args)
    37   local args = args
    38   args.type = args.type or 'table'
    39   if args.label then
    40     slot.put('<div class="ui_list_label">' .. encode.html(args.label) .. '</div>\n')
    41   end
    42   if not args or not args.collection or not args.cols then
    43     error('No args for ui_deprecated.list_end')
    44   end
    45   if args.type == 'table' then
    46     slot.put('<table class="ui_list">')
    47     slot.put('<tr class="ui_list_head">')
    48   elseif args.type == 'ulli' then
    49     slot.put('<ul class="ui_list">')
    50     slot.put('<li class="ui_list_head">')
    51   end
    52   if args.type == 'table' then
    53   elseif args.type == 'ulli' then
    54   end
    55   for j, col in ipairs(args.cols) do
    56     class_string = ''
    57     if col.align then
    58       class_string = ' class="' .. col.align .. '"'
    59     end
    60     if args.type == 'table' then
    61       slot.put('<th style="width: ' .. col.width .. ';"' .. class_string ..'>')
    62       slot.put(ui_deprecated.box({ name = 'ui_list_col_value', content = encode.html(col.label) }) )
    63       slot.put('</th>')
    64     elseif args.type == 'ulli' then
    65       slot.put('<div style="width: ' .. col.width .. ';"' .. class_string ..'>')
    66       slot.put(ui_deprecated.box({ name = 'ui_list_col_value', content = encode.html(col.label) }) )
    67       slot.put('</div>')
    68     end
    69   end
    70   if args.type == 'table' then
    71     slot.put('</tr>')
    72   elseif args.type == 'ulli' then
    73     slot.put('<br style="clear: left;" />')
    74     slot.put('</li>')
    75   end
    76   for i, obj in ipairs(args.collection) do
    77     if args.type == 'table' then
    78       slot.put('<tr>')
    79     elseif args.type == 'ulli' then
    80       slot.put('<li>')
    81     end
    82     for j, col in ipairs(args.cols) do
    83       class_string = ''
    84       if col.align then
    85         class_string = ' class="ui_list_col_align_' .. col.align .. '"'
    86       end
    87       if args.type == 'table' then
    88         slot.put('<td' .. class_string ..'>')
    89       elseif args.type == 'ulli' then
    90         slot.put('<div style="width: ' .. col.width .. ';"' .. class_string ..'>')
    91       end
    92       if col.link then
    93         local params = {}
    94         if col.link then
    95           params = col.link.params or {}
    96         end
    97         if col.link_values then
    98           for key, field in pairs(col.link_values) do
    99             params[key] = obj[field]
   100           end
   101         end
   102         local id
   103         if col.link_id_field then
   104           id = obj[col.link_id_field]
   105         end
   106         local value
   107         if col.value then
   108           value = col.value
   109         else
   110           value = obj[col.field]
   111         end
   112         ui_deprecated.link{
   113           label  = convert.to_human(value),
   114           module = col.link.module,
   115           view   = col.link.view,
   116           id     = id,
   117           params = params,
   118         }
   119       elseif col.link_func then
   120         local link = col.link_func(obj)
   121         if link then
   122 	      ui_deprecated.link(link)
   123 	    end
   124       else
   125         local text
   126         if col.func then
   127           text = col.func(obj)
   128         elseif col.value then
   129           text = convert.to_human(value)
   130         else
   131           text = convert.to_human(obj[col.field])
   132         end
   133         ui_deprecated.box{ name = 'ui_list_col_value', content = text }
   134       end
   135       if args.type == 'table' then
   136         slot.put('</td>')
   137       elseif args.type == 'ulli' then
   138         slot.put('</div>')
   139       end
   140     end
   141     if args.type == 'table' then
   142       slot.put('</tr>')
   143     elseif args.type == 'ulli' then
   144       slot.put('<br style="clear: left;" />')
   145       slot.put('</li>')
   146     end
   147   end
   148   if args.type == 'table' then
   149     slot.put('</table>')
   150   elseif args.type == 'ulli' then
   151     slot.put('</ul><div style="clear: left"> </div>')
   152   end
   153 end
