Quasar QTable columns are where most table bugs start: a date that sorts as text, a price that searches as 12900 instead of $129.00, a name column stuck to the right edge. Every one of those is a single option on the column object. This post goes through each option the Quasar 2.35.2 API has, with the rule behind it, and introduces a free tool that writes the first draft of the array for you: the QTable Columns Generator.
If you haven't built a QTable yet, start with the Quasar QTable tutorial, which covers filtering, pagination and a status cell. Here we stay on the columns prop.
Typing column objects by hand gets old after the third table. The QTable Columns Generator starts from your data instead:
{ "data": [...] }, works too.name, label, field, align chosen from the value type, sortable, and a format function for numbers, money and dates. Nested objects get their own columns with a field function, such as row => row.customer.name.columns array, typed as QTableColumn<Row>[] with an inferred Row interface, or a complete QTable component that uses it, in TypeScript or JavaScript.It runs in your browser. Nothing is sent anywhere and you don't need an account. The generated field always keeps the raw value: numbers stay numbers and ISO dates stay ISO strings, which sort in date order when they share one format. The readable text goes in format. Treat the output as a first draft: the sections below explain what each generated option does, so you know which ones to change.
| Option | Type | What it does |
|---|---|---|
name | string, required | The column's id. sortBy, visible-columns and the body-cell-[name] slots refer to it. |
label | string, required | Header text. |
field | string or (row) => any | Where the value comes from. |
format | (val, row) => string | How the value is displayed. |
align | 'left', 'center', 'right' | Defaults to 'right'. |
sortable | boolean | Makes the header clickable. |
sort / rawSort | (a, b, rowA, rowB) => number | Custom comparison. |
sortOrder | 'ad' or 'da' | Direction of the first click. |
required | boolean | Always shown, even with visible-columns. |
classes / style | string or (row) => string | Classes and styles on body cells. |
headerClasses / headerStyle | string | Classes and styles on the header cell. |
autoWidth | boolean | Shrinks the column to its content (v2.31+). |
The rest of the post takes them in the order you'll need them.
field tells QTable where the cell value lives. A string reads that key from the row. A function receives the row and returns anything, which is how you reach nested data or compute a value:
import type { QTableColumn } from 'quasar'
interface Invoice {
id: string
customer: { name: string; country: string }
amountCents: number
issuedAt: string
paidAt: string | null
overdueDays: number
}
const columns: QTableColumn<Invoice>[] = [
{ name: 'id', label: 'Invoice', field: 'id', align: 'left', sortable: true },
{ name: 'customer', label: 'Customer', field: row => row.customer.name, align: 'left', sortable: true },
]The value field returns is the one QTable sorts by, so it should be the raw value (a number, a Date), not a display string. A column with no field at all is allowed. Its value is empty and QTable skips it when sorting and filtering, which is what you want for an actions column drawn entirely by a slot.
QTableColumn<Invoice> is worth the import: with a row type, a string field must be a real key of Invoice, and the function form gets a typed row.
format receives the value from field and the whole row, and returns what the cell shows:
const money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' })
const day = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium' })
const columns: QTableColumn<Invoice>[] = [
{
name: 'amount',
label: 'Amount',
field: 'amountCents',
sortable: true,
format: (cents: number) => money.format(cents / 100),
},
{
name: 'issuedAt',
label: 'Issued',
field: row => new Date(row.issuedAt),
sortable: true,
format: (date: Date) => day.format(date),
},
]Three rules decide how format behaves:
amountCents, a number, so $1,200.00 correctly lands after $950.00.Nov 3 finds the invoice issued on that day.QTableColumn types format as returning string. For anything richer, a badge or a link, use a body-cell-[name] slot.Create the Intl formatters once, outside the column array. format runs for every visible cell on every render.
The date column shows the trick for date sorting: the field function turns the ISO string into a Date, and QTable's default sort compares two Date objects as dates. The format function turns it back into readable text.
Every column is right-aligned unless you say otherwise. QTable puts the text-right class on the header and body cells, and a left or center value switches it. The convention is simple:
right (the default), so digits line up;left;center.If you leave out columns entirely, QTable builds them from the first row: one column per key, the key in upper case as the label, right for numbers and left for everything else, all sortable. That's fine for a quick look at data. For a real screen you want proper labels, formats and sort rules, which is the gap the QTable Columns Generator fills.
sortable: true is enough for most columns. The default comparison looks at the two raw values: numbers as numbers, Date objects as dates, booleans as booleans, and anything else as lowercase text. The tutorial shows a sort function for a status column with its own order.
Two details are easy to miss.
Empty values. Before sort is called, rows whose value is null or undefined are put aside: they come first when sorting ascending and last when sorting descending. When "Unpaid" should sort after every paid date, handle it yourself with rawSort, which receives empty values too:
{
name: 'paidAt',
label: 'Paid',
field: 'paidAt',
sortable: true,
rawSort: (a: string | null, b: string | null) => {
if (a === b) return 0
if (a === null) return 1
if (b === null) return -1
return a < b ? -1 : 1
},
format: (val: string | null) => (val === null ? 'Unpaid' : day.format(new Date(val))),
}QTable multiplies the result by -1 for a descending sort, so unpaid invoices move to the top when the user flips the column.
The first click. A click on a sortable header sorts ascending, the next one descending, the third removes the sort. For dates and amounts, users usually want "newest" or "largest" first. sortOrder: 'da' on the column makes the first click sort descending. The table-wide column-sort-order="da" prop does the same for every column, and the column option wins over the prop.
classes and style style the body cells. Both take a string, or a function that receives the row, so a cell can react to its data:
{
name: 'overdue',
label: 'Overdue',
field: 'overdueDays',
sortable: true,
format: (days: number) => (days > 0 ? `${days} days` : '—'),
classes: row => (row.overdueDays > 30 ? 'text-negative text-weight-medium' : ''),
headerClasses: 'text-no-wrap',
}headerClasses and headerStyle apply to the header cell only, and they're plain strings. Use them for widths (headerStyle: 'width: 160px') and to stop a long label from wrapping.
autoWidth: true shrinks a column to the narrowest width its header and cells need. It suits icon columns, checkboxes and row actions, and it needs Quasar 2.31 or later.
When you let users pick columns with the visible-columns prop, any column not in that list is hidden, except columns with required: true. Mark the column that identifies the row (the invoice number, the customer name) as required, so a user can't hide every way of telling rows apart.
Why does my QTable column sort numbers as text? The field returns strings like "129". Return a number from field (row => Number(row.total)), or add a sort function.
Can a column show two fields? Yes. Use a field function for the sort value and a format that reads the row, or a body-cell-[name] slot for markup.
Why is my format function not used in my slot? It is: props.value in a body-cell slot is the formatted value. Use props.row for the raw data.
Do I need TypeScript for the generator's output? No. Switch the generator to JavaScript and it drops the QTableColumn import, the Row interface and the parameter types.
Open the QTable Columns Generator, paste a sample of your API response, and copy the columns. Then go through the draft with the rules above: the right field for sorting, a format for display, align for each type, and sortOrder: 'da' where newest-first makes sense.