Slickgrid column width should automatically get resized according to widest row content

て烟熏妆下的殇ゞ 提交于 2019-12-03 16:48:54

Yes, To add to that. It takes away from the whole point of the row virtualization. But if your data set is small you could calculate it up front. Here is a function I have used to calculate the width of some text.

 String.prototype.textWidth = function(font) {
    var f = font || '12px Helvetica,​Arial,​sans-serif',
      o = $('<div>' + this + '</div>')
            .css({'position': 'absolute', 'float': 'left', 'white-space': 'nowrap', 'visibility': 'hidden', 'font': f})
            .appendTo($('body')),
      w = o.width();

    o.remove();
  return w;
}

What i did was calculate either the textWidth() of the header. "Name" value. So at least the column is the width of the header text. Which you can do without worrying about virtualization. Fx...

columns.push({ id: "col1", name: "Column 1", field: "col1", width: "Column 1".textWidth() + <Some padding?>});

Or if you really want scan your entire data array calling the function against each data value in the column/field and then keep the max. But this is very inefficient.

You can use the forceFitColumns option, that will resize each columns to fit, though if your width is not wide enough it will try to fit according to your minimal and maximal values of width, so you might want to add a minWidth and a maxWidth so you have a bit more control.

For example the columns definition might look like this:

{id: "duration", name: "Duration", field: "duration", minWidth:50, width:100, maxWidth:120}

and the options definition like this:

options = {
        enableCellNavigation: true,
        enableColumnReorder: false,
        multiColumnSort: true,
        asyncEditorLoading: true,
        forceFitColumns: true // this one is important        
    };
SimplGy

Yes, grow to fit content can be done, but you have to do it yourself. forceFitColumns sounds close, but it grows to fill the viewport without concerning itself with the cell contents.

@Tim's idea is right on, although I wouldn't go as far as to say it takes away from the point of row virtualization. Because SlickGrid gives you nice access to the viewport grid.getViewport().top // bottom you can calculate the size of only the content in your viewport very successfully.

I describe how here in answer to this similar question: https://stackoverflow.com/a/22231459

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!