【问题标题】:is there a way to auto adjust widths in ng-grid?有没有办法在 ng-grid 中自动调整宽度?
【发布时间】:2015-09-16 18:51:43
【问题描述】:

我的列宽有问题。我事先不知道它们的宽度,也不想指定每列的宽度。是否有我可以使用的属性根据内容(包括列标题)自动调整所有列宽?

【问题讨论】:

    标签: ng-grid


    【解决方案1】:

    哇!我想出了一个很酷的答案!将宽度设置为"auto" 真的不适合我。也许是因为我使用的是ng-view?没有把握。所以我创建了 2 个新函数,您可以将它们放入您的 controller.js。像这样使用它们会给你计算出的列宽!阅读代码cmets;有一些注意事项。

    示例用法,controllers.js:

    var colDefs = makeColDefs(rows[0]);
    colDefs = autoColWidth(colDefs, rows[0]);
    
    $scope.phones = rows;
    $scope.gridOptions = {
        data : 'phones',
        showFilter : true,
        enableColumnResize : true,
        columnDefs : colDefs
    };
    

    代码,controllers.js:

    /**
     * Create a colDefs array for use with ng-grid "gridOptions". Pass in an object
     * which is a single row of your data!
     */
    function makeColDefs(row) {
        var colDefs = [];
        for ( var colName in row) {
            colDefs.push({
                'field' : colName
            });
        }
        return colDefs;
    }
    
    /**
     * Return a colDefs array for use with ng-grid "gridOptions". Work around for
     * "auto" width not working in ng-grid. colDefs array will have percentage
     * widths added. Pass in an object which is a single row of your data! This
     * function does not do typeface width! Use a fixed width font. Pass in an
     * existing colDefs array and widths will be added!
     */
    function autoColWidth(colDefs, row) {
        var totalChars = 0;
        for ( var colName in row) {
            // Convert numbers to strings here so length will work.
            totalChars += (new String(row[colName])).length;
        }
        colDefs.forEach(function(colDef) {
            var numChars = (new String(row[colDef.field])).length;
            colDef.width = (numChars / totalChars * 100) + "%";
        });
        return colDefs;
    }
    

    Jasmine 测试,controllerSpec.js:

    'use strict';
    
    var ROW = {
        'col1' : 'a',
        'col2' : 2,
        'col3' : 'cat'
    };
    var COLUMN_DEF = [ {
        field : 'col1'
    }, {
        field : 'col2'
    }, {
        field : 'col3'
    } ];
    var COLUMN_DEF2 = [ {
        field : 'col1',
        width : '20%'
    }, {
        field : 'col2',
        width : '20%'
    }, {
        field : 'col3',
        width : '60%'
    } ];
    
    /* jasmine specs for controllers go here */
    
    describe('controllers', function() {
        beforeEach(module('myApp.controllers'));
    
        it('should make an ng-grid columnDef array from a row of data.',
                function() {
                    expect(makeColDefs(ROW)).toEqual(COLUMN_DEF);
                });
    
        it('should return an ng-grid columnDef array with widths!', function() {
            var colDefs = makeColDefs(ROW);
            expect(autoColWidth(colDefs, ROW)).toEqual(COLUMN_DEF2);
        });
    
    });
    

    【讨论】:

    • 请注意,上述解决方案会使 ui-grid 3.0 (ng-grid) 变得非常缓慢和迟缓。
    • 我想知道为什么?我确实报告了一个问题,因为 ng-grid 很慢。
    【解决方案2】:
    【解决方案3】:

    这行得通:

    $scope.gridOptions = {
                    init: function (gridCtrl, gridScope) {
                        gridScope.$on('ngGridEventData', function () {
                            $timeout(function () {
                                angular.forEach(gridScope.columns, function (col) {
                                    gridCtrl.resizeOnData(col);
                                });
                            });
                        });
                    },
                    data: 'scopeDataField',
                    columnDefs: [
                        {
                            field: 'property1',
                            displayName: 'property1title',
                            width: '100px' // a random fixed size, doesn't work without this
                        },
                        {
                            field: 'property2',
                            displayName: 'property2title',
                            width: '100px'
                        }
    
                    ],
                    enableColumnResize : true
                };
    

    【讨论】:

      【解决方案4】:

      https://stackoverflow.com/a/32605748/1688306查看答案

      按列名或基准计算的列宽,以较长者为准。

      【讨论】:

        【解决方案5】:

        是的!在您的columndefs 中,您可以将宽度设置为auto,这将根据数据和/或标题的宽度自动调整列的大小。

        参考:Defining Columns

        【讨论】:

        • 这不起作用。以下是您提供的链接的引用:注意:“auto”目前仅适用于单页应用程序,因为重新调整大小发生在“document.ready”上。仍在努力改进。”
        • 这里有什么奇怪的地方。如果我显示所有列,它会起作用。如果我显示 1 列,则它仅在我的网格位于可滚动面板中并且超出限制时才有效。如果它低于限制并且没有滚动(最少行),那么什么都不会出现!如果我设置一个固定长度,那么它没有问题..但是如果我调整我的窗口大小,它就会成为一个巨大的负担:(
        • 这个auto 宽度设置即使在最新的 ui-grid 中也非常糟糕。它破坏了这么多数据。
        【解决方案6】:

        我正在使用 ui-grid。以下代码对我有用。你可以试试这个。

        First you need to add a module dependency 'ui.grid.autoResize' in your main module.
        
        Then add css class something like this 
        
        .grid {
          width    : 400px !important;
          max-width : 400px !important;
          height   : 600px !important;
        }
        

        最后像这样在你的 html 文件中添加 ui-grid 指令。请记住,不要忘记在您的网格指令中添加“ui-grid-auto-resize”。

        <div id="grid1" ui-grid="gridOptions" class="grid" ui-grid-auto-resize></div>
        

        【讨论】:

        • 这控制网格本身的自动调整大小,OP 询问列自动调整大小
        猜你喜欢
        • 2014-04-05
        • 2017-04-16
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-07
        相关资源
        最近更新 更多