All Downloads are FREE. Search and download functionalities are using the official Maven repository.

static.js.druidTable-0.0.1.js Maven / Gradle / Ivy

The newest version!
var DruidTable = function() {
  var _private = {
    data: []
  };

  var _public = {
    getRows: function() {
      return _private.data;
    },

    getRow: function(row) {
      return _private.data[row];
    },

    getNumRows: function() {
      return _private.data.length;
    },

    getCol: function(colName) {
      return _.map(_private.data, function(dataRow) {
        return dataRow[colName];
      });
    },

    getColTotal: function(colName) {
      var total = 0;

      _.each(this.getCol(colName), function(num) {
        total += 0 + num;
      });
      return total;
    },

    getCell: function(row, col) {
      return _private.data[row][col];
    },

    setCell: function(row, col, val) {
      _private.data[row] = _private.data[row] || {};
      _private.data[row][col] = val;
    },

    addColumn: function(colName, colVals) {
      var i = 0;

      _.each(_private.data, function(row) {
        row[colName] = colVals[i];
        i++;
      })
    },

    toHTMLTable: function(el) {
      var html = "";

      // build table header
      html += "";

      // find all unique field names
      var fieldNames = {};
      for (var row in this.getRows()) {
        for (var field in this.getRow(row)) {
          fieldNames[field] = 1;
        }
      }

      // build table header filters
      html += "";
      for (var field in fieldNames) {
        html += "";
      }
      html += "";

      // build table header column headings
      html += "";
      for (var field in fieldNames) {
        html += "" + field + "";
      }
      html += "";
      html += "";

      // build table body
      html += "";
      for (var r in this.getRows()) {
        html += "";
        for (var field in fieldNames) {
          var row = this.getRow(r);
          if (row.hasOwnProperty(field)) {
            html += "" + this.getCell(r, field) + "";
          } else {
            html += "";
          }
        }
        html += "";
      }
      html += "";

      el.html(html);
    }
  };

  return _public;
};




© 2015 - 2025 Weber Informatics LLC | Privacy Policy