Handsontable, colWidths: function: how to set a column width fitting content?

Viewed 6416

My aim is to set the first column's width equal to a certain number and leave other columns' widths untouched. Now I know that setting

colWidths: [myValue]

actually breaks other columns' widths but this seems to be workaroundable using

colWidths: function(index) {
    if(index == 0)
        return myValue;
    return ???
}

but I wonder what should I return in the place of ???? undefined doesn't work (well, it works in the same fashion as colWidths: [myValue] – every other column gets the same default width). How do I get the "width by content" value for this?

3 Answers

I am currently using

modifyColWidth: function(width, col){
  if(width > 300) return 300;
}

Referencing by col number would work as well.

modifyColWidth: function(width, col){
  if(col === 0) return 300;
}

Edit: You would also need this after HandsOnTable have been instantiated, otherwise you cannot set the width beyond 300px.

colWidths = [];
[...Array(hot.countCols()).keys()].map(i => {
  colWidths.push(hot.getColWidth(i));
});

hot.updateSettings({
  modifyColWidth: ()=>{},
  colWidths: colWidths
});
Related