Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Anonymous Column documents to reference ability to use strings #1588

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
- Internal - modify GitHub workflows to improve caching, but use unique caches per workflow matrix
- Internal - remove superfluous PHPStan ignoreErrors
- Internal - update Test Suite to also test at PHP 8.3
- Docs - Update Anonymous Column documents to reference ability to use strings as well as views

## [v3.1.4] - 2023-12-04
### New Features
Expand Down
53 changes: 50 additions & 3 deletions docs/columns/anonymous_columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,59 @@ Though, it doesn't have to be an action column, it could be anything.
By using an anonymous column, you take full control by using your own view component. So if you find the LinkColumn,
ImageColumn, or any of the other columns too restrictive for your needs, then an anonymous column may be what you need.

To make an anonymous column, you create an anonymous function that returns a view into the `label()` method, which will
To make an anonymous column, you create an anonymous function that returns a string or a view into the `label()` method, which will
remove the requirement for a database column. Thus, making it "anonymous". You can also pass variables to the view by
chaining the `with()` method onto the `view()` method that gets returned by the anonymous function into the `label()`.
So you can either pass specific values, or the whole row itself. Lastly, chain the `html()` method to the column so it
So you can either pass specific values, or the whole row itself.

Lastly, you may chain the `html()` method to the column so it
can render your view component as html.

## Example Action Column
## Example Column Using a String
Here is an example of using a label to return the "Full Name" of a User. Note that as we are using a label(), you must add any related fields into the setAdditionalSelects() method in the configure() method.

In this case, we are adding the "forename" and "surname" field from the database to the computed set of selects, then in the label, we are combining those, while capitalising the first letter of each name. There are of course better methods to achieve this use case, however this is just an example of the functionality.

In your `DataTableComponent`:

```php
<?php

namespace App\Livewire;

use App\Models\User;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;

class UserTable extends DataTableComponent
{
protected $model = User::class;

public function configure(): void
{
$this->setPrimaryKey('id')
->setDefaultSort('id', 'asc')
->setAdditionalSelects(['users.forename as forename', 'users.surname as surname']);
}

public function columns(): array
{
return [
Column::make('id', 'id')
->sortable()
->searchable(),
Column::make('Email', 'email')
->sortable()
->searchable(),
Column::make('Full Name')
->label(fn ($row, Column $column) => ucwords($row->forename ?? '' . ' ' . $row->surname)),

];
}
}
```

## Example Action Column Using Views

Here is an example of an action column using FontAwesome icons for the "view", "edit", and "delete" actions.

Expand Down Expand Up @@ -130,3 +176,4 @@ Or, if you passed the whole record, you could use:
The final result can look something like this:

<img width="1104" alt="users-table-action-column" src="https://github.com/rappasoft/laravel-livewire-tables/assets/9557392/b0432731-c882-45bb-8c53-54a3f485f9a3">