Is there a way that the column width of jqgrid change dynamically according to the content of that column? I used shrinkToFit and autoWidth but non of them worked for me. I use jqgrid 4.5.2. I have searched and read the other questions but those didn't work for me. Maybe there is a widget to do that. I will be thankful if you help me.
问题:
回答1:
Here's how I implement jqGrid column width based on column content. Add this to the gridComplete event.
$(this).parent().append('<span id="widthTest" />');
gridName = this.id;
$('#gbox_' + gridName + ' .ui-jqgrid-htable,#' + gridName).css('width', 'inherit');
$('#' + gridName).parent().css('width', 'inherit');
var columnNames = $("#" + gridName).jqGrid('getGridParam', 'colModel');
var thisWidth;
// Loop through Cols
for (var itm = 0, itmCount = columnNames.length; itm < itmCount; itm++) {
var curObj = $('[aria-describedby=' + gridName + '_' + columnNames[itm].name + ']');
var thisCell = $('#' + gridName + '_' + columnNames[itm].name + ' div');
$('#widthTest').html(thisCell.text()).css({
'font-family': thisCell.css('font-family'),
'font-size': thisCell.css('font-size'),
'font-weight': thisCell.css('font-weight')
});
var maxWidth = Width = $('#widthTest').width() + 24;
//var maxWidth = 0;
// Loop through Rows
for (var itm2 = 0, itm2Count = curObj.length; itm2 < itm2Count; itm2++) {
var thisCell = $(curObj[itm2]);
$('#widthTest').html(thisCell.html()).css({
'font-family': thisCell.css('font-family'),
'font-size': thisCell.css('font-size'),
'font-weight': thisCell.css('font-weight')
});
thisWidth = $('#widthTest').width();
if (thisWidth > maxWidth) maxWidth = thisWidth;
}
$('#' + gridName + ' .jqgfirstrow td:eq(' + itm + '), #' + gridName + '_' + columnNames[itm].name).width(maxWidth).css('min-width', maxWidth);
}
$('#widthTest').remove();
A working example:
http://jsfiddle.net/Ba5yK/17/
Hope this helps,
Paul
回答2:
This is a good solution, but the column widths jump to the initial width when re-sizing. This is my modified solution as a separate function. Designed to work with shrinkToFit set to false, and fire just once:
function setColumnWidths(gridName) {
if ($('#' + gridName).attr('columnsSet')=='true') return;
$('body').append('<span id="widthTest">WIDTHTEST</span>');
$('#gbox_' + gridName + ' .ui-jqgrid-htable,#' + gridName).css('width', 'inherit');
$('#' + gridName).parent().css('width', 'inherit');
var containerWidth=$('#' + gridName).width() - 20;
var columnNames = $("#" + gridName).jqGrid('getGridParam', 'colModel');
var thisWidth;
// Loop through Cols
for (var itm = 0, itmCount = columnNames.length; itm < itmCount; itm++) {
var curObj = $('[aria-describedby=' + gridName + '_' + columnNames[itm].name + ']');
var thisCell = $('#' + gridName + '_' + columnNames[itm].name + ' div');
$('#widthTest').html(thisCell.text()).css({
'font-family': thisCell.css('font-family'),
'font-size': thisCell.css('font-size'),
'font-weight': thisCell.css('font-weight')
});
var maxWidth = $('#widthTest').width() + 40;
//var maxWidth = 0;
// Loop through Rows
for (var itm2 = 0, itm2Count = curObj.length; itm2 < itm2Count; itm2++) {
var thisCell = $(curObj[itm2]);
$('#widthTest').html(thisCell.html()).css({
'font-family': thisCell.css('font-family'),
'font-size': thisCell.css('font-size'),
'font-weight': thisCell.css('font-weight')
});
thisWidth = $('#widthTest').width();
if (thisWidth > maxWidth) maxWidth = thisWidth;
}
if (maxWidth > containerWidth) maxWidth=containerWidth;
$("#" + gridName).jqGrid('setColProp','amount',{width: maxWidth});
var gw = $("#" + gridName).jqGrid('getGridParam','width');
$("#" + gridName).jqGrid('setGridWidth',gw,true);
$('#' + gridName + ' .jqgfirstrow td:eq(' + itm + '), #' + gridName + '_' + columnNames[itm].name).width(maxWidth).css('min-width', maxWidth);
}
$('#widthTest').remove();
$('#' + gridName).attr('columnsSet','true');
}
回答3:
I am not sure this answered earlier or not but below is solution I implemented which is working one. Just provide column width in percentage and summation of all column should reach to 100%. But I am not sure how to deal with adding/removing columns at runtime.
I am working on that as well and will update all.
columns: [
{ text: 'CheckBox', datafield: 'ProjectSubstantialPOUADetailsID', width: '10%' },
{ text: 'Area/Phase Substantial', datafield: 'SubstantialPOUA', width: '30%' },
{ text: 'Type', datafield: 'SubstantialTypeName', width: '15%' },
{ text: 'Achieved', datafield: 'DateAchieved', width: '15%', cellsformat: 'd', formatter: "date", formatoptions: { srcformat: "ISO8601Long", newformat: "m/d/Y" } },
{ text: 'PL Final', datafield: 'DateFinal', width: '15%', cellsformat: 'd' },
{ text: 'Warranty?', datafield: 'Warranty', width: '15%', columntype: 'checkbox' }
]
Thanks Rushi
回答4:
pphillips001 answer works for me but resizing is to slow when the grid grows. I just updated the logic and the performance is very good now.
function setColumnWidths(gridName) {
if ($('#' + gridName).attr('columnsSet')=='true') return;
$('body').append('<span id="widthTest">WIDTHTEST</span>');
$('#gbox_' + gridName + ' .ui-jqgrid-htable,#' + gridName).css('width', 'inherit');
$('#' + gridName).parent().css('width', 'inherit');
var containerWidth=$('#' + gridName).width() - 20;
var columnNames = $("#" + gridName).jqGrid('getGridParam', 'colModel');
var thisWidth = 0;
// Loop through Cols
for (var itm = 0, itmCount = columnNames.length; itm < itmCount; itm++) {
var curObj = $('[aria-describedby=' + gridName + '_' + columnNames[itm].name + ']');
var thisCell = $('#' + gridName + '_' + columnNames[itm].name + ' div');
$('#widthTest').html(thisCell.text()).css({
'font-family': thisCell.css('font-family'),
'font-size': thisCell.css('font-size'),
'font-weight': thisCell.css('font-weight')
});
var maxWidth = $('#widthTest').width() + 40;
//var maxWidth = 0;
// Loop through Rows
var maxText = 0;
var theCell;
for (var itm2 = 0, itm2Count = curObj.length; itm2 < itm2Count; itm2++) {
thisCell = $(curObj[itm2]);
if (maxText < thisCell.text().length) {
maxText = thisCell.text().length;
theCell = thisCell;
}
}
if (theCell !== undefined) {
$('#widthTest').html(theCell.html()).css({
'font-family': theCell.css('font-family'),
'font-size': theCell.css('font-size'),
'font-weight': theCell.css('font-weight')
});
thisWidth = $('#widthTest').width() + 5;
}
if (thisWidth > maxWidth) maxWidth = thisWidth;
if (maxWidth > containerWidth) maxWidth=containerWidth;
$("#" + gridName).jqGrid('setColProp','amount',{width: maxWidth});
var gw = $("#" + gridName).jqGrid('getGridParam','width');
$("#" + gridName).jqGrid('setGridWidth',gw,true);
$('#' + gridName + ' .jqgfirstrow td:eq(' + itm + '), #' + gridName + '_' + columnNames[itm].name).width(maxWidth).css('min-width', maxWidth);
}
$('#widthTest').remove();
$('#' + gridName).attr('columnsSet','true');
}
回答5:
colModel: [ { name: "f_name",index:"f_name",width:'100%'} ]