Skip to content

Custom Table Component

Introduction

If you want to use this package in a headless way, you can totally discard the default Table component and create your own while still using all the features of the package. This way you can create a table that fits your design perfectly. It's also great for creating other components like a list or a grid view, and still use the filtering and sorting logic.

Creating a Custom Table

To create a custom table, you need to create a new React or Vue component and import the useActions and useTable composable functions from the inertiaui/table package. You can then use the useTable function to interact with the table state and the useActions function to interact with the Row Actions and Bulk Actions.

Please note that the React and Vue versions slightly differ.

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

How to implement the Custom Table

Now that you have the useActions and useTable composable functions and the resource object, you can start implementing the template of your custom table. It is highly recommended to study the default Table component to see how the table is structured and how the features are implemented.

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 can add metadata to Actions and Links, Columns, Exports, and Filters. This metadata is not used in the default Table component but can be useful when building a custom Table component.