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

72
LINES

< > BotCompany Repo | #1021721 // htmlTable2 (make HTML table from arbitrary data, more options)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5293L/32K).

1  
// func(S) -> S
2  
static new ThreadLocal htmlTable2_cellEncoder;
3  
4  
// htmlEncode = true
5  
static S htmlTable2(O data, O... _) {
6  
  bool htmlEncode = optPar htmlEncode(_, true);
7  
  optPar bool useBr;
8  
  optPar Map<S, O[]> paramsByColName;
9  
  optPar O[] tableParams;
10  
  optPar O[] trParams;
11  
  optPar O[] tdParams;
12  
  optPar SS replaceHeaders; // optionally replace HTML contents for header row
13  
  optPar bool noHeader;
14  
  
15  
  // prepare table
16  
  
17  
  new LL<S> rows;
18  
  LS cols = new ContentsIndexedList;
19  
  
20  
  if (data instanceof L) {
21  
    for (O x : (L) data) pcall {
22  
      rows.add(dataToTable_makeRow(x, cols));
23  
    }
24  
  } else if (data instanceof Map) {
25  
    Map map = cast data;
26  
    for (O key : map.keySet()) {
27  
      O value = map.get(key);
28  
      rows.add(litlist(structureOrText(key), structureOrText(value)));
29  
    }
30  
  } else
31  
    print("Unknown data type: " + data);
32  
    
33  
  // get table width
34  
  int w = 0;
35  
  for (L<S> row : rows)
36  
    w = max(w, l(row));
37  
    
38  
  // construct HTML for table
39  
  
40  
  new StringBuilder buf;
41  
  buf.append(hopeningtag('table, paramsPlus(tableParams, border := html_valueLessParam())) + "\n");
42  
  
43  
  // title
44  
  if (!noHeader) {
45  
    buf.append("<tr>\n");
46  
    for (S cell : padList(cols, w, ""))
47  
      buf.append("  <th>" + htmlTable2_encodeCell(
48  
        getOrKeep(replaceHeaders, cell), htmlEncode, useBr) + "</th>\n");
49  
    buf.append("</tr>\n");
50  
  }
51  
  
52  
  // data
53  
  for (L<S> row : rows) {
54  
    buf.append(hopeningtag tr(trParams));
55  
    int i = 0;
56  
    for (S cell : padList(row, w, "")) {
57  
      S col = get(cols, i++);
58  
      O[] params = paramsPlus(tdParams, mapGet(paramsByColName, col));
59  
      buf.append("  " + tag('td, htmlTable2_encodeCell(cell, htmlEncode, useBr), params) + "\n");
60  
    }
61  
    buf.append("</tr>\n");
62  
  }
63  
  buf.append("</table>\n");
64  
  ret buf.toString();
65  
}
66  
67  
static S htmlTable2_encodeCell(S cell, boolean useHtmlEncode, boolean useBr) {
68  
  if (htmlTable2_cellEncoder! != null) ret (S) callF(htmlTable2_cellEncoder!, cell);
69  
  if (useHtmlEncode) cell = htmlEncode2(cell);
70  
  if (useBr) cell = nlToBr(cell);
71  
  ret cell;
72  
}

Author comment

Began life as a copy of #1001986

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1021721
Snippet name: htmlTable2 (make HTML table from arbitrary data, more options)
Eternal ID of this version: #1021721/17
Text MD5: eb4143dab1b929ee89fb07612c95ad2e
Transpilation MD5: 6bdb5e2c4f98606bcc282264eda1d941
Author: stefan
Category: javax html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-01-20 15:39:36
Source code size: 2111 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 492 / 599
Version history: 16 change(s)
Referenced in: [show references]