Background
In Vue, you can make a Wijmo FlexGrid header row automatically grow after a column is resized by enabling wrapping on the column header cells and then calling autoSizeRow(0, true) after the grid renders and after column resizing.
Steps to Complete
- Register the Wijmo Vue grid components.
- Add the FlexGrid markup.
- Add wrapping to the column header cells.
- Resize the header row after loading and column resizing.
Getting Started
Register the Wijmo Vue grid components
// main.js
import { createApp } from 'vue';
import App from './App.vue';
import '@mescius/wijmo.styles/wijmo.css';
import { registerGrid } from '@mescius/wijmo.vue2.grid';
const app = createApp(App);
registerGrid(app);
app.mount('#app');
- This imports the Wijmo stylesheet and registers the Vue grid components, including
wj-flex-gridandwj-flex-grid-column.
Add the FlexGrid markup
<wj-flex-grid
:itemsSource="data"
:autoGenerateColumns="false"
:initialized="initializeGrid"
>
<wj-flex-grid-column binding="id" header="ID" :width="60" />
<wj-flex-grid-column binding="customerName" header="Customer Name" :width="140" />
<wj-flex-grid-column
binding="orderDescription"
header="Detailed Order Description"
:width="160"
/>
<wj-flex-grid-column binding="orderDate" header="Order Date" :width="120" />
</wj-flex-grid>
- This creates the FlexGrid, binds it to the Vue data source, defines the columns, and calls
initializeGridwhen the grid is ready.
Add wrapping to the column header cells
function initializeGrid(grid) {
grid.formatItem.addHandler((s, e) => {
toggleClass(e.cell, 'wj-wrap', e.panel.cellType === CellType.ColumnHeader);
});
}
- The
formatItemhandler checks whether the cell belongs to the column header panel. If it does, thewj-wrapclass is applied so long header text can wrap.
Resize the header row after loading and column resizing
function initializeGrid(grid) {
const autoSizeHeaderRow = () => {
grid.autoSizeRow(0, true);
};
grid.formatItem.addHandler((s, e) => {
toggleClass(e.cell, 'wj-wrap', e.panel.cellType === CellType.ColumnHeader);
});
grid.loadedRows.addHandler(autoSizeHeaderRow);
grid.resizedColumn.addHandler(autoSizeHeaderRow);
autoSizeHeaderRow();
}
- The
loadedRowsevent sizes the header row when the grid first renders. TheresizedColumnevent runs after the user resizes a column, so the header row is resized again to fit the wrapped header text.
With this setup, long column headers wrap when a column becomes narrow, and the column header row automatically increases in height after the resize.
Happy coding!
Andrew Peterson
Technical Engagement Engineer
