Crud: Filter 'active' by default?

Created on 13 Feb 2017  路  11Comments  路  Source: Laravel-Backpack/CRUD

I have searched docs, code and Google. Can't seem to see if this is currently possible, if not perhaps it could be a new feature?

I would like to load the 'list' crud view, with a filter enabled by default. In my use case the users only really care about active entries, I have given them an active filter, but it's confusing to some users to have to enable it every time.

Alternativley if there was a way to remove certain rows from the list view entirely, that would also suffice.

Thanks

Most helpful comment

@tabacitu if some filter logic is applied every time, I suppose the corresponding filter should be renamed to tell the user when it's "turned on" its deactivating some filtering..

Your example thought is kind of useless, ass this is what can be achieved directly via addClause.

See my example down here. I implement a filter to show "only inactive items" and by default I show only active Items.
That' the way we use this.

$this->crud->addFilter([ // add a "simple" filter called Draft
          'type'  => 'simple',
          'name'  => 'checkbox',
          'label' => 'Show inactive Items',
        ],
        false, // the simple filter has no values, just the "Draft" label specified above
        function () { // if the filter is active (the GET parameter "checkbox" exits)
            $this->crud->addClause('where', 'active', '0');
        },
        function () { // if the filter is NOT active (the GET parameter "checkbox" does not exit)
            $this->crud->addClause('where', 'active', '1');
        });

All 11 comments

I just have my navigation/sidebar link directly to the filter, seems easiest :D

Otherwise you can play with model scopes maybe

Sounds simple! What am I missing? I have AJAX pagination enabled so I don't appear to have a direct URL to the filter.

I have a query scope setup for the filter on the model. But I can't see where I would limit the initial :list view by that scope?

Sorry if I am being super dumb. Wouldnt be the first time!

You can use ?parameter=key

Hi @jdfx ,

I think the fastest way would be the one they suggested:

  • enter the page
  • activate the filters that you want active
  • copy the GET parameters and place them in your sidebar menu item

Result: whenever someone clicks on the sidebar item, they go directly to the filtered list.

Cheers!

this is not working with the ajax data tables. anyway to do it?

@remipou - it should work with the current Ajax implementation too. Try yourapp/admin/yourentity?filtername=value. Just tried the demo with http://demo/admin/monster?checkbox=true and it worked for me.

There is a second closure when adding an filter via $this->crud->addFilter(..) which is the if filter inactive closure. this would be the best position to apply a default filter i suppose, as you don't need to add the Get parameter everywhere :)

That's an excellent point, @OliverZiegler - very smart use. Something like this would make the filter active ALL THE TIME:

        $this->crud->addFilter([ // add a "simple" filter called Draft
          'type'  => 'simple',
          'name'  => 'checkbox',
          'label' => 'Simple',
        ],
        false, // the simple filter has no values, just the "Draft" label specified above
        function () { // if the filter is active (the GET parameter "checkbox" exits)
            $this->crud->addClause('where', 'checkbox', '1');
        },
        function () { // if the filter is NOT active (the GET parameter "checkbox" does not exit)
            $this->crud->addClause('where', 'checkbox', '1');
            $this->crud->request->request->add(['checkbox' => 1]); // to make the filter look active
        });

The problem with both solutions is that, upon click, the filter will NOT get deactivated, unfortunately. Hmm... I can't see a way around this, tbh...

@tabacitu if some filter logic is applied every time, I suppose the corresponding filter should be renamed to tell the user when it's "turned on" its deactivating some filtering..

Your example thought is kind of useless, ass this is what can be achieved directly via addClause.

See my example down here. I implement a filter to show "only inactive items" and by default I show only active Items.
That' the way we use this.

$this->crud->addFilter([ // add a "simple" filter called Draft
          'type'  => 'simple',
          'name'  => 'checkbox',
          'label' => 'Show inactive Items',
        ],
        false, // the simple filter has no values, just the "Draft" label specified above
        function () { // if the filter is active (the GET parameter "checkbox" exits)
            $this->crud->addClause('where', 'active', '0');
        },
        function () { // if the filter is NOT active (the GET parameter "checkbox" does not exit)
            $this->crud->addClause('where', 'active', '1');
        });

@OliverZiegler yup, I agree with you. The best solution here is probably semantics :-) Renaming the filter :-)

thanks @OliverZiegler. very useful

Was this page helpful?
0 / 5 - 0 ratings