自动调整 DataGridCoulmn/AdvancedDataGridColumn 的宽度以适合内容

发布于 2024-09-02 13:22:57 字数 205 浏览 3 评论 0原文

我希望 DataGridColumn 或 AdvancedDataGridColumn 能够自动调整其宽度,以适应其中的内容。

我是 Flex 新手。我想实现一些类似 HTML 表格的东西。

呈现的数据是简单的文本。有些行的文本稍长,在这种情况下我想自动扩展 DataGridColumn 的宽度。

任何帮助解决这个问题。

提前致谢

I want that the DataGridColumn or AdvancedDataGridColumn would automatically resize it's width so as to fit the content within..

I'm new to flex. I want to implement something like HTML tables.

The Data Rendered is simple Text. Some Rows have little longer Text, in that case I would like to automatically extend the width of DataGridColumn.

Any help to solve this.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫竹語嫣☆ 2024-09-09 13:22:57

您已经遇到了 DataGrid 和 AdvancedDataGrid 的最大问题之一。我非常讨厌让单元格内容看起来舒适是多么困难。由于一些不太明显的原因,狭窄的字段值将出现在非常宽的单元格中,而宽的内容和标题将被压缩。

由于某种原因,对于第一列和最后一列尤其如此。

我找到的唯一解决方案是在列上设置 minWidth 属性,并且我必须首先浏览数据以找到这些列中最宽的异常值并确保它们适合。另一个有用的解决方案是在左侧和右侧设置虚拟列,这些虚拟列的宽度以及 minWidths 和 maxWidths 的尺寸非常小,例如 5,这似乎可以让中间的真实列更好地“呼吸”。

<mx:columns>
  <mx:DataGridColumn id="leftDummy" width="5" minWidth="5" maxWidth="5"/>
  <!-- Your "real" columns here, with minWidth assignments -->
  <mx:DataGridColumn id="rightDummy" width="5" minWidth="5" maxWidth="5"/>
</mxcolumns>

不过要小心。如果您在列上设置宽度,它不会被解释为文字值或实际百分比,而是被解释为某种半途而废的比例。我只能假设列大小调整程序厌倦了计算,并决定对列宽度进行某种“合理”解释——当然,这在大多数情况下是完全不合理的。

此时此刻,我非常沮丧,我正在考虑使用第 3 方产品 ElfGrid,它解决这些问题以及更多问题。查看文档,尤其是 ElfColumnUtils,其中有一些非常方便的方法来处理这些问题。在我所做的测试中它也相当快。

You've hit upon one of the biggest problems of the DataGrid and AdvancedDataGrid. I absolutely hate how hard it is to get cell content to appear comfortably. For reasons not immediately apparent, narrow field values will appear in very wide cells while wide content and headers will get scrunched.

This is especially true for the first and last columns for some reason.

The only solution I have found is to set the minWidth property on columns, and I have to go through the data first to find the widest outliers in those columns and make sure they fit comfortably. Another solution that helps is to have dummy columns on the left and right that are given widths and minWidths and maxWidths of some very small size, say 5, which seems to allow the real columns in the middle to "breathe" a little better.

<mx:columns>
  <mx:DataGridColumn id="leftDummy" width="5" minWidth="5" maxWidth="5"/>
  <!-- Your "real" columns here, with minWidth assignments -->
  <mx:DataGridColumn id="rightDummy" width="5" minWidth="5" maxWidth="5"/>
</mxcolumns>

Be careful, though. If you set a width on a column it gets interpreted not as a literal value or an actual percentage but as some kind of half-assed proportion. I can only assume that the column-sizing procedures get tired of calculating and settle on some kind of "reasonable" interpretation of column width — which, of course, turns out to be utterly unreasonable much of the time.

At this moment I am so frustrated I am considering going with a 3rd party product, ElfGrid, which solves these issues and more. Look at the documentation, especially the ElfColumnUtils, which have some very handy methods for dealing with these issues. It's also quite fast in the testing I've done.

兲鉂ぱ嘚淚 2024-09-09 13:22:57

这是我的做法,它工作得很好...

创建一个可绑定变量并给它一个起始数字(假设为 100),例如:

[Bindable] private var colWidth:int = 100;

使用数据绑定来调整列的大小,例如:

mx:DataGridColumn width="{colWidth}"

向数据添加调整大小事件处理程序grid,例如:

myDataGrid.addEventListener(ResizeEvent.RESIZE , onDataGridResize);

在处理程序中侦听数据网格的新宽度,获取该数字并将其除以列数(如果所有列都相等),或者进行一些数学计算以创建“百分比”列的宽度,例如:

private function onDataGridResize(event:ResizeEvent):void {
  colWidth = myDataGrid.width / numberOfColumns;
}

Here's how I do it, and it works quite well...

Create a bindable variable and give it a starting number (let's say 100), ex:

[Bindable] private var colWidth:int = 100;

Use data binding to size the columns, ex:

mx:DataGridColumn width="{colWidth}"

Add a resize event handler to your data grid, ex:

myDataGrid.addEventListener(ResizeEvent.RESIZE , onDataGridResize);

In the handler listen for the new width of the data grid, take that number and divide it by the amount of columns (if all columns are to be equal), or do some math to create the 'percentage' for the width of the columns, ex:

private function onDataGridResize(event:ResizeEvent):void {
  colWidth = myDataGrid.width / numberOfColumns;
}
℉服软 2024-09-09 13:22:57

如果您可以猜测可能的宽度,则可以将宽度设置为某个固定百分比。
但我建议另一个选项:

  1. 将数据网格的variableRowHeight属性设置为true,
  2. 将具有较长文本的列的wordWrap属性设置为true。

通过这种方式,您的长文本将被放入多行中,并且只有行高会增加。尝试一下吧。如果您不想要这个,那么我认为您可能需要编写一个扩展 datagridColumn 的新类。

<mx:DataGrid dataProvider="{testAC}" width="80%" height="100%" variableRowHeight="true">
         <mx:columns>
              <mx:DataGridColumn dataField="company" wordWrap="true"/>                    <mx:DataGridColumn dataField="share"/>
              <mx:DataGridColumn dataField="expense"/>
        </mx:columns>
 </mx:DataGrid>

干杯,
PK

You can set the width to some fixed percentage if you can guess the possible width.
But i would suggest another optiong :

  1. set the variableRowHeight property of the datagrid to true
  2. set the wordWrap property of the column which will have longer text to true.

By this way your long text will be put in multiple lines and only that row height will be increased. just give it a try. if you dont want this, then i think you may need to write a new class extending datagridColumn.

<mx:DataGrid dataProvider="{testAC}" width="80%" height="100%" variableRowHeight="true">
         <mx:columns>
              <mx:DataGridColumn dataField="company" wordWrap="true"/>                    <mx:DataGridColumn dataField="share"/>
              <mx:DataGridColumn dataField="expense"/>
        </mx:columns>
 </mx:DataGrid>

Cheers,
PK

夏有森光若流苏 2024-09-09 13:22:57

我遇到了同样的问题,您应该事先知道较长字符串的大小,然后分配该大小的 MinWidth 并使列可调整大小 = 'false'。

这对我有用

I had the same problem, you should know beforehand the size of your longer string, then assigns the MinWidth that size and have the column resizeable = 'false'.

It worked for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文