-
Notifications
You must be signed in to change notification settings - Fork 573
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
Comments
Do you have a screenshot and/or code? Help us help you. :) |
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. 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) |
…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)
Should be good in the trunk branch. please test. |
Yes it works now, thank you |
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.
The text was updated successfully, but these errors were encountered: