Use column type (if available) to better render tables.

This commit is contained in:
Arik Fraimovich 2014-05-05 19:44:52 +03:00
parent 0ffda9d002
commit 52f44588e6
2 changed files with 43 additions and 20 deletions

View File

@ -197,15 +197,22 @@
QueryResult.prototype.getColumns = function () { QueryResult.prototype.getColumns = function () {
if (this.columns == undefined && this.query_result.data) { 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 v.name;
}); });
} }
return this.columns; return this.columnNames;
} }
QueryResult.prototype.getColumnNameWithoutType = function (column) { QueryResult.prototype.getColumnNameWithoutType = function (column) {
var parts = column.split('::'); var parts = column.split('::');
return parts[0]; return parts[0];
@ -239,13 +246,13 @@
} }
QueryResult.prototype.getColumnCleanNames = function () { QueryResult.prototype.getColumnCleanNames = function () {
return _.map(this.getColumns(), function (col) { return _.map(this.getColumnNames(), function (col) {
return this.getColumnCleanName(col); return this.getColumnCleanName(col);
}, this); }, this);
} }
QueryResult.prototype.getColumnFriendlyNames = function () { QueryResult.prototype.getColumnFriendlyNames = function () {
return _.map(this.getColumns(), function (col) { return _.map(this.getColumnNames(), function (col) {
return this.getColumnFriendlyName(col); return this.getColumnFriendlyName(col);
}, this); }, this);
} }
@ -261,7 +268,7 @@
QueryResult.prototype.prepareFilters = function () { QueryResult.prototype.prepareFilters = function () {
var filters = []; var filters = [];
var filterTypes = ['filter', 'multi-filter']; var filterTypes = ['filter', 'multi-filter'];
_.each(this.getColumns(), function (col) { _.each(this.getColumnNames(), function (col) {
var type = col.split('::')[1] var type = col.split('::')[1]
if (_.contains(filterTypes, type)) { if (_.contains(filterTypes, type)) {
// filter found // filter found

View File

@ -54,32 +54,48 @@
$scope.gridData = prepareGridData($scope.queryResult.getData()); $scope.gridData = prepareGridData($scope.queryResult.getData());
var columns = $scope.queryResult.getColumns();
$scope.gridColumns = _.map($scope.queryResult.getColumnCleanNames(), function (col, i) { $scope.gridColumns = _.map($scope.queryResult.getColumnCleanNames(), function (col, i) {
var columnDefinition = { var columnDefinition = {
'label': $scope.queryResult.getColumnFriendlyNames()[i], 'label': $scope.queryResult.getColumnFriendlyNames()[i],
'map': col 'map': col
}; };
var rawData = $scope.queryResult.getRawData(); var columnType = columns[i].type;
if (rawData.length > 0) { if (!columnType) {
var exampleData = rawData[0][col]; var rawData = $scope.queryResult.getRawData();
if (angular.isNumber(exampleData)) {
columnDefinition['formatFunction'] = 'number'; if (rawData.length > 0) {
columnDefinition['formatParameter'] = 2; var exampleData = rawData[0][col];
} else if (moment.isMoment(exampleData)) { if (angular.isNumber(exampleData)) {
columnDefinition['formatFunction'] = function (value) { columnType = 'float';
// TODO: this is very hackish way to determine if we need } else if (moment.isMoment(exampleData)) {
// to show the value as a time or date only. Better solution if (exampleData._i.match(/^\d{4}-\d{2}-\d{2}T/)) {
// is to complete #70 and use the information it returns. columnType = 'datetime';
if (value._i.match(/^\d{4}-\d{2}-\d{2}T/)) { } else {
return value.format("DD/MM/YY HH:mm"); 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; return columnDefinition;
}); });
} }