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,14 +197,21 @@
QueryResult.prototype.getColumns = function () {
if (this.columns == undefined && this.query_result.data) {
this.columns = _.map(this.query_result.data.columns, function (v) {
return v.name;
});
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.columnNames;
}
QueryResult.prototype.getColumnNameWithoutType = function (column) {
var parts = column.split('::');
@ -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

View File

@ -54,30 +54,46 @@
$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 columnType = columns[i].type;
if (!columnType) {
var rawData = $scope.queryResult.getRawData();
if (rawData.length > 0) {
var exampleData = rawData[0][col];
if (angular.isNumber(exampleData)) {
columnDefinition['formatFunction'] = 'number';
columnDefinition['formatParameter'] = 2;
columnType = 'float';
} 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 (exampleData._i.match(/^\d{4}-\d{2}-\d{2}T/)) {
columnType = 'datetime';
} else {
columnType = 'date';
}
}
}
}
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;