Jqgird 自动列表

function setColumnWidths(gridName) {
    if ($('#' + gridName).attr('columnsSet')=='true') return;

    $('body').append('WIDTHTEST');
    $('#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');

你可能感兴趣的:(javascript,前端,开发语言)