我的列宽有问题。我事先不知道它们的宽度,也不想指定每列的宽度。是否有我可以使用的属性根据内容(包括列标题)自动调整所有列宽?
问问题
41600 次
6 回答
12
哇!我想出了一个很酷的答案!将宽度设置"auto"
为真的不适合我。也许是因为我使用的是ng-view
? 没有把握。因此,我创建了 2 个新功能,您可以将其放入controller.js
. 像这样使用它们会给你计算出的列宽!阅读代码注释;有一些警告。
示例用法,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);
});
});
于 2013-09-19T19:58:22.130 回答
0
使用 ui-grid,必须更好:http ://ui-grid.info/docs/#/tutorial/204_column_resizing
但是https://github.com/angular-ui/ng-grid/wiki/Configuration-Options
于 2015-02-25T11:20:32.130 回答
0
这有效:
$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
};
于 2015-05-08T12:21:16.600 回答
0
请参阅https://stackoverflow.com/a/32605748/1688306上的答案
按列名或基准计算的列宽,以较长者为准。
于 2015-09-16T10:51:43.463 回答
-1
是的!您columndefs
可以将宽度设置为auto
,这将根据数据和/或标题的宽度自动调整列的大小。
参考:定义列
于 2013-06-28T15:07:49.573 回答
-1
我正在使用 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>
于 2015-06-15T04:54:24.603 回答