UPDATES to conditional-logic.js to fix 'in' array logic

Support MB Conditional Logic UPDATES to conditional-logic.js to fix 'in' array logic

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #2408
    MikeTrebilcockMikeTrebilcock
    Participant

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

    #2425
    Tan NguyenTan Nguyen
    Participant

    Dear 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

    #2426
    MikeTrebilcockMikeTrebilcock
    Participant

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

    #2439
    Tan NguyenTan Nguyen
    Participant

    Oh, 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 😉

    #2453
    Tan NguyenTan Nguyen
    Participant

    Btw, I've added it to the latest version. Cheers!

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘UPDATES to conditional-logic.js to fix 'in' array logic’ is closed to new replies.