如何自动调整 SlickGrid 中的列大小?
我希望 slickgrid 根据最宽的内容或标题文本自动调整列的大小 - 以较宽者为准。简单来说,我希望它在调整列大小时模拟常规 HTML 表格的默认行为。我怎样才能在 slickgrid 中做到这一点?
I want slickgrid to autosize the columns based on the widest content or header text - whichever is wider. In simpler terms, I want it to simulate the default behavior of regular HTML tables when it comes to column sizing. How can I do it in slickgrid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在构造选项时,您可以使用
forceFitColumns: true
这将使列填充网格 div 的整个宽度。
When constructing your options, you can use
forceFitColumns: true
This will make the columns fill the entire width of your grid div.
OP 正在寻找能够增长的专栏以与其内容相匹配。 grid.autosizeColumns() 增大单元格以适合父容器,这不是一回事。
我添加了此功能,并且它与您想象的一样手动。您循环显示的单元格并测量每个单元格,保存最宽的单元格并使用该宽度来设置列的宽度。 SlickGrid 使您可以轻松访问视口中的单元格,因此效果很好。
测量算法是您的重大决定。您可以将内容放在屏幕之外并进行测量,正如 @jay 建议的那样。这是可行的,但它是最慢的方法,因为插入时需要重新绘制,删除时也需要重新绘制。可能有一些方法可以优化。我采用的解决方案是测量字母表中每个字母以及我们遇到的其他印刷字符的宽度,并将它们相加以生成宽度。是的,这听起来很荒谬。它有很多限制:字体大小必须相同、不支持图像、不能有任何换行符等等。如果您能忍受这些限制,您可以在
<5ms
内计算出巨大网格视口的大小,因为字符宽度仅测量一次。获取列的大小后,使用
grid.setColumns()
将它们分配给您的列。The OP is looking for columns to grow to match their content.
grid.autosizeColumns()
grows the cells to fit the parent container, which is not the same thing.I have added this feature, and it is about as manual as you might imagine. You loop through the displayed cells and measure each one, saving the widest cell and using that width to set the width of your column. SlickGrid gives you good access to the cells in the viewport, so that works nicely.
The measurement algorithm is your big decision. You may put the content off screen and measure it, as @jay suggests. This works, but it is the slowest method, as it requires a repaint to insert, and a repaint when you remove. There may be ways to optimize. The solution I went with is to measure the width of every letter in the alphabet, as well as other typographic characters we come across, and sum them to generate a width. Yes, this sounds absurd. It has many constraints: The font size must be the same, it doesn't support images, there can't be any line returns, and more. If you can live with the constraints though, you can calculate sizes for a huge grid viewport in
<5ms
, because the character widths are only measured once.After you get the sizes of the columns, you assign them to your columns using
grid.setColumns()
.Slickgrid不支持根据数据自动调整列大小。您需要编写插件或fork slickgrid核心来修改。
这是我创建的插件来处理 slickgrid 自动大小的链接
https://github.com/naresh-n/slickgrid-column-data-autosize
Slickgrid will not support column auto size based on data.You need to write a plugin or fork the slickgrid core to modify.
Here is the link I have created a plugin to handle slickgrid auto size
https://github.com/naresh-n/slickgrid-column-data-autosize
我在绘制网格后添加了它,并且效果很好。
I added this after the grid is drawn and it works fine.
您应该能够调用网格对象的
autosizeColumns()
方法。You should be able to call the
autosizeColumns()
method of the grid object.对 Naresh 的 https://github.com/naresh-n/slickgrid 进行简单调整-column-data-autosize,在
init
函数上:$container.ready(resizeAllColumns);
添加到 init 函数中。这确保了列在初始加载时自动调整大小
Make this simple adjustment to Naresh's https://github.com/naresh-n/slickgrid-column-data-autosize, on the
init
function:$container.ready(resizeAllColumns);
to the init function.This ensures the columns autoresize on initial load
将文本插入屏幕外元素并检索元素的宽度。这就是 excanvas 测量文本的方法。使用它来设置列的宽度,因为它需要像素值。
Insert the text into an off-screen element and retrieve the width of the element. This is what excanvas does to measure text. Use this to set the width of the column since it's expecting a pixel value.