Forum


08:49

Hello,
I'm using jqgrid 3.5.2 with shrinkToFit=false and I would like to keep it that way. My jqgrid is as wide as the webpage, and when the grid has a lot of columns, I get a horizontal scrollbar, which is what I want. My jqgrid resizes when I resize the window, and my column widths stay the same, which is also what I want.
Now the problem is that when I have very few columns, let's say 4, my grid is a lot wider than the total width of the 4 columns and it doesn't look very nice. I would like the columns to be stretched to fit the grid width ONLY when their total width < grid width. Is this possible, and if so how?
Thanks in advance!
02:47

Moderators
30/10/2007

Hello,
Maybe you need to use a setGridWidth method. Also before this you will need to determine the width of the visible columns.
You can do this using getGridParam method something like:
var model = $("#mygrid").getGridParam("colModel");
var visiblewidth = 0;
$.each(model,function(){
if (!this.hidden) visiblewidth += pareseInt(this.width);
});
Hope this help.
Regards
Tony
For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.
03:21

Thanks for the reply. I tried your code, but it makes my grid as wide as the sum of the columns, but I want the opposite, i want the grid width to stay the same, but each column should stretch to "fill" the grid.
I'm not very familiar with jQuery, so I tried out some things in regular Javascript:
var header = document.getElementsByTagName("th");
var totaal = 0;
for(i=0; i <header.length; i++){
var width = header[i].style.width;
totaal += parseInt(width.substr(0,width.length-2));
}
var gridWidth = $('#mailinfoGrid').getGridParam("width");
if(totaal < gridWidth){
for(i=0; i < header.length; i++){
header[i].style.width = Math.floor(gridWidth/header.length) + 'px';
}
var cells = document.getElementById("gview_mailinfoGrid").getElementsByTagName("td");
for(i=0; i < cells.length; i++){
cells[i].style.width = Math.floor(gridWidth/header.length) + 'px';
}
}
This code does what I want, but when you then try to resize the columns, the resize "drag line" is in the wrong location.
So based on your example I tried the following code to change each column width to a static value, but the column widths aren't changed?
if(totaal < gridWidth){
$.each(model,function(){
if (!this.hidden) {
this.width = 200;
}
});
}
04:00

Moderators
30/10/2007

No need to do this just call setGridWidth method with the second parameter set to true. See docs
For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.
04:26

I tried that, and because the width of my grid has to stay the same, I just have to get the gridWidth and then set it again, but with shrinkToFit=true:
var gridWidth = $('#mailinfoGrid').getGridParam("width");
$('#mailinfoGrid').setGridWidth(gridWidth,true);
But this doesn't stretch the columns 🙁 it does nothing! It's really weird, but I think it's because shrinkToFit is initially set to false on construction of my jqgrid
04:31

Moderators
30/10/2007

var gridWidth = parseInt($('#mailinfoGrid').getGridParam(”width”));
$('#mailinfoGrid').setGridWidth(gridWidth+0.01,true);
The grid does not change if you try to set the same width (A bug wich will be corrected soon);
For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.
04:32

I tried something else, the only difference is that I get the old grid width and I set the new grid width to the old value -1. This does stretch the columns. I think because I set the width to the same width it had before, the columns didn't stretch. Weird, but it works now!
var gridWidth = $('#mailinfoGrid').getGridParam("width");
$('#mailinfoGrid').setGridWidth(gridWidth-1,true);
04:33

tony said:
var gridWidth = parseInt($('#mailinfoGrid').getGridParam(”width”));
$('#mailinfoGrid').setGridWidth(gridWidth+0.01,true);The grid does not change if you try to set the same width (A bug wich will be corrected soon);
Lol, I just discovered it when you posted the solution! Thanks a lot for your help!
03:53

Moderators
30/10/2007

Hello,
Will try to fix it, because of the new method gridResize.
Best Regards
Tony
For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.
Most Users Ever Online: 715
Currently Online:
83 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
OlegK: 1255
markw65: 179
kobruleht: 144
phicarre: 132
YamilBracho: 124
Renso: 118
Member Stats:
Guest Posters: 447
Members: 11373
Moderators: 2
Admins: 1
Forum Stats:
Groups: 1
Forums: 8
Topics: 10592
Posts: 31289
Newest Members:
, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66