Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

OwenMelbz
Copy link
Contributor

@OwenMelbz OwenMelbz commented Oct 5, 2016

Summary

Hides/Shows other form groups based on a radio selection

Preview
backpack - toggle

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
]);

@OwenMelbz
Copy link
Contributor Author

@tabacitu and maybe this :)

@OwenMelbz
Copy link
Contributor Author

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 :)

@MarcosBL
Copy link
Contributor

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(){

image

image

@OwenMelbz
Copy link
Contributor Author

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

  • If you don't mind editing the view and adding in the event, thats fine, otherwise you should make your iCheck plugin or any other plugin fire the change event from their plugin.

I dont know how icheck works, but imagine this

$('input[radio]').iCheck({
    onChange() {
        this.trigger('change');
    }
});

@MarcosBL
Copy link
Contributor

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 :)

@MarcosBL
Copy link
Contributor

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',
	});
}

@jsiebach
Copy link

@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:

'hide_when' => [
  0 => ['B', 'C'],
  1 => []
]

Field B is configured like:

'hide_when' => [
  0 => ['C'],
  1 => []
]

Then I go through this process:

  1. Select "1" for "A"
  • B and C appear
  1. Select "0" for "B"
  • C disappears
  1. Toggle "A" to "0" and then back to "1"
  • C reappears, even though it should still be hidden due to it's dependency on B

@eyemiru
Copy link

eyemiru commented Apr 19, 2017

Cool feature, waiting for it to be merged! :)

Edit: Can we make this a checkbox instead of radio button?
Edit2: Seems like it won't work for other data types like "date_range".

Fixed Edit2 while editing toggle.blade.php

var f = $('[name="'+name+'"]').parents('.form-group'); -> var f = $('.'+name+'');

@jrpub
Copy link
Contributor

jrpub commented May 24, 2017

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.
When I see the code proposed I don't think it is a good idea to merge it.

@lloy0076
Copy link
Contributor

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).

@usmanhafeez147
Copy link

what is coding for toggle view?

@lloy0076
Copy link
Contributor

@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'] : '' ));
Copy link
Contributor

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++)
Copy link
Contributor

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 !!}
Copy link
Contributor

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 !!}
Copy link
Contributor

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')

Copy link
Contributor

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($){
Copy link
Contributor

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 ){

Copy link
Contributor

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){
Copy link
Contributor

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){
Copy link
Contributor

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

@OwenMelbz
Copy link
Contributor Author

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?

@tabacitu
Copy link
Member

tabacitu commented Mar 9, 2018

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.

@Tarpsvo
Copy link

Tarpsvo commented Feb 21, 2019

It's been 84 years.

@OwenMelbz
Copy link
Contributor Author

@Tarpsvo I believe you missed out an extra 1, 184 years :D

@Tarpsvo
Copy link

Tarpsvo commented Feb 25, 2019

I copied the field to my project and it works like a charm - thank you for this! 👍

@tabacitu tabacitu changed the title [Feature] Input - Toggle Field Visibility [4.0][Feature] New field type - Toggle Field Visibility Aug 17, 2019
@tabacitu tabacitu changed the title [4.0][Feature] New field type - Toggle Field Visibility [4.1][Feature] New field type - Toggle Field Visibility Aug 20, 2019
This was referenced Apr 2, 2020
@tabacitu
Copy link
Member

tabacitu commented Jul 9, 2020

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!

@tabacitu tabacitu closed this Jul 9, 2020
@tabacitu tabacitu deleted the feature-input-toggle-fields branch July 9, 2020 07:31
zachweix added a commit to zachweix/CRUD that referenced this pull request Jun 14, 2021
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
]);
```
@sandeepchowdary7
Copy link

Hello,

Is this PR merged? is the toggle type available in backpack 4.1 V??

@tabacitu
Copy link
Member

tabacitu commented Apr 6, 2022

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.