JSFiddle - React, Tailwind, and code Playground

by Abhishek Kumar

JavaScript

var tabulate = function(o) {
  var template = {
    table: '<table class="[style]"><thead><tr>[head]</tr></thead><tbody>[body]</tbody></table>'
  };

  var style = {
    table: "mdl-data-table mdl-js-data-table mdl-shadow--2dp",
    col: "mdl-data-table__cell--non-numeric"
  };

  var isNonNumeric = function(s) {
    return /^[0-9]+/.test(s);
  };

  var head = '',
    body = '';

  for (var h in o[0]) {
    head += enzinak.Templator.fixIn('<th class="[cell]">[value]</th>', {
      cell: (isNonNumeric(o[0][h])) ? style.col : '',
      value: h
    });
  }

  for (var i = 0; i < o.length; i++) {
    var list = '';
    for (var j in o[i]) {
      list += enzinak.Templator.fixIn('<td class="[cell]">[value]</td>', {
        cell: (isNonNumeric(o[i][j])) ? style.col : '',
        value: o[i][j]
      });
    }
    body += enzinak.Templator.fixIn('<tr>[row]</tr>', {
      row: list
    });
  }

  return enzinak.Templator.fixIn(template.table, {
    style: style.table,
    head: head,
    body: body
  });
};