Skip to content

Table

A table displays rows of data.

When To Use

  • To display a collection of structured data.
  • To sort, search, paginate, filter data.

Examples

Basic Usage

vue
<template>
  <a-table :columns="columns" :data-source="data">
    <template #headerCell="{ column }">
      <template v-if="column.key === 'name'">
        <span>
          <smile-outlined />
          Name
        </span>
      </template>
    </template>

    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'name'">
        <a>
          {{ record.name }}
        </a>
      </template>
      <template v-else-if="column.key === 'tags'">
        <span>
          <a-tag
            v-for="tag in record.tags"
            :key="tag"
            :color="tag === 'loser' ? 'volcano' : tag.length > 5 ? 'geekblue' : 'green'"
          >
            {{ tag.toUpperCase() }}
          </a-tag>
        </span>
      </template>
      <template v-else-if="column.key === 'action'">
        <span>
          <a>Invite 一 {{ record.name }}</a>
          <a-divider type="vertical" />
          <a>Delete</a>
        </span>
      </template>
    </template>
  </a-table>
</template>

<script setup>
import { SmileOutlined } from '@ant-design/icons-vue'

const columns = [
  {
    title: 'Name',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: 'Age',
    dataIndex: 'age',
    key: 'age',
  },
  {
    title: 'Address',
    dataIndex: 'address',
    key: 'address',
  },
  {
    title: 'Tags',
    key: 'tags',
    dataIndex: 'tags',
  },
  {
    title: 'Action',
    key: 'action',
  },
]

const data = [
  {
    key: '1',
    name: 'John Brown',
    age: 32,
    address: 'New York No. 1 Lake Park',
    tags: ['nice', 'developer'],
  },
  {
    key: '2',
    name: 'Jim Green',
    age: 42,
    address: 'London No. 1 Lake Park',
    tags: ['loser'],
  },
  {
    key: '3',
    name: 'Joe Black',
    age: 32,
    address: 'Sidney No. 1 Lake Park',
    tags: ['cool', 'teacher'],
  },
]
</script>

Selection

vue
<template>
  <div>
    <div style="margin-bottom: 16px">
      <a-button type="primary" :disabled="!hasSelected" :loading="loading" @click="start">
        Reload
      </a-button>
      <span style="margin-left: 8px">
        <template v-if="hasSelected">
          {{ `Selected ${selectedRowKeys.length} items` }}
        </template>
      </span>
    </div>
    <a-table :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" :columns="columns" :data-source="data" />
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'

const columns = [
  {
    title: 'Name',
    dataIndex: 'name',
  },
  {
    title: 'Age',
    dataIndex: 'age',
  },
  {
    title: 'Address',
    dataIndex: 'address',
  },
]

const data = []
for (let i = 0; i < 46; i++) {
  data.push({
    key: i,
    name: `Edward King ${i}`,
    age: 32,
    address: `London, Park Lane no. ${i}`,
  })
}

const selectedRowKeys = ref([])
const loading = ref(false)

const hasSelected = computed(() => selectedRowKeys.value.length > 0)

const start = () => {
  loading.value = true
  setTimeout(() => {
    selectedRowKeys.value = []
    loading.value = false
  }, 1000)
}

const onSelectChange = (changableRowKeys) => {
  console.log('selectedRowKeys changed: ', changableRowKeys)
  selectedRowKeys.value = changableRowKeys
}
</script>

Sorting and Filtering

vue
<template>
  <a-table :columns="columns" :data-source="data" @change="handleChange" />
</template>

<script setup>
const columns = [
  {
    title: 'Name',
    dataIndex: 'name',
    filters: [
      {
        text: 'Joe',
        value: 'Joe',
      },
      {
        text: 'Jim',
        value: 'Jim',
      },
      {
        text: 'Submenu',
        value: 'Submenu',
        children: [
          {
            text: 'Green',
            value: 'Green',
          },
          {
            text: 'Black',
            value: 'Black',
          },
        ],
      },
    ],
    onFilter: (value, record) => record.name.indexOf(value) === 0,
    sorter: (a, b) => a.name.length - b.name.length,
    sortDirections: ['descend'],
  },
  {
    title: 'Age',
    dataIndex: 'age',
    defaultSortOrder: 'descend',
    sorter: (a, b) => a.age - b.age,
  },
  {
    title: 'Address',
    dataIndex: 'address',
    filters: [
      {
        text: 'London',
        value: 'London',
      },
      {
        text: 'New York',
        value: 'New York',
      },
    ],
    onFilter: (value, record) => record.address.indexOf(value) === 0,
  },
]

const data = [
  {
    key: '1',
    name: 'John Brown',
    age: 32,
    address: 'New York No. 1 Lake Park',
  },
  {
    key: '2',
    name: 'Jim Green',
    age: 42,
    address: 'London No. 1 Lake Park',
  },
  {
    key: '3',
    name: 'Joe Black',
    age: 32,
    address: 'Sidney No. 1 Lake Park',
  },
  {
    key: '4',
    name: 'Jim Red',
    age: 32,
    address: 'London No. 2 Lake Park',
  },
]

const handleChange = (pagination, filters, sorter) => {
  console.log('Various parameters', pagination, filters, sorter)
}
</script>

API

Table

PropertyDescriptionTypeDefaultVersion
borderedWhether to show all table bordersbooleanfalse
columnsColumns of tablearray-
componentsOverride default table elementsobject-
dataSourceData record array to be displayedany[]-
defaultExpandAllRowsExpand all rows initiallybooleanfalse
defaultExpandedRowKeysInitial expanded row keysstring[]-
expandedRowKeysCurrent expanded row keysstring[]-
expandedRowRenderExpanded container render for each rowFunction(record, index, indent, expanded):VNode | v-slot:expandedRowRender="{record, index, indent, expanded}"-
expandIconCustomize row expand Icon.Function(props):VNode | v-slot:expandIcon="props"-
expandRowByClickWhether to expand row by clicking anywhere in the whole rowbooleanfalse
footerTable footer rendererFunction(currentPageData) | v-slot:footer="currentPageData"-
indentSizeIndent size in pixels of tree datanumber15
loadingLoading status of tableboolean | object (more)false
localeThe i18n text including filter, sort, empty text, etcobjectfilterConfirm: 'Ok'
filterReset: 'Reset'
emptyText: 'No Data'
Default
paginationConfig of pagination. You can ref table pagination config or full pagination document, hide it by setting it to falseobject | false-
rowClassNameRow's classNameFunction(record, index):string-
rowKeyRow's unique key, could be a string or function that returns a stringstring | Function(record):string'key'
rowSelectionRow selection configobject-
scrollWhether table can be scrolled in x/y direction, x or y can be a number that indicates the width and height of table bodyobject-
showHeaderWhether to show table headerbooleantrue
showSorterTooltipThe header show next sorter direction tooltip. It will be set as the property of Tooltip if its type is objectboolean | Tooltip propstrue
sizeSize of tabledefault | middle | smalldefault
sortDirectionsSupported sort way, could be ascend, descendArray[ascend, descend]
stickySet sticky header and scroll barboolean | {offsetHeader?: number, offsetScroll?: number, getContainer?: () => HTMLElement}-
summarySummary contentv-slot:summary-
tableLayouttable-layout attribute of table element- | 'auto' | 'fixed'-
fixed when header/columns are fixed, or using column.ellipsis
titleTable title rendererFunction(currentPageData) | v-slot:title="currentPageData"-
transformCellTextData can be changed again before rendering. The default configuration of general user empty data.Function({ text, column, record, index }) => any | v-slot:transformCellText="{text, column, record, index}"-

Table Events

Events NameDescriptionArgumentsVersion
changeCallback executed when pagination, filters or sorter is changedFunction(pagination, filters, sorter, { currentDataSource })
expandCallback executed when the row expand icon is clickedFunction(expanded, record)
expandedRowsChangeCallback executed when the expanded rows changeFunction(expandedRows)
resizeColumnCallback executed when a column is resizedFunction(width, column)

Column

One of the Table columns prop for describing the table's columns, Column has the same API.

PropertyDescriptionTypeDefaultVersion
alignThe specify which way that column is alignedleft | right | centerleft
colSpanSpan of this column's titlenumber-
dataIndexDisplay field of the data record, support nest path by string arraystring | string[]-
defaultFilteredValueDefault filtered valuesstring[]-
defaultSortOrderDefault order of sorted valuesascend | descend-
ellipsisThe ellipsis cell content, not working with sorter and filters for now.
tableLayout would be fixed when ellipsis is true.
boolean |false
filterDropdownCustomized filter overlayVNode | slot-
filterDropdownVisibleWhether filterDropdown is visibleboolean-
filteredWhether the dataSource is filteredbooleanfalse
filteredValueControlled filtered value, filter icon will highlightstring[]-
filterIconCustomized filter iconVNode | ({filtered: boolean, column: Column}) => vNode | v-slot:filterIcon="{filtered, column}"false
filterMultipleWhether multiple filters can be selectedbooleantrue
filtersFilter menu configobject[]-
fixed(IE not supported) Set column to be fixed: true(same as left) 'left' 'right'boolean | stringfalse
keyUnique key of this column, you can ignore this prop if you've set a unique dataIndexstring-
responsiveThe list of breakpoints at which to display this column. Always visible if not set.Breakpoint[]-
sorterSort function for local sort, see Array.sort's compareFunction. If you need sort buttons only, set to trueFunction | boolean-
sortOrderOrder of sorted values: ascend descend falseboolean | string-
sortDirectionsSupported sort way, override sortDirections in Table, could be ascend, descendArray[ascend, descend]
titleTitle of this columnstring | VNode | v-slot:title-
widthWidth of this column (width not working?)string | number-
customCellSet props on per cellFunction(record, rowIndex)-
customHeaderCellSet props on per header cellFunction(column)-
onFilterCallback executed when the confirm filter button is clicked, Use as a filter event when using template or jsxFunction-
onFilterDropdownVisibleChangeCallback executed when filterDropdownVisible is changedfunction(visible) {}-
slotsWhen using columns, you can use this property to configure the properties that support slot, such as slots: { title: 'XXX'}object-

rowSelection

Properties for row selection.

PropertyDescriptionTypeDefaultVersion
checkStrictlyCheck table row precisely; parent row and children rows are not associatedbooleantrue
columnWidthSet the width of the selection columnstring | number32px
columnTitleSet the title of the selection columnstring | VNode-
fixedFixed selection column on the leftboolean-
getCheckboxPropsGet Checkbox or Radio propsFunction(record)-
hideSelectAllHide the selectAll checkbox and custom selectionbooleanfalse
preserveSelectedRowKeysKeep selection key even when it removed from dataSourceboolean-
selectedRowKeysControlled selected row keysstring[] | number[][]
selectionsCustom selection config, only displays default selections when set to trueobject[] | boolean-
typecheckbox or radiocheckbox | radiocheckbox
onChangeCallback executed when selected rows changeFunction(selectedRowKeys, selectedRows)-
onSelectCallback executed when select/deselect one rowFunction(record, selected, selectedRows, nativeEvent)-
onSelectAllCallback executed when select/deselect all rowsFunction(selected, selectedRows, changeRows)-
onSelectInvertCallback executed when row selection is invertedFunction(selectedRowKeys)-
onSelectNoneCallback executed when row selection is clearedfunction()-

scroll

PropertyDescriptionTypeDefaultVersion
scrollToFirstRowOnChangeWhether to scroll to the top of the table when paging, sorting, filtering changesboolean-
xSet horizontal scrolling, can also be used to specify the width of the scroll area, could be number, percent value, true and 'max-content'string | number | true-
ySet vertical scrolling, can also be used to specify the height of the scroll area, could be string or numberstring | number-

selection

Custom selection config

PropertyDescriptionTypeDefaultVersion
keyUnique key of this selectionstring-
textDisplay text of this selectionstring | VNode-
onSelectCallback executed when this selection is clickedFunction(changeableRowKeys)-

Released under the MIT License.