自动调整 DataGridCoulmn/AdvancedDataGridColumn 的宽度以适合内容
我希望 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您已经遇到了 DataGrid 和 AdvancedDataGrid 的最大问题之一。我非常讨厌让单元格内容看起来舒适是多么困难。由于一些不太明显的原因,狭窄的字段值将出现在非常宽的单元格中,而宽的内容和标题将被压缩。
由于某种原因,对于第一列和最后一列尤其如此。
我找到的唯一解决方案是在列上设置 minWidth 属性,并且我必须首先浏览数据以找到这些列中最宽的异常值并确保它们适合。另一个有用的解决方案是在左侧和右侧设置虚拟列,这些虚拟列的宽度以及 minWidths 和 maxWidths 的尺寸非常小,例如 5,这似乎可以让中间的真实列更好地“呼吸”。
不过要小心。如果您在列上设置宽度,它不会被解释为文字值或实际百分比,而是被解释为某种半途而废的比例。我只能假设列大小调整程序厌倦了计算,并决定对列宽度进行某种“合理”解释——当然,这在大多数情况下是完全不合理的。
此时此刻,我非常沮丧,我正在考虑使用第 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.
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.
这是我的做法,它工作得很好...
创建一个可绑定变量并给它一个起始数字(假设为 100),例如:
使用数据绑定来调整列的大小,例如:
向数据添加调整大小事件处理程序grid,例如:
在处理程序中侦听数据网格的新宽度,获取该数字并将其除以列数(如果所有列都相等),或者进行一些数学计算以创建“百分比”列的宽度,例如:
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:
Use data binding to size the columns, ex:
Add a resize event handler to your data grid, ex:
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:
如果您可以猜测可能的宽度,则可以将宽度设置为某个固定百分比。
但我建议另一个选项:
通过这种方式,您的长文本将被放入多行中,并且只有行高会增加。尝试一下吧。如果您不想要这个,那么我认为您可能需要编写一个扩展 datagridColumn 的新类。
干杯,
PK
You can set the width to some fixed percentage if you can guess the possible width.
But i would suggest another optiong :
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.
Cheers,
PK
我遇到了同样的问题,您应该事先知道较长字符串的大小,然后分配该大小的 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