在您的列定义中使用星号:
width:"*";
<div ui-grid="gridOptions" ui-grid-auto-fit-columns></div>
angular.module('app', ['ui.grid', 'ui.grid.autoFitColumns'])
$elm.on('dblclick', function(event, args) {
event.stopPropagation();
var col = uiGridResizeColumnsService.findTargetCol($scope.col, $scope.position, rtlMultiplier);
// Don't resize if it's disabled on this column
if (col.colDef.enableColumnResizing === false) {
return;
}
// Go through the rendered rows and find out the max size for the data in this column
var maxWidth = 0;
var xDiff = 0;
// Get the parent render container element
var renderContainerElm = gridUtil.closestElm($elm, '.ui-grid-render-container');
// Get the cell contents so we measure correctly. For the header cell we have to account for the sort icon and the menu buttons, if present
var cells = renderContainerElm.querySelectorAll('.' + uiGridConstants.COL_CLASS_PREFIX + col.uid + ' .ui-grid-cell-contents');
Array.prototype.forEach.call(cells, function (cell) {
// Get the cell width
// gridUtil.logDebug('width', gridUtil.elementWidth(cell));
// Account for the menu button if it exists
var menuButton;
if (angular.element(cell).parent().hasClass('ui-grid-header-cell')) {
menuButton = angular.element(cell).parent()[0].querySelectorAll('.ui-grid-column-menu-button');
}
gridUtil.fakeElement(cell, {}, function(newElm) {
// Make the element float since it's a div and can expand to fill its container
var e = angular.element(newElm);
e.attr('style', 'float: left');
var width = gridUtil.elementWidth(e);
if (menuButton) {
var menuButtonWidth = gridUtil.elementWidth(menuButton);
width = width + menuButtonWidth;
}
if (width > maxWidth) {
maxWidth = width;
xDiff = maxWidth - width;
}
});
});
// check we're not outside the allowable bounds for this column
col.width = constrainWidth(col, maxWidth);
// All other columns because fixed to their drawn width, if they aren't already
resizeAroundColumn(col);
buildColumnsAndRefresh(xDiff);
uiGridResizeColumnsService.fireColumnSizeChanged(uiGridCtrl.grid, col.colDef, xDiff);
});
所以,当我需要缩小除了一个之外的所有列时,这个方法对我有用。在网格完成加载后,我遍历所有列,并在调整大小器上触发双击事件。这不是最美观的解决方案,因此如果有其他更好的解决方案,我会非常高兴听到。
function resizeColumn(uid) {
// document.querySelector might not be supported; if you have jQuery
// in your project, it might be wise to use that for selecting this
// element instead
var resizer = document.querySelector('.ui-grid-col' + uid + ' .ui-grid-column-resizer.right');
angular.element(resizer).triggerHandler('dblclick');
}
var gridOptions = {
...
onRegisterApi: function (gridApi) {
// why $interval instead of $timeout? beats me, I'm not smart enough
// to figure out why, but $timeout repeated infinitely for me
$interval(function() {
var columns = gridApi.grid.columns;
for(var i = 0; i < columns.length; i++) {
// your conditional here
if(columns[i].field !== 'name') {
resizeColumn(columns[i].uid)
}
}
}, 0, 1);
}
}
width : "*"
,会调整所有可用视口中的列,结果是列被压缩。minWidth: "somevalue"
和 width: "*"
可以使您的 ui-grid 界面看起来更好。<div ui-grid="gridOptions" class="grid" ui-grid-auto-resize></div>
var app = angular.module('app', ['ui.grid', 'ui.grid.autoResize']);
您可以参考ui-grid文档这里.
我没有找到任何实现方法,所以这就是我做的:
迭代前10条记录。
找到每列数据的最大大小。
然后动态创建一个 span
标签,并将其包装在该数据中。
测量标签的宽度,这将是您所需列的近似宽度(可能需要添加一些常量宽度,因为 CSS 的影响等)。
如果前10条记录为 null
,则使用列标题执行相同的过程。
我能想到的最好的解决方案是:
HTML:
ui-grid="gridOptions" class="myuigrid" ui-ui-grid-resize-columns ui-grid-ui-grid-auto-resize></div>
CSS:
.myuigrid {
width: 100%;
}
JS
columnDefs: [
{ name: 'Id', maxWidth:80 },
{ name: 'Name', maxWidth: 280 },
{ name: 'LongName'},
],
我知道这种方法并不适用于所有情况,但大多数情况下,您的列都是固定的,而您预先知道的可变列则是需要调整大小的。
将以下内容添加到gridoptions中
init: function (gridCtrl, gridScope) {
gridScope.$on('ngGridEventData', function () {
$timeout(function () {
angular.forEach(gridScope.columns, function (col) {
gridCtrl.resizeOnData(col);
});
});
});
}
确保每个列定义都有任意宽度的数字
width: 100
没问题。