Skip to content

Custom Table Component

Inertia Table may be used in a fully headless mode: discard the default <Table> component, build your own Vue or React component, and still use every feature of the package through the useTable and useActions composables. This is ideal for list views, card grids, or any layout that doesn't fit a tabular shape but still needs filtering, sorting, and actions.

Alternative: For programmatic access to the default Table component's methods and state without building a custom one from scratch, use Template Refs instead. They expose the same useTable and useActions functionality.

Advanced usage

The composables on this page are stable and fully supported, but the custom components you build with them are your own. We're happy to help with questions about the useTable and useActions API itself, but we cannot debug or maintain custom table implementations.

Creating a Custom Table

Create a new React or Vue component and import the useActions and useTable composables from @inertiaui/table. useTable exposes the table state; useActions exposes Row Actions and Bulk Actions.

The React and Vue versions differ slightly.

vue
<script setup>
import { useActions, useTable } from '@inertiaui/table-vue'

const props = defineProps('resource')

const {
    addFilter,
    hasBulkActions,
    hasFilters,
    isNavigating,
    isSortedByColumn,
    removeFilter,
    setPerPage,
    setSort,
    state,
    toggleColumn
    visitTableUrl,
} = useTable(props.resource)

const {
    allItemsAreSelected,
    isPerformingAction,
    performAction,
    selectedItems
    toggleItem,
} = useActions()
</script>
jsx
import { useActions, useTable } from '@inertiaui/table-react'

const Table = ({ resource }) => {
    const {
        addFilter,
        hasBulkActions,
        hasExports,
        hasFilters,
        isNavigating,
        isSortedByColumn,
        removeFilter,
        setFilter,
        setPerPage,
        setSearch,
        setSort,
        sortByColumn,
        state,
        toggleColumn,
        visitTableUrl,
    } = useTable(resource)

    const {
        allItemsAreSelected,
        isPerformingAction,
        performAction,
        selectedItems
        toggleItem,
    } = useActions()
}

The Resource Object

The resource prop is the data that is passed to your component, originating from the Laravel backend. The resource object contains the following data:

KeyDescription
actionsThe actions of the table
columnsThe columns of the table
debounceTimeThe debounce time for the search input and filters
defaultPerPageThe default per page value
defaultSortThe default sort value
exportsThe exports of the table
filtersThe filters of the table
nameThe name of the table
paginationWhether the table should paginate
perPageOptionsThe options for the per page select
resultsThe results of the query (the actual data)
searchThe attributes that are searchable
stateThe state of the table

Columns

The columns key is an array of objects that define the columns of the table. Each column object has the following properties:

KeyDescription
alignmentThe alignment of the column (left, center, right)
attributeThe attribute of the Eloquent model
headerThe title of the column
metaThe custom Metadata of the column
sortableWhether the column is sortable
toggleableWhether the column can be toggled
visibleByDefaultWhether the column is visible by default

Filters

The filters key is an array of objects that define the filters of the table. Each filter object has the following properties:

KeyDescription
attributeThe attribute of the Eloquent model that should be filtered
clausesThe available clauses of the filter
labelThe label of the filter
metaThe custom Metadata of the filter
typeThe type of the filter (text, numeric, set, date, boolean)

Actions

The actions key is an array of objects that define the actions of the table. Each action object has the following properties:

KeyDescription
asBulkActionWhether the action should be displayed as a bulk action
asRowActionWhether the action should be displayed as a row action
authorizedWhether the user is authorized to perform the action
confirmationCancelButtonThe label of the confirmation dialog cancel button
confirmationConfirmButtonThe label of the confirmation dialog confirm button
confirmationMessageThe message of the confirmation dialog
confirmationRequiredWhether the action requires confirmation
confirmationTitleThe title of the confirmation dialog
dataAttributesThe custom Data Attributes of the action
iconThe custom icon of the action
isLinkWhether the action is a link
labelThe label of the action
metaThe custom Metadata of the action
showLabelWhether the label should be displayed
styleThe style of the action
urlThe URL of the action

Exports

The exports key is an array of objects that define the exports of the table. Each export object has the following properties:

KeyDescription
labelThe label of the export
authorizedWhether the user is authorized to perform the export
urlThe URL of the export
dataAttributesThe custom Data Attributes of the export
metaThe custom Metadata of the export

State

The state key is an object that contains the state of the table. The state object has the following properties:

KeyDescription
columnsAn object that contains the visibility of the columns
filtersAn object that contains the filter state (enabled, value, clause)
perPageThe per page value
searchThe search value
sortThe sort value

Implementing the Custom Table

With the useActions and useTable composables and the resource object in hand, you may start building your template. Studying the default Table component is highly recommended to see how the features fit together.

For example, to create the header of the table, you may combine the resource.columns object with the state.columns object to show or hide columns based on the state.

vue
<th
    v-for="(column, key) in resource.columns"
    v-show="state.columns[column.attribute]"
    :key="key"
>
jsx
{resource.columns.map((column, key) => state.columns[column.attribute] && <th key={key}>{/*  */}</th>)}

Metadata

You may attach metadata to Actions and Links, Columns, Exports, and Filters. The default Table component ignores this metadata, but it's useful when building a custom Table component.