This is yet another laravel livewire table and come as is. You can filter, you can sort, you can bulk, toggle columns, the basics. The data input is a collection/array, we cant handle models.
Consider using other packages like the ones that heavily inspired this one (rappasoft livewire tables/powergrid tables) for better performance and more and better features.
- Laravel 10/11
- Livewire 3
composer require fantismic/yet-another-table
You may want to let the user save the table arrange of columns. If so, publish the migrations...
php artisan vendor:publish --provider="Fantismic\\YetAnotherTable\\YATProvider" --tag="migrations"
...and run them.
php artisan migrate
That's it. You are ready to go.
You may want to publish the lang settings and change all the 6 keys. Up to you.
php artisan vendor:publish --provider="Fantismic\\YetAnotherTable\\YATProvider" --tag="lang"
To create a table you can run the following:
php artisan make:yatable MyBrandNewTable
This is nothing more than a livewire component, so the rendering is just the same:
Add the layout attribute right before the class with your layout:
#[Layout('layouts.app')]
class MyBrandNewTable extends YATBaseTable
<livewire:mybrandnewtable />
Livewire docs to render component
Here you can set some settings to the table in general: title, column id, bulk, state handler, etc.
Here you return the data. As is, a collection of arrays or objects, you return it we display it. When you create a table an example of data is in the stub in order to see what is expected.
Here you return an array of column instances with the modifiers you want.
Here you return an array of filter instances.
Here you return an array with function => label form.
This will generate an "Options" button in the view that will display a dropdown with the options you set and they will call the function established as key.
To pass data to the component, either from a route or from an attribute in the <livewire>
tag, you only need to declare it as a public property in the table component.
<livewire:mytable color_type="primary">
class MyTable extends YATBaseTable
{
public $color_type;
...
If you need to use the mount()
function in the table component, be sure to call the parent mount()
order to avoid errors.
public function mount() {
parent::mount();
mySoNeededMountFunction();
}
- Set table title
- Override title classes
- Set a custom header
- Set component classes
- Add table classes
- Set table classes
- Set sticky header
- Table spinner
- Table spinner view
- Modals View
- Styling
- Custom Data
- Hidding columns
- Hidding columns from selector
- Hide column by default
- Boolean columns
- Link columns
setTitle(string $title)
By default the component shows only the table. Use this to display a title.
$this->setTitle('My brand new table');
overrideTitleClasses(string $classes)
The title is displayed in a single div, if you want to change the default styling you can use this function to override the default classes and apply your own.
Tailwind classes only
$this->overrideTitleClasses('text-3xl text-red-700 dark:text-red-300');
setCustomHeader(string $html)
If you want, you can add your own html in the header in order to replace the title at all or add some stuff.
This will render at the top of the component any html you pass.
$this->setCustomHeader('
<div class="flex justify-between">
<div class="text-2xl font-bold">My brand new table</div>
<div><button href="/">Go back</button></div>
</div>
');
setComponentClasses(string $classes)
Here you can set classes for the component wrapper, this includes the buttons, the pagination, the title, etc. Its the whole view.
Tailwind classes only
$this->setComponentClasses('bg-black p-4');
addTableClasses(string $classes)
Here you can add classes to the table wrapper. Only the table, buttons, pagination, title, etc are outside this wrapper.
Tailwind classes only
$this->addTableClasses('text-xl');
setTableClasses(string $classes)
Same as addTableClasses
, but instead of adding classes to the wrapper you override the defaults leaveing only what you pass to the function.
Tailwind classes only
$this->setTableClasses('max-h-64 md:max-h-80 lg:max-h-[30rem] overflow-y-scroll');
setStickyHeader()
Disabled by defualt, enable this to stick the table header, useful when disabled pagination with a scrolling table.
$this->setStickyHeader();
useTableSpinner(bool $bool)
Enabled by default, set this to false
to disable the loading spinner.
$this->useTableSpinner();
setTableSpinnerView(string $view)
If useTableSpinner
is activated, you can use your own blade to display the spinner/message you want when loading.
$this->setTableSpinnerView('myviews.myspinner');
setModalsView(string $view)
This adds the passed view at the bottom of the component, useful for modals.
$this->setModalsView('myviews.mymodals');
setSortColumn(string $column)
By default data is displayed as is, you can set a default sort by a column.
$this->setSortColumn('createtime');
setSortDirectionAsc(bool $boolean)
If setSortColumn is set, by default the order will be 'asc', but you can enforce that if necessary.
$this->setSortDirectionAsc(true);
setSortDirectionDesc(bool $boolean)
If setSortColumn is set, you can set the direction 'desc' by default.
$this->setSortDirectionDesc(true);
setSearchLabel(string $title)
By default the global search placeholder is "Search", you can change it for whatever you want here.
$this->setSearchLabel('Find');
hasBulk(bool $boolean)
Disabled by default. Set hasBulk to true to enable the functionality. This will use the 'id' column to identify the row, if there is no 'id' column in your data you must set which column to use with setColumnID
Bulk functionality can be combined with Options
getSelectedRows()
This will return an array of ids of the current selected options.
$ids = $this->getSelectedWRows();
removeRowFromTable(string $id, ?bool $resetSelected = true)
Remove a row by its id from the table.
By default this function reset the selected rows, if you dont want that pass a
false
as a second parameter.
$this->removeRowFromTable(3);
useStateHandler(bool $boolean)
In order to enable this feature you need to publish and run the migration of the package.
Disabled by default. When enabled in column toggle button the user can save the current set of column's visibility. Each time the component is reloaded the columns will be visibile according to user election.
This functionality distincts users and tables, you can have many to many, each will get their custom visualization per table.
$this->useStateHandler(true);
usePagination(bool $bool)
Enabled by default, set this to false
to disable pagination.
setPerPageDefault(int $number)
By default 10 rows are displayed. Change this to show as many as you want per page.
$this->setPerPageDefault(25);
setPerPageOptions(int $number)
Default value: ["10", "15", "25", "50"].
$this->setPerPageOptions(["10","20","50","100"]);
setColumnID(string $column)
By default 'id' is the assumed column for identifying a row in order to use bulk functions. If you want to set another column of your data you can do that using this.
$this->setColumnID('ticket');
showColumnToggle(bool $bool)
Enabled by default, you can disable this button and remove it from view.
$this->showColumnToggle(false);
Column::make(string label, ?string $field)
In order to render the data the way you need it, you have to create a "columns" function returning an array of any of the Column objects we provide.
public function columns(): array {
return [
Column::make('Name','name')
->styling('text-lg font-bold')
->hideFromSelector(true),
Column::make('Hex Code','hex'),
Column::make('Color')
->customData(function($row, $value){
return '<span style="color:'.$row['hex'].'">██████████████</span>';
})
->toHtml(),
LinkColumn::make('Google it','name')
->href(function($row, $value){
return "https://www.google.com/search?q=".$value;
})
->text('Google'),
BoolColumn::make('Primary','isPrimary')
];
}
The Column class provides several modifiers to customize your table:
styling(string $classes)
You can pass classes to add to all <td>
in the column. The classes will be prefixed with !
in order to override other conflicting classes.
Column::make('Big text column','bigtext')
->styling('text-7xl')
customData(Closure $function)
To customize the data of the column you can pass any html in a closure function with $row
and $value
variable available.
Column::make('Upper Name','name')
->customData(function($row, $value){
return strtoupper($value);
})
Column::make('Full Name')
->customData(function($row, $value){
return $row['firstname'] . " " . $row['lastname'];
})
Tip: if you pass html, you need to add the ->toHtml() modifier
Column::make('Big Name','name')
->customData(function($row, $value){
return '<div class="text-3xl">'.$value.'</div>';
})->toHtml()
hideWhen(bool $bool)
Hides the column from the view. When true
the user cannot access this column at all.
Column::make('Only Admins','sensitive-row')
->hideWhen(Auth::user()->isAdmin)
hideFromSelector(bool $bool)
Hides the column from the toggle dropdown.
Column::make('Always visible','force-data')
->hideFromSelector(true)
isVisible(bool $bool)
By default all columns are visible. IF you set this to true
the table will render with this column hidden, but the user can display it any time with the column toggle switch.
Column::make('Not so important data','meh-column')
->isVisible(false)
BoolColumn::make(string $label, string $field)
You can use this column to boolean values, they will render in table view as ✔️ and ❌.
You can set what is considered true
for a strict comparison with the trueIs()
.
Also, you can customize the label/icons to appear in each case with trueLabel()
and falseLabel()
BoolColumn::make('User status','isActive')
->trueIs('enabled')
->trueLabel('User Active')
->falseLabel('<i class="icon"></i>')
LinkColumn::make(string $label, ?string $field)
Link column takes an destination and generates a link with the closure function.
LinkColumn::make('Edit user','id')
->text('Edit')
->href(function($row, $value){
return '/edit/'.$value;
})
[Filter]::make(string label, ?string $column)
In addition to the search input that filters globally on the table, you can add filters by column.
Note: if you pass only the label parameter, the filter will search for a column with same label and match, if you want to specify a column use the second parameter to assign the filter to the column label you want.
To enable filters, you need to create a "filters" function that returns an array of any of the Filter objects we provide.
public function filters(): array {
return [
FilterString::make('name'),
FilterDateRange::make('Created At')
];
}
FilterString::make(string $label, ?string $column)
This filter will search in column with label $label
and filter the table accordingly.
FilterString::make('name') # This will search in column with label 'name'
FilterString::make('Edad', 'age') # This will search in column with label 'age'
FilterDateRange::make(string $label, ?string $column)
This filter uses flatpickr to display a date range select and filter the table based on user input.
FilterDateRange::make('created_at') # This will search in column with label 'name'
FilterSelect::make(string $label, array $options)
This filter displays a select
with the options given.
FilterSelect::make('type',["primary","secondary"])
FilterSelectMagic::make(string $label, array $options)
Same as select filter, but with magic. This filter with get all values from the column given, make the unique and display them.
[
["id"=>1, "color"=>"red", "type"=>"Primary"],
["id"=>2, "color"=>"violet", "type"=>"Secondary"],
["id"=>2, "color"=>"green", "type"=>"Primary"],
]
FilterSelectMagic::make('type')
# Will display Primary and Secondary in the <select>
FilterBool::make(string $label, ?array $compared_with = null, ?string $index = null)
This filter displays a toggle
to make boolean comparisons.
By default it will compare true
and false
, but you can pass an array ["true" => "My True value", "false" => "My false value"]
and it will compare to that.
FilterBool::make('isprimary')
You can add an additional button next to the column toggle to display a list of custom options/actions. This options/actions will 'wire:click' the key given.
public function options(): array {
return [
'export' => 'Export selected rows',
'remove' => 'Delete selected rows',
];
}
public function remove() {
foreach ($this->getSelectedRows() as $id) {
$this->removeRowFromTable($id);
}
}
In this example, when user clicks on 'Delete selected rows' the function 'remove' will be called.
Make sure you implement this functions in your component in order to avoid errors.
You have three methods in order to handle the data to export:
$this->getAllData() # Will return collection of all data
$this->getAfterFiltersData() # Will return collection of filtered data (global search and custom filters)
$this->getSelectedData() # Will return collection of data by selected rows
If you have Laravel Excel the stub comes with three functions added to the Options dropdown in order to export the data by this criterias that will work out of the box.
If you have other ways to expor the data or simply dont want to export at all feel free to delete the functions in the component.