jqgrid列宽自动

时间:2014-02-19 19:04:29

标签: jqgrid jqgrid-formatter

是否有一种方法可以根据该列的内容动态更改jqgrid的列宽?我使用了shrinkToFit和autoWidth但是没有为我工作。我使用jqgrid 4.5.2。我搜索并阅读了其他问题,但那些对我不起作用。也许有一个小部件可以做到这一点。如果你帮助我,我将感激不尽。

5 个答案:

答案 0 :(得分:0)

以下是基于列内容实现jqGrid列宽的方法。将其添加到gridComplete事件。

$(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();

一个工作示例:

http://jsfiddle.net/Ba5yK/17/

希望这有帮助,

答案 1 :(得分:0)

这是一个很好的解决方案,但在重新调整大小时,列宽会跳转到初始宽度。 这是我修改后的解决方案作为一个单独的功能设计用于将shrinkToFit设置为false,并仅触发一次:

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');
}

答案 2 :(得分:0)

我不确定这个问题是否已提前回答,但下面是我实施的解决方案。只提供百分比的列宽,所有列的总和应达到100%。但我不知道如何处理在运行时添加/删除列。

我也在努力,并将更新所有。

 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' }
     ]

由于 如是

答案 3 :(得分:0)

pphillips001答案对我有用,但是当网格扩大时,调整大小会减慢速度。我刚刚更新了逻辑,现在性能非常好。

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');

}

答案 4 :(得分:-1)

colModel:[{name:“f_name”,index:“f_name”, width:'100%'}]