`

pentaho ctools cde 插件BTable format

 
阅读更多

问题:pentaho ctools cde 插件BTable 对维度进行格式化,这个需求很少见,因为维度一般都是字符串,但遇到维度是浮点数值型的时候,就需要对维度进行格式化 

 

修改BTable插件的代码如下:

 

BTable 使用的Table

%.2f  

 

bt.table.js

191行代码,添加对维度值的类型colType设置

		$.each(normalizedJson.metadata, function(i, v) {
			var colName = v.colName;
			if(colName.indexOf("]/[") > 0 || $.inArray(colName, measureQualifiedNames) >= 0) {
				v.colType = "numeric";
			} else {
				if(colName != "[Measures].[MeasuresLevel]" && $.inArray(colName, dimensionQualifiedNames) < 0 && $.inArray(colName, pivotDimensionQualifiedNames) < 0)
					v.colType = "numeric";
				else {
					if(v.colType === 'Numeric')
						v.colType = "numeric";
					else
						v.colType = "string";
				}
			}
		});

 

 

 

BTableComponent.js

 

579行插入如下代码,添加对维度值的格式colFormat设置

	/* 
	 * Callback for when the table is finished drawing. Called every time there
	 * is a redraw event (so not only updates, but also pagination and sorting).
	 * We handle addIns and such things in here.
	 */
	fnDrawCallback: function(dataTableSettings) {
		var dataTable = dataTableSettings.oInstance,
		cd = this.chartDefinition,
		myself = this;

		this.timer.check("Table drawn without alarms and spans");

		var measuresLevelColIdx = $.inArray("[Measures].[MeasuresLevel]", cd.colHeaders);
		var formatStrings = myself.bTable.olapCube.getFormatStrings();
		var cellFormats = [];
		if(measuresLevelColIdx < 0) {
			$.each(myself.rawData.metadata, function(i, v) {
				if(v.colType == "numeric" && v.colName.indexOf("[Measures].[") >= 0) {
					if (!v.colIsCalculated) {
						var start_pos = v.colName.indexOf("[Measures].");
						if(start_pos >= 0) {
							var end_pos = v.colName.indexOf("]", start_pos+10);
							var measureName = v.colName.substring(start_pos,end_pos+1);
							cellFormats.push(formatStrings[measureName]);
						}
					} else {
						cellFormats.push(v.colFormat);
					}
				} else {
					if(v.colType == "numeric" && v.colFormat !== undefined) {
						cellFormats.push(v.colFormat);
					} else {
						cellFormats.push("");
					}
				}
			});
		}

 

 

898行插入如下代码,添加对维度值的格式化

					if(coltype == "numeric") {
						oRows.each(function(i, v) {
							var aData = oTable.fnGetData(this);
							var value = aData[index] == "" ? 0 : parseFloat(aData[index]);
			                if (format && (typeof value != "undefined" && value !== null)) {
			                	$('td:eq(' + index + ')',this).text(bt.utils.getLocalizedFormattedValue(format,value));
			                }
						});
					}

 

 

最后

在BTable 的 Post Fetch 

 

function f(data) {

    // BTable/resources/amd-components/BTable/lib/bt.utils.js 389行
    //			case "": return valueString;
	//			case "none": return valueString;
	//			case "general number": formatString = "0.00"; break;
	//			case "currency": formatString = "#,##0.00"; break;
	//			case "fixed":  formatString = "0,.00####"; break;
	//			case "standard": formatString = "#,##0"; break;
	//			case "percent": formatString = "0.00%"; break;
	//			case "scientific": formatString = "0.00e+00"; break;
	//			case "yes/no": return testZeroValue(valueString) ? "No" : "Yes";
	//			case "true/false": return testZeroValue(valueString) ? "False" : "True";
	//			case "on/off": return testZeroValue(valueString) ? "Off" : "On";

    // data.metadata[3].colType = 'Numeric'
    // data.metadata[3].colFormat = 'Currency'
    data.metadata[7].colType = 'Numeric'
    data.metadata[7].colFormat = 'general number'

    return data;

} 
 

 

 

...

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics