My ultimate goal is to have jqgrid to auto-set the column width according to the data content. As part of the path to go there, I need to be able to set the column width AFTER the data is loaded by which time I would know the maximum width of each column. But once I know the maximum width of a column, how can I set the width of each column in the "loadComplete" event and have the grid refresh with the new width of each column? Most of the posts I've found on the net are regarding the grid's overall width. What I want is to set each individual column's width and have the horizontal scrollbar appear automatically if the total width is too long.
Update: After seeing Oleg's awesome demo, I ended up coding this in the dqGrid (4.5.4) itself. Here's what I did:
Locate the function
addJSONData = function(data,t, rcnt, more, adjust) {
then locate within this function
for (j=0;j<rowReader.length;j++) {
v = $.jgrid.getAccessor(cur,rowReader[j]);
rd[ts.p.colModel[j+gi+si+ni].name] = v;
rowData.push(addCell(idr, v, j + gi + si + ni, i + rcnt, cur, rd));
// my addtion: stores the largest header size
var newWidth = v.length * 6;
if (ts.grid.headers[j].width < newWidth) {
ts.grid.headers[j].width = newWidth;
}
}
Then right before the end } of this function, add the following
// my addition: invoke the resizing logic
for (j = 0; j < rowReader.length; j++) {
ts.grid.resizing = { idx: j };
ts.grid.dragEnd();
}
After that, the grid will adjust the width of each column according to the content. One thing I still need help on is how to calculate preciously the new width. My current hard-coded calculation
var newWidth = v.length * 6;
is obviously not very scalable.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…