-
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
[4.1][Feature] New field type - Toggle Field Visibility #165
Conversation
@tabacitu and maybe this :) |
Hey @tabacitu - I've had a good few people recently asking about this feature, and as it's simple, wondering what the chances are of a merge for it soon? We're about to start another project and would be awesome to have a few more baked in :) |
Not sure how to propose this, don't know if I can make a PR over a PR, so I better comment it here, as it is a tiny change. @OwenMelbz , iCheck doesn't propagate "change" events, so if you use it to style your forms, it will break toggle: it will hide fields on initial load correctly, but it will not work on the "change/click" events. In order to enable it for iCheck as well, we can listen for both native and iCheck events (ifChecked), and that solves it: $('input[data-field-toggle]').on('change ifChecked', function(){ |
Hmm, is a tricky one! However, I would say we cannot cater for every plugin lol, otherwise we'd be never stopping! My solution would be something along the lines of
I dont know how icheck works, but imagine this $('input[radio]').iCheck({
onChange() {
this.trigger('change');
}
}); |
You are of course right, I only commented because of AdminLTE being part of Backpack, and including the plugin (and because is a non breaking change, of course!), just in case someone found the same problem if they use your toggle template, so they can find the comment :) |
Just as aditional information, I managed to get it working without the event using this, pretty self-explanatory: if ( $.isFunction($.fn.iCheck) ) {
$('input:not(.primary_list,.secondary_list)').on('ifChanged', function(event){
$(event.target).trigger('change');
}).iCheck({
checkboxClass: 'icheckbox_flat-blue',
radioClass: 'iradio_flat-blue',
});
} |
@OwenMelbz One thing that doesn't seem to work properly - a field that is toggled by multiple other fields. Basically, field A is a toggle. If a certain response is filled out, then show field B. B is also a toggle. B determines whether or not to show field C. Field A is configured like:
Field B is configured like:
Then I go through this process:
|
Cool feature, waiting for it to be merged! :) Edit: Can we make this a checkbox instead of radio button? Fixed Edit2 while editing toggle.blade.php var f = |
Would be good to find a better way: Laravel is using VueJS for the client's side rendering (by default). The lack of convergence between Backpack and VueJS will lead to a problem one day or another, and it is already frustrating. |
Integrating VueJS and Backpack would be a lot of work - I agree that Laravel seems to be going down the VueJS pathway but - and this is just my own opinion - I really don't trust Laravel to decide (next year, week, in Laravel 5.8) to choose some other shiny new JavaScript framework. (I know I'm sounding old and grumpy :P). |
what is coding for toggle view? |
@usmanhafeez147 - sorry I don't quite understand what you're asking in #165 (comment)? |
<!-- radio toggle --> | ||
@php | ||
$optionPointer = 0; | ||
$optionValue = old($field['name']) ? old($field['name']) : (isset($field['value']) ? $field['value'] : (isset($field['default']) ? $field['default'] : '' )); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ternaries in ternaries. Icck.
@if( isset($field['options']) && is_array($field['options']) ) | ||
|
||
@foreach ($field['options'] as $value => $label ) | ||
@php ($optionPointer++) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the $loop
variable supported in the versions of Blade we support? https://laravel.com/docs/5.4/blade#loops
@if( isset($field['inline']) && $field['inline'] ) | ||
|
||
<label class="radio-inline" for="{{$field['name']}}_{{$optionPointer}}"> | ||
<input data-field-toggle="{{ json_encode($field['hide_when']) }}" type="radio" id="{{$field['name']}}_{{$optionPointer}}" name="{{$field['name']}}" value="{{$value}}" {{$optionValue!=='' && $optionValue == $value ? ' checked': ''}}> {!! $label !!} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$optionValue !== ''
- notice the spaces (may as well be consistent with the following ==).
|
||
<div class="radio"> | ||
<label for="{{$field['name']}}_{{$optionPointer}}"> | ||
<input data-field-toggle="{{ json_encode($field['hide_when']) }}" type="radio" id="{{$field['name']}}_{{$optionPointer}}" name="{{$field['name']}}" value="{{$value}}" {{$optionValue!=='' && $optionValue == $value ? ' checked': ''}}> {!! $label !!} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above re spacing.
|
||
{{-- FIELD CSS - will be loaded in the after_styles section --}} | ||
@push('crud_fields_styles') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NL?
{{-- FIELD JS - will be loaded in the after_scripts section --}} | ||
@push('crud_fields_scripts') | ||
<script> | ||
jQuery(document).ready(function($){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we're spelling jQuery here but using $
in the function?
window.$hiddenFields = window.$hiddenFields || {}; | ||
|
||
var $toggle = function( $radio ){ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NL?
$hiddenFields[ $radioSet ] = $hiddenFields[ $radioSet ] || []; | ||
|
||
if( Object.keys($hiddenFields[ $radioSet ]).length ){ | ||
$.each($hiddenFields[ $radioSet ], function(idx, field){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I have to ask, everything has gotten the evil $ that isn't needed in JS, why not the function parameters? 👿
} | ||
|
||
if( typeof $hideWhen[ $value ] !== undefined ){ | ||
$.each($hideWhen[ $value ], function(idx, name){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my impish questin above :P
Ha just tried to use this, forgetting it didn't get merged. @tabacitu what was your opinion on all this, would it be something you could add soon? |
… field : select2_from_array (based on PR Laravel-Backpack#165 PR of @OwenMelbz)
Sorry @OwenMelbz - yes, I think you're right, I think it's about time we implement a solution for this problem. I'll test & review this PR shortly, along with #1257 and see which one to merge. I really need to test this carefully, and see if interacts ok with tabs & everything. |
It's been 84 years. |
@Tarpsvo I believe you missed out an extra 1, 184 years :D |
I copied the field to my project and it works like a charm - thank you for this! 👍 |
Wow this is probably one of the oldest PRs of yours that I haven't merged @OwenMelbz . 2016 - man, time passes fast. I've decided this doesn't make the cut as a core feature, but I'd love to have it as an add-on. So I've opened Laravel-Backpack/addons#11 in the hopes that maybe someone else likes this idea and wants to create an add-on and maintain it. If you @OwenMelbz don't want to, maybe someone else who's using it will. If so, let's move the conversation there, in Laravel-Backpack/addons#11 . Cheers! |
I was looking at Laravel-Backpack#165 by https://github.com/OwenMelbz and I wanted to update it to bring it into line with the current schema for Backpack. In the process, I realized all you need to do is add a few lines to the radio field. You can just add a parameter that this field can be a toggle. The rest of the usage is the same as in that PR. Additionally, one thing that bothered me is that you couldn't hide a field from two toggle fields. Now, I've added functionality that allows two toggle fields choose to hide another field, and then the only way it becomes visible if both toggles allow it to be visible *Usage* ``` //Controller->setUp() $this->crud->addField([ 'label' => 'Is this a featured article?', 'name' => 'featured', // can be a real db field, or unique name 'type' => 'toggle', 'options' => [ // same as radio, these act as the options, the key is the radio value 0 => 'Not Featured', 1 => 'Yes Featured' ], 'hide_when' => [ // these fields hide (by name) when the key matches the radio value 0 => ['featured_image', 'featured_title'], 1 => ['basic_title'] ], 'default' => 0 // which option to select by default ]); ```
Hello, Is this PR merged? is the |
@sandeepchowdary7 no - the feature is still in development. I've just pushed a PR with the most likely solution yesterday - you can see it here - #4312 |
Summary
Hides/Shows other form groups based on a radio selection
Preview

Usage