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

add applyDefaultSortingToTableQuery method to custom default sort behavior #6586

Merged
merged 2 commits into from May 22, 2023
Merged

add applyDefaultSortingToTableQuery method to custom default sort behavior #6586

merged 2 commits into from May 22, 2023

Conversation

debuqer
Copy link

@debuqer debuqer commented May 22, 2023

This feature allows developers to custom "default sort" behavior in Table builder

Usage:

  1. When you need a set of columns to be sorted by default
  2. When you need a calculated column to be sorted by default
  3. When you need a json object attribute to be sorted by default

Since all the logic just moved to a new method; it's not breaking any thing

Tested scenarios:

  1. Not given defaultSort, Not overrided applyDefaultSortingToTableQuery: Nothing have broken, no sort applied
  2. Given defaultSort, Not overrided applyDefaultSortingToTableQuery: sorted by price order by id
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('price'),
                Tables\Columns\TextColumn::make('count'),
                Tables\Columns\TextColumn::make('discount'),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\DeleteBulkAction::make(),
            ])->defaultSort('price', 'desc');
  1. Given defaultSort, Overrided applyDefaultSortingToTableQuery:
    protected function applyDefaultSortingToTableQuery(Builder $query, string $sortColumn, string $sortDirection): Builder
    {
        return $query->orderByRaw("$sortColumn * ( 100 - discount ) / 100 $sortDirection"); 
    }

sorted by the given formula

  1. Multiple sort columns
    protected function applyDefaultSortingToTableQuery(Builder $query, string $sortColumn, string $sortDirection): Builder
    {
        return $query->orderByRaw("price desc, discount asc");
    }
  1. Json sort
         ...
            ])->defaultSort("prices->'$.tax'", 'desc'); // not working when applyDefaultSortingToTableQuery is not overrided
    . . .
    protected function applyDefaultSortingToTableQuery(Builder $query, string $sortColumn, string $sortDirection): Builder
    {
         return $query->orderByRaw("$sortColumn $sortDirection");
    }

@zepfietje zepfietje added this to the v2 milestone May 22, 2023
@danharrin danharrin added the enhancement New feature or request label May 22, 2023
@danharrin danharrin merged commit b4efb37 into filamentphp:2.x May 22, 2023
10 of 11 checks passed
@danharrin
Copy link
Member

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants