Textを整形して出力するもの
ほぼ確実に没るので投稿。
ブラウザにはテーブルがあるから需要が無いだろう・・。
ソース
TextTableLayoutUtil = function() { this.cols = new Array(); this.cells = new Array(); } TextTableLayoutUtil.prototype = { AddCol : function(colName, colLength) { this.cols.push({name : colName, colLength : colLength, emptyStr : this.getEmptyString(colLength)}); this.cells[this.cols.length-1] = new Array(); if (this.cells[0].length != this.cells[this.cols.length-1].length) { for (var i=0; i<this.cells[0].length; i++) { this.cells[this.cols.length-1].push(""); } } }, getEmptyString : function(length) { // AS Version var s=""; for (var i=0; i<length; i++) { s+=" "; } return s; /* // JS Version var ary = new Array(length+1) return ary.join(" "); */ }, AddRow : function(row) { for (var i=0; i<this.cols.length; i++) { this.cells[i].push((row[i] ? row[i] : "")); } }, Output : function() { var out=""; var rows = new Array(this.cols.length+1); var i, j, k,l=0; for (i=0; i<this.cols.length; i++) { out+=this.cols[i].name+this.cols[i].emptyStr.substring(0, this.cols[i].colLength-this.cols[i].name.length); } rows[l++]=out; for (k=0; k<this.cells[0].length; k++) { out=""; for (i=0; i<this.cols.length; i++) { out += this.cells[i][k]+this.cols[i].emptyStr.substring(0, this.cols[i].colLength-this.cells[i][k].length); } rows[l++]=out; } return rows.join("\n"); } }
一応解説
一応ActionScript向け。JavaScriptで使う場合は出来れば JS Versionと書かれているところのコメントアウトを外し、AS Versionと書かれているところをコメントアウトしてほしい。
無駄にパフォーマンス測定に力を入れたので、Arrayのjoinとか使いまくり。なんか弊害があるかも(空の行とかね)。
Getterすら用意されて無いので、使い捨てで。
かなり適当。