简体   繁体   English

ag-grid 列文本截断问题

[英]ag-grid column text truncating issue

Want to render the column width based on the text.想要根据文本呈现列宽。 Few characters are missing after crossing min width(200px) of the column.跨越列的最小宽度(200px)后,很少有字符丢失。

Note: Columns are loading dynamically, so that i can't add width property for every column.注意:列是动态加载的,所以我不能为每一列添加宽度属性。

I am using Ag-Grid Enterprise edition我正在使用 Ag-Grid 企业版

Thank you in-advance.先感谢您。

You can't.你不能。 You have to preset the value of the column's width in the columnDef您必须在columnDef中预设列宽的值

const columnDefs: Array<ColDef | ColGroupDef> = [
  // ...
  {
    // ...
    width: number,
  }
];

You can also have a cellStyle or cellClass property in the column definition.您还可以在列定义中包含cellStylecellClass属性。

You can see the API here: https://github.com/ag-grid/ag-grid/blob/master/packages/ag-grid/src/ts/entities/colDef.ts您可以在此处查看 API: https ://github.com/ag-grid/ag-grid/blob/master/packages/ag-grid/src/ts/entities/colDef.ts

I think you have a couple of options.我认为你有几个选择。

1) Set column widths before rendering. 1)在渲染前设置列宽。 Before the grid is rendered, cycle through your data object for max length of each element that will be displayed in the grid and then set your columnDefs accordingly.在渲染网格之前,循环浏览您的数据对象以获得将在网格中显示的每个元素的最大长度,然后相应地设置您的 columnDefs。 To edit columnDefs, you'll need to make it a variable, not a constant and then you are just editing an array of objects.要编辑 columnDefs,您需要将其设为变量,而不是常量,然后您只需编辑对象数组。 See How to change value of object which is inside an array using JavaScript or jQuery?请参阅如何使用 JavaScript 或 jQuery 更改数组内的对象的值? This approach will slow the initial rendering while calculating the widths.这种方法会在计算宽度时减慢初始渲染速度。

2) Set column widths after rendering. 2)渲染后设置列宽。 After the grid is rendered, use the gridApi.setColumnWidth() method.渲染网格后,使用 gridApi.setColumnWidth() 方法。 https://www.ag-grid.com/javascript-grid-column-api/ https://www.ag-grid.com/javascript-grid-column-api/

onGridReady(params) {
  params.columnApi.setColumnWidth('someColumn', someColumnWidth, true);
}

With this approach, the grid will start rendering a little quicker, but the user might see the column width adjust.使用这种方法,网格将开始渲染得更快一些,但用户可能会看到列宽调整。

Not exactly what you're trying to do, I don't think, but another option is to let the text truncate and have it show on mouse hover:不完全是你想要做的,我不认为,但另一种选择是让文本截断并让它在鼠标悬停时显示:

  agGridColumnDef.cellRenderer = function(params) {
    return "<span title='" + params.value + "'>" + params.value + "</span>";
  };

The answer for future googlers未来谷歌人的答案

<AgGridColumn
  wrapText={true}
  autoHeight={true}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM