Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

64
LINES

< > BotCompany Repo | #1001986 // htmlTable (make HTML table from arbitrary data)

JavaX fragment (include)

1  
// func(S) -> S
2  
static new ThreadLocal htmlTable_cellEncoder;
3  
4  
// htmlEncode = true
5  
static S htmlTable(O data) {
6  
  ret htmlTable(data, true);
7  
}
8  
9  
static S htmlTable(O data, boolean useHtmlEncode) {
10  
  ret htmlTable(data, useHtmlEncode, false);
11  
}
12  
13  
static S htmlTable(O data, boolean useHtmlEncode, boolean useBr) {
14  
  // prepare table
15  
  
16  
  new L<L<S>> rows;
17  
  new L<S> cols;
18  
  
19  
  if (data instanceof L) {
20  
    for (O x : (L) data) pcall {
21  
      rows.add(dataToTable_makeRow(x, cols));
22  
    }
23  
  } else if (data instanceof Map) {
24  
    Map map = cast data;
25  
    for (O key : map.keySet()) {
26  
      O value = map.get(key);
27  
      rows.add(litlist(structureOrText(key), structureOrText(value)));
28  
    }
29  
  } else
30  
    print("Unknown data type: " + data);
31  
    
32  
  // get table width
33  
  int w = 0;
34  
  for (L<S> row : rows)
35  
    w = max(w, l(row));
36  
    
37  
  // construct HTML for table
38  
  
39  
  new StringBuilder buf;
40  
  buf.append("<table border>\n");
41  
  
42  
  // title
43  
  buf.append("<tr>\n");
44  
  for (S cell : padList(cols, w, "?"))
45  
    buf.append("  <th>" + htmlTable_encodeCell(cell, useHtmlEncode, useBr) + "</th>\n");
46  
  buf.append("</tr>\n");
47  
  
48  
  // data
49  
  for (L<S> row : rows) {
50  
    buf.append("<tr>\n");
51  
    for (S cell : padList(row, w, ""))
52  
      buf.append("  <td>" + htmlTable_encodeCell(cell, useHtmlEncode, useBr) + "</td>\n");
53  
    buf.append("</tr>\n");
54  
  }
55  
  buf.append("</table>\n");
56  
  ret buf.toString();
57  
}
58  
59  
static S htmlTable_encodeCell(S cell, boolean useHtmlEncode, boolean useBr) {
60  
  if (htmlTable_cellEncoder! != null) ret (S) callF(htmlTable_cellEncoder!, cell);
61  
  if (useHtmlEncode) cell = htmlEncode(cell);
62  
  if (useBr) cell = nlToBr(cell);
63  
  ret cell;
64  
}

Author comment

Began life as a copy of #1001613

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
1220 stefan htmlencode default = true
useBr default = false
2016-04-28 21:15:07

add comment

Snippet ID: #1001986
Snippet name: htmlTable (make HTML table from arbitrary data)
Eternal ID of this version: #1001986/4
Text MD5: 10bb32b74c0fe8d9c5e19ef04cfb66e1
Author: stefan
Category: javax html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-24 12:23:09
Source code size: 1708 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 811 / 1202
Version history: 3 change(s)
Referenced in: [show references]