Autosize Column Widths
Is there are way to have a column automatically set its width based on the width of the data?
T Regal
March 17,
<style id='gridStyle'>
var colSizes = new Array(myColumns.length) // array for colwidths
for (h=0;h<myColumns.length;h++){ // intialize with header widths
colSizes[h]= myColumns[h].length;
}
//get data function sets the col width for th
//data column if it is greater than the stored
//width
function getData(i, j){
chars = myData[i][j].length;
if ( chars > colSizes[j]) {
if (chars<70) { //70 char max to stop really wide cols.
colSizes[j] = chars;
} else {
colSizes[j] = 70;
}
}
return myData[i][j]
}
grid.setProperty("data/text", getData);
document.write(grid);
var ssheet = document.getElementById("gridStyle").styleSheet;
for( cs=0;cs<myColumns.length-1;cs++) {
ssheet.addRule(".active-column-"+cs, "width:"+((colSizes[cs]*6)+20)+'px');
}
grid.getTemplate("layout").action("adjustSize");
var columnA = new Active.Templates.Text;
columnA.setStyle('width', newSize+'px');
gridasGridObj.setTemplate('column', columnA, ndx);
while( document.getElementById( tableid +".data.item:"+ row + ".item:" + ndx ) ){
arr[row] = document.getElementById( tableid +".data.item:"+ row + ".item:" + ndx );
row++;
}
for(var i=0; i<colArr.length; i++){
colArr[i].style.width = "auto";
var imax = colArr[i].offsetWidth;
colArr[i].style.width = "";
maxx = (typeof(imax)=="number" && imax>maxx) ? imax : maxx;
}
var nodeX = document.body.appendChild(nX);
for(var i=0; i<colArr.length; i++){
nodeX.innerHTML = colArr[i].innerHTML;
var imax = nodeX.offsetWidth;
maxx = (typeof(imax)=="number" && imax>maxx) ? imax : maxx;
}
document.body.removeChild(nodeX);
var stylesheet = document.styleSheets[document.styleSheets.length-1];
stylesheet.addRule(".active-column-2", "width:150px");
var header = obj.getTemplate("top/item");
header.getContent("div").setEvent("onclick", function(){
alert(1);
});
var row = new Active.Templates.Row;
row.setEvent("ondblclick", function(){this.action("_DoubleClick");}); obj.setAction("_DoubleClick", rowDoubleClick);
row.setEvent("oncontextmenu", rowRightClick);
obj.setTemplate("row", row);
obj.setAction("click", rowClick);
function rowClick (src) {
alert('Single Click on: '+src.getProperty("item/index"));
}
function rowRightClick (src) {
showPopup(event);
}
function rowDoubleClick (src) {
alert('Double Click on: '+src.getProperty("item/index"));
}
// dynamic column width
$records = mysql_numrows($data);
for($i=0; $i < $records; $i++) {
$row = mysql_fetch_row($data);
$length = mysql_fetch_lengths($data);
foreach($length as $key => $value) {if($maxlength[$key] < $value) {$maxlength[$key] = $value;} }
}
mysql_data_seek($data,0);
$min_col_length = 30;
print "<style>";
foreach($maxlength as $key => $value) {if($value * 10 < $min_col_length) {print".active-column-$key {width:" . ($min_col_length) . "px}";} else {print".active-column-$key {width:" . ($value * 10) . "px}";}}
print "</style>";
This topic is archived.
ActiveWidgets is a javascript library for creating user interfaces. It offers excellent performance for complex screens while staying simple, compact and easy to learn. Deployed by thousands of commercial customers in more than 70 countries worldwide.
Copyright © ActiveWidgets 2021