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).

// func(S) -> S
static new ThreadLocal htmlTable2_cellEncoder;

// htmlEncode = true
static S htmlTable2(O data, O... _) {
  bool htmlEncode = optPar htmlEncode(_, true);
  optPar bool useBr;
  optPar Map<S, O[]> paramsByColName;
  optPar O[] tableParams;
  optPar O[] trParams;
  optPar O[] tdParams;
  optPar SS replaceHeaders; // optionally replace HTML contents for header row
  optPar bool noHeader;
  
  // prepare table
  
  new LL<S> rows;
  LS cols = new ContentsIndexedList;
  
  if (data instanceof L) {
    for (O x : (L) data) pcall {
      rows.add(dataToTable_makeRow(x, cols));
    }
  } else if (data instanceof Map) {
    Map map = cast data;
    for (O key : map.keySet()) {
      O value = map.get(key);
      rows.add(litlist(structureOrText(key), structureOrText(value)));
    }
  } else
    print("Unknown data type: " + data);
    
  // get table width
  int w = 0;
  for (L<S> row : rows)
    w = max(w, l(row));
    
  // construct HTML for table
  
  new StringBuilder buf;
  buf.append(hopeningtag('table, paramsPlus(tableParams, border := html_valueLessParam())) + "\n");
  
  // title
  if (!noHeader) {
    buf.append("<tr>\n");
    for (S cell : padList(cols, w, ""))
      buf.append("  <th>" + htmlTable2_encodeCell(
        getOrKeep(replaceHeaders, cell), htmlEncode, useBr) + "</th>\n");
    buf.append("</tr>\n");
  }
  
  // data
  for (L<S> row : rows) {
    buf.append(hopeningtag tr(trParams));
    int i = 0;
    for (S cell : padList(row, w, "")) {
      S col = get(cols, i++);
      O[] params = paramsPlus(tdParams, mapGet(paramsByColName, col));
      buf.append("  " + tag('td, htmlTable2_encodeCell(cell, htmlEncode, useBr), params) + "\n");
    }
    buf.append("</tr>\n");
  }
  buf.append("</table>\n");
  ret buf.toString();
}

static S htmlTable2_encodeCell(S cell, boolean useHtmlEncode, boolean useBr) {
  if (htmlTable2_cellEncoder! != null) ret (S) callF(htmlTable2_cellEncoder!, cell);
  if (useHtmlEncode) cell = htmlEncode2(cell);
  if (useBr) cell = nlToBr(cell);
  ret cell;
}

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: 408 / 496
Version history: 16 change(s)
Referenced in: [show references]