laravel-admin icon indicating copy to clipboard operation
laravel-admin copied to clipboard

create a default filter and resetting when user passes in filters

Open kevinohashi opened this issue 2 years ago • 3 comments

  • Laravel Version: 8.49.0
  • PHP Version: 7.4.3
  • Laravel-admin: 1.8.11

Description:

I haven't been able to find a way to create default filters that get reset when a user enters their own filter criteria.

I think it's similar to this issue which never was resolved https://github.com/z-song/laravel-admin/issues/2432

The goal is setting a default filter when no filters are being passed.

So on new load of the admin page, nothing has been filtered yet, use these default filters. But once a user starts putting their own filters in, those conditions don't apply anymore.

The problem I've run into using the suggested solution $grid->model()->where() is that that condition doesn't even reset. So if a user filters by something that conflicts with it, no results are ever returned.

For example,

$grid->model()->where('id','>',100) //we default to showing all results with id > 100.

use filters for id = 50 //no results are returned because we're sending a query that is select * from table where id > 100 and id = 50. This condition can never be true.

So the goal would be use id > 100 when no other filter is present. Then that condition is not used if we pass in any user defined filters. So If a user does id = 50, that is the only filter being applied and id > 100 no longer does until we reset to empty/default state.

Steps To Reproduce:

kevinohashi avatar Jul 14 '21 11:07 kevinohashi

        if (empty($_GET)||(count($_GET) ==1 && isset($_GET['_pjax']))) {
            $grid->model()->whereRaw("id > 100");
        }

Yanghsuanming avatar Aug 14 '21 05:08 Yanghsuanming

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 03 '22 02:03 stale[bot]

I similarly achieved:

$input = request()->all(); if(empty($input)){ admin_info('Please set filters to Search grid'); $grid->model()->whereNull('ID'); }

On Wed, Mar 2, 2022 at 9:18 PM stale[bot] @.***> wrote:

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

— Reply to this email directly, view it on GitHub https://github.com/z-song/laravel-admin/issues/5362#issuecomment-1057597506, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALF5SJDXUSQKTAIY65V7H7LU6AOPLANCNFSM5ALHWGHQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

axsweet avatar Mar 03 '22 21:03 axsweet