Skip to content

Add ability to use Closure as a sortable parameter in table's column #353

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

Merged
merged 2 commits into from
Mar 30, 2023

Conversation

Jampire
Copy link
Contributor

@Jampire Jampire commented Mar 14, 2023

Hi
This PR makes possible to use self-defined Closure as sortable parameter for table's column.

There can be a case when you need to sort by computed or virtual column. You can't just set sortable: true in call to column() method. You can use Spatie Query Builder for such case. But this solution for those who don't want to install it just for one field.

So, let's say you have such Eloquent model:

<?php

class Subscriber extends Model
{
    protected $appends = [
        'full_name',
    ];

    protected $fillable = [
        'first_name',
        'last_name',
    ];

    public function fullName(): Attribute
    {
        return Attribute::make(
            get: fn () => trim(sprintf('%s %s', $this->first_name, $this->last_name)),
        );
    }
}

And you want to sort by full_name. Then you can do:

<?php

class SubscribersViewTable extends AbstractTable
{
    /**
     * Determine if the user is authorized to perform bulk actions and exports.
     *
     * @param Request $request
     * @return bool
     */
    public function authorize(Request $request): bool
    {
        return true;
    }

    /**
     * The resource or query builder.
     *
     */
    public function for(): Builder
    {
        return Subscriber::query();
    }

    /**
     * Configure the given SpladeTable.
     *
     * @param SpladeTable $table
     * @return void
     */
    public function configure(SpladeTable $table): void
    {
        $table
            ->withGlobalSearch(columns: [
                'first_name',
                'last_name',
            ])
            ->column(
                key: 'full_name',
                sortable: fn (Builder $query, string $direction) => $query
                    ->orderBy('first_name', $direction)
                    ->orderBy('last_name', $direction),
            )
            ->searchInput(
                key: ['first_name', 'last_name'],
                label: 'Full Name',
            )
            ->paginate(10)
        ;
    }
}

P.S. I don't find instructions on how to set-up tests. So, right now there are no tests of this functionality.

@pascalbaljet
Copy link
Contributor

Very cool! Thank you :)

@pascalbaljet pascalbaljet merged commit 4abed07 into protonemedia:main Mar 30, 2023
@Jampire Jampire deleted the sortable_closure branch April 2, 2023 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants