toHtml method

String toHtml({
  1. Iterable<String>? columns,
  2. Map<String, int> fixedPlaces = const {},
})

A conversion of this data frame to an html table.

Implementation

String toHtml({
  Iterable<String>? columns,
  Map<String, int> fixedPlaces = const {},
}) {
  columns = columns ?? [...numericColumns.keys, ...categoricColumns.keys];
  if (columns.any((column) =>
      !numericColumns.containsKey(column) &&
      !categoricColumns.containsKey(column))) {
    throw PackhorseError.badArgument("Bad column name in $columns.");
  }

  final table = toListOfMaps(),
      row = (Iterable<String> cells) =>
          "    <tr><td>${cells.join("</td><td>")}</td></tr>",
      sb = StringBuffer("\n")
        ..writeln('<div class="packhorse">\n<table>\n  <thead>')
        ..writeln("    <tr><th>${columns.join("</th><th>")}</th></tr>")
        ..writeln("  </thead>\n  <tbody>");

  for (final r in table) {
    sb.writeln(row(columns.map((key) => r[key]!
        .replaceAll('"', "")
        .replaceAll("<", "&lt;")
        .replaceAll(">", "&gt;"))));
  }

  sb.writeln("  </tbody>\n</table>\n</div>");

  return sb.toString();
}