Laravel Livewire Tables Documentation

🎉 Enjoying this package? Consider sponsoring me on GitHub or buying me a beer.

This is the documentation for v2 but the latest version is v3. You can switch versions in the menu at the top. Check your current version with the following command:

composer show rappasoft/laravel-livewire-tables

Available Methods

These are the available configuration methods on the datatable component.

General

setPrimaryKey

Set the primary key column of the component.

1public function configure(): void
2{
3 $this->setPrimaryKey('id');
4}

Attributes

setComponentWrapperAttributes

Set a list of attributes to override on the main wrapper of the component

1public function configure(): void
2{
3 $this->setComponentWrapperAttributes([
4 'id' => 'my-id',
5 'class' => 'this that',
6 ]);
7}

setTableWrapperAttributes

Set a list of attributes to override on the div that wraps the table

1public function configure(): void
2{
3 $this->setTableWrapperAttributes([
4 'id' => 'my-id',
5 'class' => 'this that',
6 ]);
7}

By default, this replaces the default classes on the table wrapper, if you would like to keep them, set the default flag to true.

1public function configure(): void
2{
3 $this->setTableWrapperAttributes([
4 'default' => true,
5 'class' => 'added these classes',
6 ]);
7}

setTableAttributes

Set a list of attributes to override on the table element

1public function configure(): void
2{
3 $this->setTableAttributes([
4 'id' => 'my-id',
5 'class' => 'this that',
6 ]);
7}

By default, this replaces the default classes on the table, if you would like to keep them, set the default flag to true.

1public function configure(): void
2{
3 $this->setTableAttributes([
4 'default' => true,
5 'class' => 'added these classes',
6 ]);
7}

setTheadAttributes

Set a list of attributes to override on the thead element

1public function configure(): void
2{
3 $this->setTheadAttributes([
4 'id' => 'my-id',
5 'class' => 'this that',
6 ]);
7}

By default, this replaces the default classes on the thead, if you would like to keep them, set the default flag to true.

1public function configure(): void
2{
3 $this->setTheadAttributes([
4 'default' => true,
5 'class' => 'added these classes',
6 ]);
7}

setTbodyAttributes

Set a list of attributes to override on the tbody element

1public function configure(): void
2{
3 $this->setTbodyAttributes([
4 'id' => 'my-id',
5 'class' => 'this that',
6 ]);
7}

By default, this replaces the default classes on the tbody, if you would like to keep them, set the default flag to true.

1public function configure(): void
2{
3 $this->setTbodyAttributes([
4 'default' => true,
5 'class' => 'added these classes',
6 ]);
7}

setThAttributes

Set a list of attributes to override on the th elements

1public function configure(): void
2{
3 // Takes a callback that gives you the current column.
4 $this->setThAttributes(function(Column $column) {
5 if ($column->isField('name')) {
6 return [
7 'class' => 'bg-green-500',
8 ];
9 }
10 
11 return [];
12 });
13}

By default, this replaces the default classes on the th, if you would like to keep them, set the default flag to true.

1public function configure(): void
2{
3 $this->setThAttributes(function(Column $column) {
4 if ($column->isField('name')) {
5 return [
6 'default' => true,
7 'class' => 'bg-green-500',
8 ];
9 }
10 
11 return ['default' => true];
12 });
13}

setThSortButtonAttributes

Set a list of attributes to override on the th sort button elements

1public function configure(): void
2{
3 // Takes a callback that gives you the current column.
4 $this->setThSortButtonAttributes(function(Column $column) {
5 if ($column->isField('name')) {
6 return [
7 'class' => 'bg-green-500',
8 ];
9 }
10 
11 return [];
12 });
13}

setTrAttributes

Set a list of attributes to override on the tr elements

1public function configure(): void
2{
3 // Takes a callback that gives you the current row and its index
4 $this->setTrAttributes(function($row, $index) {
5 if ($index % 2 === 0) {
6 return [
7 'class' => 'bg-gray-200',
8 ];
9 }
10 
11 return [];
12 });
13}

By default, this replaces the default classes on the tr, if you would like to keep them, set the default flag to true.

1public function configure(): void
2{
3 $this->setTrAttributes(function($row, $index) {
4 if ($index % 2 === 0) {
5 return [
6 'default' => true,
7 'class' => 'bg-gray-200',
8 ];
9 }
10 
11 return ['default' => true];
12 });
13}

setTdAttributes

Set a list of attributes to override on the td elements

1public function configure(): void
2{
3 // Takes a callback that gives you the current column, row, column index, and row index
4 $this->setTdAttributes(function(Column $column, $row, $columnIndex, $rowIndex) {
5 if ($column->isField('total') && $row->total < 1000) {
6 return [
7 'class' => 'bg-red-500 text-white',
8 ];
9 }
10 
11 return [];
12 });
13}

By default, this replaces the default classes on the td, if you would like to keep them, set the default flag to true.

1public function configure(): void
2{
3 // Takes a callback that gives you the current column, row, column index, and row index
4 $this->setTdAttributes(function(Column $column, $row, $columnIndex, $rowIndex) {
5 if ($column->isField('total') && $row->total < 1000) {
6 return [
7 'default' => true,
8 'class' => 'bg-red-500 text-white',
9 ];
10 }
11 
12 return ['default' => true];
13 });
14}

Offline

Offline indicator is enabled by default, but if you ever needed to toggle it you can use the following methods:

Enable/disable the offline indicator.

setOfflineIndicatorStatus

1public function configure(): void
2{
3 $this->setOfflineIndicatorStatus(true);
4 $this->setOfflineIndicatorStatus(false);
5}

setOfflineIndicatorEnabled

Enable the offline indicator.

1public function configure(): void
2{
3 // Shorthand for $this->setOfflineIndicatorStatus(true)
4 $this->setOfflineIndicatorEnabled();
5}

setOfflineIndicatorDisabled

Disable the offline indicator.

1public function configure(): void
2{
3 // Shorthand for $this->setOfflineIndicatorStatus(false)
4 $this->setOfflineIndicatorDisabled();
5}

Query String

The query string is enabled by default, but if you ever needed to toggle it you can use the following methods:

setQueryStringStatus

Enable/disable the query string.

1public function configure(): void
2{
3 $this->setQueryStringStatus(true);
4 $this->setQueryStringStatus(false);
5}

setQueryStringEnabled

Enable the query string.

1public function configure(): void
2{
3 // Shorthand for $this->setQueryStringStatus(true)
4 $this->setQueryStringEnabled();
5}

setQueryStringDisabled

Disable the query string.

1public function configure(): void
2{
3 // Shorthand for $this->setQueryStringStatus(false)
4 $this->setQueryStringDisabled();
5}

Relationships

Disabled by default, enable to eager load relationships for all columns in the component.

setEagerLoadAllRelationsStatus

Enable/disable column relationship eager loading.

1public function configure(): void
2{
3 $this->setEagerLoadAllRelationsStatus(true);
4 $this->setEagerLoadAllRelationsStatus(false);
5}

setEagerLoadAllRelationsEnabled

Enable column relationship eager loading.

1public function configure(): void
2{
3 // Shorthand for $this->setEagerLoadAllRelationsStatus(true)
4 $this->setEagerLoadAllRelationsEnabled();
5}

setEagerLoadAllRelationsDisabled

Disable column relationship eager loading.

1public function configure(): void
2{
3 // Shorthand for $this->setEagerLoadAllRelationsStatus(false)
4 $this->setEagerLoadAllRelationsDisabled();
5}

Builder

setAdditionalSelects

By default the only columns defined in the select statement are the ones defined via columns. If you need to define additional selects that you don't have a column for you may:

1public function configure(): void
2{
3 $this->setAdditionalSelects(['users.id as id']);
4}

Since you probably won't have an ID column defined, the ID will not be available on the model to use. In the case of an actions column where you have buttons specific to the row, you probably need that, so you can add the select statement to make it available on the model.

Misc.

setEmptyMessage

Set the message displayed when the table is filtered but there are no results to show.

Defaults to: "No items found. Try to broaden your search."

1public function configure(): void
2{
3 $this->setEmptyMessage('No results found');
4}