Table.vue component provides a flexible and dynamic way to render tabular data in Vue. It supports custom slots for headers, cells, checkboxes, and sorting elements, allowing developers to fully customize the table’s functionality and presentation.
Overview
TheTable component accepts columns and rows as props to define the structure and content of the table. Slots are provided for extending and customizing various parts of the table, including:
Headers
Define headers for table columns for clear structure and labeling.
Rows
Populate the table with dynamic rows based on the provided data.
Checkbox Elements
Include checkbox elements for selecting rows or performing bulk actions.
Sorting Indicators
Display sorting indicators to show the current sort order of the table.
Props
Defines the columns of the table. Each column object should include at least a
header and optionally a field and additional properties.Defines the rows of the table. Each row is an object containing data corresponding to the fields defined in the columns.
Enables box styling for the component.
Enables a compact mode for the component.
Allows content to overflow its container.
Makes the component stick to its position when scrolling. (does not work with overflow)
Slots
Slot for rendering the checkbox header.
<template #checkboxHead="{}"></template>Slot for rendering checkboxes in table cells.
<template #checkboxCell="{ row, rowIndex, colIndex }"></template>Slot for rendering custom content in the table header.
<template #head="{ column, field }"></template>Slot for rendering sorting controls in the header.
<template #headSort="{ column, field }"></template>Slot for rendering custom content in table cells.
<template #cell="{ row, rowIndex, colIndex, field, column }"></template>Usage
Basic Table Example
Here’s an example of how to implement theTable component with dynamic data and custom slots:
Columns and Rows Data Format
Columns
Thecolumns prop should be an array of objects. Each column object can include:
| Property | Type | Required | Description |
|---|---|---|---|
header | String | true | The display name for the column header. |
field | String | false | The key in the rows data that corresponds to this column. |
class | String | false | Optional class for custom styling of the column. |
Rows
Therows prop should be an array of objects, where each object represents a row. The keys in each row object should match the field values in the columns array.
Example:
Customization via Slots
Custom Checkbox
Custom Header with Sorting
Custom Cell Content
Notes
- Dynamic Data: Ensure the
rowsprop data corresponds to the fields defined in thecolumnsprop. - Custom Slots: Utilize slots for advanced customization of table behavior, such as sorting, filtering, or adding interactive elements.
- Responsive Design: The table structure supports scrollable content for better handling of wide datasets.