From 52f44588e6f44b10a8a6bf0315d7c43e5f44ca1c Mon Sep 17 00:00:00 2001 From: Arik Fraimovich Date: Mon, 5 May 2014 19:44:52 +0300 Subject: [PATCH] Use column type (if available) to better render tables. --- rd_ui/app/scripts/services/resources.js | 19 ++++++---- rd_ui/app/scripts/visualizations/table.js | 44 +++++++++++++++-------- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/rd_ui/app/scripts/services/resources.js b/rd_ui/app/scripts/services/resources.js index 49d6556f..91064000 100644 --- a/rd_ui/app/scripts/services/resources.js +++ b/rd_ui/app/scripts/services/resources.js @@ -197,15 +197,22 @@ QueryResult.prototype.getColumns = function () { if (this.columns == undefined && this.query_result.data) { - this.columns = _.map(this.query_result.data.columns, function (v) { + this.columns = this.query_result.data.columns; + } + + return this.columns; + } + + QueryResult.prototype.getColumnNames = function () { + if (this.columnNames == undefined && this.query_result.data) { + this.columnNames = _.map(this.query_result.data.columns, function (v) { return v.name; }); } - return this.columns; + return this.columnNames; } - QueryResult.prototype.getColumnNameWithoutType = function (column) { var parts = column.split('::'); return parts[0]; @@ -239,13 +246,13 @@ } QueryResult.prototype.getColumnCleanNames = function () { - return _.map(this.getColumns(), function (col) { + return _.map(this.getColumnNames(), function (col) { return this.getColumnCleanName(col); }, this); } QueryResult.prototype.getColumnFriendlyNames = function () { - return _.map(this.getColumns(), function (col) { + return _.map(this.getColumnNames(), function (col) { return this.getColumnFriendlyName(col); }, this); } @@ -261,7 +268,7 @@ QueryResult.prototype.prepareFilters = function () { var filters = []; var filterTypes = ['filter', 'multi-filter']; - _.each(this.getColumns(), function (col) { + _.each(this.getColumnNames(), function (col) { var type = col.split('::')[1] if (_.contains(filterTypes, type)) { // filter found diff --git a/rd_ui/app/scripts/visualizations/table.js b/rd_ui/app/scripts/visualizations/table.js index af3ec536..962c1022 100644 --- a/rd_ui/app/scripts/visualizations/table.js +++ b/rd_ui/app/scripts/visualizations/table.js @@ -54,32 +54,48 @@ $scope.gridData = prepareGridData($scope.queryResult.getData()); + var columns = $scope.queryResult.getColumns(); $scope.gridColumns = _.map($scope.queryResult.getColumnCleanNames(), function (col, i) { var columnDefinition = { 'label': $scope.queryResult.getColumnFriendlyNames()[i], 'map': col }; - var rawData = $scope.queryResult.getRawData(); + var columnType = columns[i].type; - if (rawData.length > 0) { - var exampleData = rawData[0][col]; - if (angular.isNumber(exampleData)) { - columnDefinition['formatFunction'] = 'number'; - columnDefinition['formatParameter'] = 2; - } else if (moment.isMoment(exampleData)) { - columnDefinition['formatFunction'] = function (value) { - // TODO: this is very hackish way to determine if we need - // to show the value as a time or date only. Better solution - // is to complete #70 and use the information it returns. - if (value._i.match(/^\d{4}-\d{2}-\d{2}T/)) { - return value.format("DD/MM/YY HH:mm"); + if (!columnType) { + var rawData = $scope.queryResult.getRawData(); + + if (rawData.length > 0) { + var exampleData = rawData[0][col]; + if (angular.isNumber(exampleData)) { + columnType = 'float'; + } else if (moment.isMoment(exampleData)) { + if (exampleData._i.match(/^\d{4}-\d{2}-\d{2}T/)) { + columnType = 'datetime'; + } else { + columnType = 'date'; } - return value.format("DD/MM/YY"); } } } + if (columnType === 'integer') { + columnDefinition.formatFunction = 'number'; + columnDefinition.formatParameter = 0; + } else if (columnType === 'float') { + columnDefinition.formatFunction = 'number'; + columnDefinition.formatParameter = 2; + } else if (columnType === 'date') { + columnDefinition.formatFunction = function (value) { + return value.format("DD/MM/YY"); + }; + } else if (columnType === 'datetime') { + columnDefinition.formatFunction = function (value) { + return value.format("DD/MM/YY HH:mm"); + }; + } + return columnDefinition; }); }