UPDATES to conditional-logic.js to fix 'in' array logic
Support › MB Conditional Logic › UPDATES to conditional-logic.js to fix 'in' array logic
- This topic has 4 replies, 2 voices, and was last updated 9 years, 1 month ago by
Tan Nguyen.
-
AuthorPosts
-
March 2, 2016 at 8:54 PM #2408
MikeTrebilcock
ParticipantWe have been working with the 'in' array of values and found that it didn't work as expected, we expected the array to be true if any or all values are set but found the code did not support this scenario so we have been making updates!
Using :
'visible' => array('type_select', 'in', array(35,32)),
when registering metaboxes`We have changed the case statement to run a little differently
Replaced:case 'in': return haystack.indexOf(needle) > -1;
With:
case 'in': if ( $.isArray(needle) ) { var found = false; $.each(needle, function(index, value) { if(haystack.indexOf(value)>-1) { found = true; } }); return found; } else { return haystack.indexOf(needle) > -1; }
and then further down
changed the following to handle arrays of numeric values:compare = compare.trim(); if ( $.isNumeric( item ) ) item = parseInt( item );
To:
compare = compare.trim(); if ( $.isArray(item) ) { $.each(item, function(index, value) { if ( $.isNumeric( value ) ) item[index] = parseInt( value ); }); } else { if ( $.isNumeric( item ) ) item = parseInt( item ); }
Not sure if this is of any use or if it breaks/steps on the toes of other functions, but thought it worth submitting in case it might be used in the code base.
March 7, 2016 at 5:05 PM #2425Tan Nguyen
ParticipantDear Mike,
Thanks for your contribution, can you please post your code that you used to register meta boxes? I've tested
in
statement and see that it still works. This is my example:add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) { $meta_boxes[] = array( 'id' => 'personal', 'title' => __( 'Personal Information', 'textdomain' ), 'post_types' => array( 'post', 'page', 'product' ), 'context' => 'normal', 'priority' => 'high', 'fields' => array( array( 'name' => 'dummy_checkbox', 'type' => 'checkbox_list', 'id' => 'dummy_checkbox', 'options' => array('foo', 'bar', 'baz') ), array( 'name' => __( 'Full name', 'textdomain' ), 'desc' => 'Format: First Last', 'id' => 'fname', 'type' => 'text', 'std' => 'Anh Tran', 'class' => 'custom-class', 'visible' => array('dummy_checkbox', 'in', array(1,2)) ), ) ); return $meta_boxes; });
Best regards
Tan
March 7, 2016 at 6:30 PM #2426MikeTrebilcock
ParticipantHi we are using:
`$meta_boxes[] = array(
'id' => 'he_subtitle',
'title' => __( 'HE Sub-Title'),
'post_types' => 'courses',
'priority' => 'low',
'context' => 'side',
'visible' => array('course_type_select', 'in', array(32,35)),
'fields' => array(
// Course reference code
array(
'name' => __( '' ),
'id' => "{$prefix}he_subtitle",
'desc' => __( 'HE sub-title (Year of entry)' ),
'type' => 'text',
),
),
);`The updated code allows the 32 or 35, or both 32 and 35 to be selected to display the field, rather than having to have both(32 and 35) only selected to display which is what we were finding.
March 9, 2016 at 5:27 PM #2439Tan Nguyen
ParticipantOh, thank you, I'm now knowing the problem, it works when either 32 or 35 selected but not works when both 32 and 35 selected. I've updated code and will release within next few day 😉
March 10, 2016 at 11:05 PM #2453Tan Nguyen
ParticipantBtw, I've added it to the latest version. Cheers!
-
AuthorPosts
- The topic ‘UPDATES to conditional-logic.js to fix 'in' array logic’ is closed to new replies.