DEV Community

Ray
Ray

Posted on

VTable usage issue: How to make the table automatically calculate column width based only on the table header

Question title

How to make the table automatically calculate column width based only on the content width of the table header

Problem description

In automatic width mode, you want the width of a column to be determined only by the content width of the header cell and not affected by the content cell.

Solution

VTable provides columnWidthComputeModeconfiguration for specifying the bounded areas that are involved in content width calculations:

  • 'Only-header ': Only the header content is calculated.

  • 'Only-body ': Only calculate the content of the body cell.

  • 'Normal ': Calculate normally, that is, calculate the contents of the header and body cells.

Code example

const options = {
    //......
    columnWidthComputeMode: 'only-header'
};
Enter fullscreen mode Exit fullscreen mode

Results show

Image description

Full sample code (you can try pasting it into the editor ):

let  tableInstance;
  fetch('https://lf9-dp-fe-cms-tos.byteorg.com/obj/bit-cloud/VTable/North_American_Superstore_data.json')
    .then((res) => res.json())
    .then((data) => {

const columns =[
    {
        "field": "Order ID",
        "title": "Order ID",
        "width": "auto"
    },
    {
        "field": "Customer ID",
        "title": "Customer ID",
        "width": "auto"
    },
    {
        "field": "Product Name",
        "title": "Product Name",
        "width": "auto"
    }
];

const option = {
  records:data,
  columns,
  widthMode:'standard',
  columnWidthComputeMode: 'only-header'
};
tableInstance = new VTable.ListTable(document.getElementById(CONTAINER_ID),option);
window['tableInstance'] = tableInstance;
    })
Enter fullscreen mode Exit fullscreen mode

Related Documents

Related api: https://www.visactor.io/vtable/option/ListTable#columnWidthComputeMode

github:https://github.com/VisActor/VTable

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay