【发布时间】:2015-09-16 18:51:43
【问题描述】:
我的列宽有问题。我事先不知道它们的宽度,也不想指定每列的宽度。是否有我可以使用的属性根据内容(包括列标题)自动调整所有列宽?
【问题讨论】:
标签: ng-grid
我的列宽有问题。我事先不知道它们的宽度,也不想指定每列的宽度。是否有我可以使用的属性根据内容(包括列标题)自动调整所有列宽?
【问题讨论】:
标签: ng-grid
哇!我想出了一个很酷的答案!将宽度设置为"auto" 真的不适合我。也许是因为我使用的是ng-view?没有把握。所以我创建了 2 个新函数,您可以将它们放入您的 controller.js。像这样使用它们会给你计算出的列宽!阅读代码cmets;有一些注意事项。
var colDefs = makeColDefs(rows[0]);
colDefs = autoColWidth(colDefs, rows[0]);
$scope.phones = rows;
$scope.gridOptions = {
data : 'phones',
showFilter : true,
enableColumnResize : true,
columnDefs : colDefs
};
/**
* 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;
}
'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);
});
});
【讨论】:
这行得通:
$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
};
【讨论】:
在https://stackoverflow.com/a/32605748/1688306查看答案
按列名或基准计算的列宽,以较长者为准。
【讨论】:
是的!在您的columndefs 中,您可以将宽度设置为auto,这将根据数据和/或标题的宽度自动调整列的大小。
【讨论】:
auto 宽度设置即使在最新的 ui-grid 中也非常糟糕。它破坏了这么多数据。
我正在使用 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>
【讨论】: