-
Notifications
You must be signed in to change notification settings - Fork 920
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
Filters on lists #175
Comments
This is a feature that's in development/planning However if you add your own UI code into the template, you can utilise the "addClause()" method described here -> https://laravel-backpack.readme.io/docs/crud-full-api#usage-level-2-often an example could be -> #83 |
Hi @alexgmin , There's no one-line way to do it right now. It's a know feature request and one of the 2 core features that are urgent (multi-language CRUDs, filters). I like how it's done in rapyd, thank you, we should take a closer look before we implement it ourselves and learn from it. For now, you can "fake" the filter functionality by adding new buttons on top of the form (next to the Add button). You can do that by adding buttons that, instead of being actual buttons, will be dropdowns or inputs. So something like this should work: public function setup()
{
// add a button whose HTML is in a view placed at resources\views\vendor\backpack\crud\buttons
$this->crud->addButtonFromView('top', 'featured_filter', 'featured_filter', 'end');
if (Input::get('featured_filter')) {
$this->crud->addClause(Input::get('featured_filter'));
}
} resources/views/vendor/backpack/crud/buttons/featured_filter.blade.php: <select name="featured_filter">
<option value="featured">Featured</option>
<option value="not_featured">Not Featured</option>
<option value="">Whatever</option>
</select>
<script>
jQuery(document).ready(function($) {
$("select[name=featured_filter]").change(function() {
window.location.href = "{{ url($crud->route) }}?featured_filter="+$(this).val();
});
});
</script> Please note I haven't tested the code above, it's more like pseudocode :-) |
Yeah, that should work for now. Thanks, @tabacitu . Backpack is awesome, btw, we love it. |
I think we could implement something very similar to how fields, buttons and columns work: filter types, each in their own blade file in $this->crud->addFilter([
'name' => 'featured',
'type' => 'dropdown',
'values' => [
'featured' => 'Featured',
'not_featured' => 'Not featured',
'' => 'Whatever',
],
], function($value) {
if ($value != '') {
$this->crud->addClause($value);
}
}); The filters would come with some pre-built logic (probably) and you can pass a closure as an optional second parameter to overwrite it with your own. What WOULD be very useful, and I hope it's ok if I hijack this thread for it, would be to make a list of all possible filter types. So that we know if this implementation is ok. //TODO Off the top of my head:
|
@mpixelz yep, that could be an option too. So 4 stacks should do:
|
@tabacitu I vaguely remember you saying you're working on this? is that still the case? |
Yup. WIll be done by Nov 17th, the latest. Currently my thinking is to have these field types: |
Currently my thinking is to first develop these most important filter types:
The general syntax would be: $this->crud->addFilter($options, $values, $filter_logic); I'll be releasing the select and select2 in about two weeks, and their syntax should be something like this: $this->crud->addFilter([ // select from enum column values
'name' => 'published',
'type' => 'select',
'label'=> 'Published'
],
function() {
return App\Models\Article::getPossibleEnumValues('published');
}
// , function($value) {} // optional filter logic overwrite; by default it makes sure that {name} == $value
);
$this->crud->addFilter([ // select_from_array
'name' => 'featured',
'type' => 'select_from_array',
], [ // values
'featured' => 'Featured',
'not_featured' => 'Not featured',
'' => 'Whatever',
]
// , function($value) {} // optional filter logic overwrite; by default it makes sure that {name} == $value
});
$this->crud->addFilter([ // select_from_array
'name' => 'featured',
'type' => 'select_from_array',
], function() {
// get all Users and create an associative array with ['id' => 'name']
return App\Models\User::all()->keyBy('id')->pluck('name');
}
// , function($value) {} // optional filter logic overwrite; by default it makes sure that {name} == $value
}); This would eliminate the need for a bunch of filter types for different purposes. What we'll do instead is give a number of examples, to match most common uses:
What do you guys think? |
Not sure if this is simplifying it too much, but could we utilise model scopes? for the actual filtering? maybe just define a scope, then internally handle the rest? what would the select from ajax do? would it not be better to just do that by default? additionally I've found https://github.com/dangrossman/bootstrap-daterangepicker to be good plugin for date ranges, specifically the "last 30 days" etc type fields. but apart from that all happy here :) |
Yup, you could move the filtering logic entirely into scopes with something like this: $this->crud->addFilter([ // select from enum column values
'name' => 'published',
'type' => 'select',
'label'=> 'Published'
],
function() {
return App\Models\Article::getPossibleEnumValues('published');
},
function($value) {
if ($value != '') {
$this->crud->addClause($value); // adds the name of the variable as a scope
}
}
); Oooor... we could use the third parameter as either a closure or string. If you pass a closure, it will run the closure. If you pass a string, it will run a pre-defined operation. We could use these strings (similar to the PHP comparison operators):
Not 100% sure this is achievable though. Definitely agree to dangrossman's daterangepicker. It's awesome. The select_from_ajax would load the select2 values with ajax. For when you have 1mil users and you want to filter the ones with the first_name==Owen. We can't do them all like this, because it would be too inconvenient to insert a filter then. In order to have an ajax filter you need to:
|
I like AngelList's top filters for their jobs search (https://angel.co/jobs): User always knows which filters are applied. Even with a lot of filters selected they don't take up too much space. There is also an option to save filters. Most filters have option to include or exclude values. Drop-downs for filters are optimized based on their values: Just an idea since you asked :-) Your design works too. Maybe for top/bottom the filters could be included in the same box as the table. Maybe below the top buttons. |
You didn't left us something to say. |
I'm done implementing the most important filters and will be launching the feature soon. Any feedback on the filter docs? https://laravel-backpack.readme.io/v3.0/docs/filters Cheers! |
Done, launched! Use it and abuse it :-) |
This type of filters

(This screenshot is from https://github.com/zofe/rapyd-laravel)
The code to do this on rapyd
What would be the best way to implement something like this on backpack?
Are there plans to do something similar?
The text was updated successfully, but these errors were encountered: