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

Group Radio Input Values #263

Closed
f-o-n opened this issue Mar 24, 2015 · 4 comments
Closed

Group Radio Input Values #263

f-o-n opened this issue Mar 24, 2015 · 4 comments

Comments

@f-o-n
Copy link

f-o-n commented Mar 24, 2015

Hi,
something about groups again:
I have the issue that the newly dynamically generated group rows don't have any values assigned to the radio inputs.
The values only appear if I save my post. I use the radio input to show/hide other fields and consistent radio input values would simplify this task greatly.

Awesome plugin btw. Much obliged.

@jtsternberg
Copy link
Member

Do you have a screenshot and/or code? Help us help you. :)

@f-o-n
Copy link
Author

f-o-n commented Apr 5, 2015

Yes sure, here are some snippets that I tested in a fresh WordPress install.

I have a Post Type to create Slideshows. I created a checkbox so the user can easily switch between Images and Videos so he doesn't has both options. The code works if I press save once after creating the repeatable groups and the input gets an value assigned.

Before saving the post:
presave

After saving the post:
postsave

Here is my MetaBox code:

function my_gallery_group_metabox() {
    $prefix = '_my_gallery_';

    $cmb_group = new_cmb2_box( array(
        'id'           => $prefix . 'group_metabox',
        'title'        => __( 'Gallery Slides', 'my-gallery' ),
        'object_types' => array('gallery'),
    ) );

    $group_field_id = $cmb_group->add_field( array(
        'id'          => $prefix . 'group',
        'type'        => 'group',
        'options'     => array(
            'group_title'   => __( 'Slide {#}', 'my-gallery' ),
            'add_button'    => __( 'Add Slide', 'my-gallery' ),
            'remove_button' => __( 'Remove Slide', 'my-gallery' ),
            'sortable'      => true,
        ),
    ) );

    $cmb_group->add_group_field( $group_field_id, array(
        'name' => __( 'Slide Title', 'my-gallery' ),
        'id'   => 'title',
        'type' => 'text_medium',
    ));

    $cmb_group->add_group_field( $group_field_id, array(
        'name'    => __( 'Slide Type', 'my-gallery' ),
        'id'      => 'type',
        'type'    => 'radio_inline',
        'options' => array(
            'image' => __( 'Image', 'my-gallery' ),
            'video'   => __( 'Video', 'my-gallery' ),
        ),
        'row_classes' => 'radio_parent radio_type',
        'default' => 'image',
    ));

    $cmb_group->add_group_field( $group_field_id, array(
        'name' => __( 'Image', 'my-gallery' ),
        'id'   => 'image',
        'type' => 'file',
        'row_classes' => 'radio_type_child radio_child_image',
    ));

    $cmb_group->add_group_field( $group_field_id, array(
        'name' => 'Video',
        'desc' => __( 'Enter a Vimeo URL.', 'my-gallery' ),
        'id'   => 'video',
        'type' => 'oembed',
        'row_classes' => 'radio_type_child radio_child_video',
    ));

}
add_action( 'cmb2_init', 'my_gallery_group_metabox' );

The JS code I use:

var parents = ['.radio_type'];
//Trigger all Group Boxes
function initVisibilityAll() {
    $('.cmb-repeatable-grouping').each(function(i, e) {
        var group = $(e);
        $.each( parents, function( key, value ) {
            group.find(value + '_child').hide(); //.radio_type_child
            var active = group.find(value + ' input:radio.cmb2-option:checked').val(); //image
            group.find('.radio_child_' + active).show(); //.radio_child_image
        });
    });
}

$(document).on('change', '.radio_parent input:radio.cmb2-option', function() {
    initVisibilityAll();
});

$(document).on('click', 'button.cmb-add-group-row, .button.cmb-shift-rows', function() {
    initVisibilityAll();
});

initVisibilityAll();

But the JS code is not needed. If you inspect newly created elements, you see that they don't have any value assigned as shown in the screenshots above. This example also shows two more bugs already mentioned in other tickets (#246 and #257)

kynetiv pushed a commit to kynetiv/CMB2 that referenced this issue Apr 28, 2015
…a checkbox or radio input.

If the input is a checkbox or radio, the original value is needed or saving will not save the value (subsequent post saves work because inputs  will then have the required value set).
This patch conditionally adds values to radios and inputs on new repeatable and sets the checked attribute only on the default option (radios).
Fixes issue CMB2#257 (this will set reset text inputs as well), and CMB2#246 & CMB2#263(restores radio and checkbox values on new row saves)
@jtsternberg
Copy link
Member

Should be good in the trunk branch. please test.

@f-o-n
Copy link
Author

f-o-n commented Dec 8, 2015

Yes it works now, thank you

@f-o-n f-o-n closed this as completed Dec 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants