[Feature Proposal] Conditional fields (aka. hide/show fields depending on other fields, aka. toggle field visibility)
See original GitHub issueThis is the most upvoted feature in our feature poll. So itās the first one we can tackle, now that we have v5 ready for launch.
Letās do this!!! šŖ
The Problem
Iāve studied all previous PRs for this:
- 2016 - https://github.com/Laravel-Backpack/CRUD/pull/165
- 2018 - https://github.com/Laravel-Backpack/CRUD/pull/1257
- 2020 - https://github.com/Laravel-Backpack/CRUD/pull/3420
- 2021 - https://github.com/Laravel-Backpack/CRUD/pull/3753
As I said in https://github.com/Laravel-Backpack/addons/issues/11 ā¦ this is a tricky problem to solveā¦ because the developer needs can be so very different. In various places, people ask:
- for the āsourceā to be a
select
, othersradio
,checkbox
,checklist
,select2_from_array
, etc; - for the āsourceā to be multiple fields, not just one;
- for the āactionā to be āhidingā;
- for the āactionā to be āshowingā;
- for the āactionā to be āhiding and disablingā;
- for the āactionā to be ācalculating the valueā;
Iām ashamed we havenāt done anything to solve this until now. Literal shame. Butā¦ the reason we havenāt merged any of thoseā¦ is that I didnāt see way for us to cover what most devs need, most of the time. We would only be covering a particular use case, which would most certainly have meant we would be adding features and niche use cases to it for years, as people report them. And that simply wasnāt a good idea, to take on such a big maintenance burdenā¦ when itās not that difficult to do it in JS, for the people who needed it. You could do it in create.js
and update.js
and you have a solution, for your project alone, with maximum customizability. Thatās why Iāve been suggesting ācreating an addon for itā to devs who have made PRs for thisā¦ because they were niche solutions, and for sure somebody would have used itā¦ but not everybody would have been happy with it.
I didnāt see how we can build this feature/field/whatever so that most people will be happy with it. But nowā¦ I think I do. And itās sort of what @pxpm suggested hereā¦ but without the AJAX calls, which I think are unproductive and bound to fail.
And thanks to the open-core splitā¦ we can start adding more and more feature, right into backpack/pro
.
The Solution
What ifā¦ instead of wanting to define the JS behavior in PHPā¦ we accept we have to write a little bit of JS for this? And have Backpack make that bit of JS so easy to write, itās a pleasure? In Backpack v5, thanks to the script
widget, we can do:
Widget::add()->type('script')->content(asset('/path/to/js/file.js'));
So what ifā¦
Step 1. We make it even easier, by providing a convenience method on CRUD, that also allows for inline content. Something like:
CRUD::addJavascript('/path/to/js/file.js');
// or even
CRUD::addJavascript("alert('what you want to be done here')");
Step 2. We make it dead-simple to write ALL of the combinations above, by providing a selector and a few actions on the crud
javascript object (itās already onpage, usually used for working with DataTables). So that what JS you actually write looks like this:
// option 1 - probably needs jQuery
crud.field('agree').on("change", function() {
crud.field('price').show();
});
crud.field('agree').onChange(function() {
crud.field('price').show();
});
// option 2 - maybe doesn't need jQuery
crud.field('agree').addEventListener("change", function() {
crud.field('price').show();
});
// of course, you should be able to do a few other stuff with your `fields`, but the minimum would be:
crud.field('price').show();
crud.field('price').hide();
crud.field('price').disable();
crud.field('price').enable();
crud.field('price').value(); // aka .setValue(), aka .val()
I believe this would solve all cases people have already expressedā¦ but also fix the cases people havenāt expressed yet. Which Iām sure will come up, in real apps, right after we introduce a feature with limited customizability. But by moving this logic to JSā¦ it opens up the possibility for you to doā¦ anything you want. Itās JS, and you have complete control.
Thoughts, anyone? Am I missing something? Or is this a solution we could all get behind? Iām eager to prototype this, to see if we can launch it with Backpack v5 this week š I have a feeling itās either a lot simpler than I expectā¦ or a lot more difficult š
Canāt tell if this idea is incredibly goodā¦ or incredibly stupid š Let me know.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:34 (22 by maintainers)
Top GitHub Comments
Hi everyone! Iāve done this in the past, using Javascript, I created a simple solution for what I needed at that time, take a look at the result;
https://user-images.githubusercontent.com/1838187/161063634-b3cc44fb-201e-471a-adea-7cee785f0425.mp4
You can show/hide based on
select
andinput[type="checkbox"]
.Until Backpack provides a solution for this, you can use this code. It uses
wrapperAttributes
to add attributes to fields (select, checboxs) so it hides and shows based on thatItās super easy to implement, here is the code;
public/assets/js/backpack-toggle.js
JavaScript
public/assets/css/backpack-toggle.css
CSS
app\Http\Controllers\Admin\EntityCrudController.php
PHP
The example video above uses the following code;
app\Http\Controllers\Admin\EntityCrudController.php
Example PHP
however, I have to do it using javascript for the time being. but when you guys put your plan into action?