jqgrid列宽自动

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jqgrid列宽自动相关的知识,希望对你有一定的参考价值。

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

答案

这是我如何根据列内容实现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/

希望这可以帮助,

保罗

另一答案

这是一个很好的解决方案,但在重新调整大小时,列宽会跳转到初始宽度。这是我修改后的解决方案作为一个单独的功能设计用于将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');
}
另一答案

我不确定这个问题是否已提前回答,但下面是我实施的解决方案。只提供百分比的列宽,所有列的总和应达到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' }
     ]

谢谢Rushi

另一答案

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

}

另一答案

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

以上是关于jqgrid列宽自动的主要内容,如果未能解决你的问题,请参考以下文章

ASP.net MVC 代码片段问题中的 Jqgrid 实现

jqgrid 最近在用jqgrid,我想要实现列的拖拽功能,请问有人实现过吗

jqGrid属性介绍

Jqgrid + JQuery 自动完成多输入

jqGrid filterToolbar():无法读取未定义的属性“length”

fastreport中列宽固定 根据字段内容多少自动调整行高