Skip to content

Repeatable and sortable group with repeatable fields, issue with group shift #1426

Closed

Description

@milutinjaric
add_action( 'cmb2_admin_init', function(){
	
	$cmb = new_cmb2_box([
		'id'            => 'metabox_attachment',
		'title'         => 'TEst',
		'object_types'  => [ 'page' ], // Post type
		'priority'      => 'high',
		'show_names'    => true
	] );

	$group_field_id = $cmb->add_field( array(
		'id'          => 'wiki_test_repeat_group',
		'type'        => 'group',
		'description' => __( 'Generates reusable form entries', 'cmb2' ),
		'repeatable'  => true,
		'options'     => array(
			'group_title'       => __( 'Entry {#}', 'cmb2' ), 
			'add_button'        => __( 'Add Another Entry', 'cmb2' ),
			'remove_button'     => __( 'Remove Entry', 'cmb2' ),
			'sortable'          => true,
		),
	) );

	$cmb->add_group_field( $group_field_id, array(
		'name' => 'Entry Title 2',
		'id'   => 'subtitle',
		'type' => 'text',
		 'repeatable' => true,
		 'sortable' => true
	) );

});

Step 1.
In first group click to repeat field and populate both https://prnt.sc/13o8bn2

Step 2.
Add another group ( Add another Entry )

Step 3.
Click move down on first group.

Result:
Only first field from group 1 is transferred to group 2

Activity

  1. tw2113 commented on Jun 1, 2021

    @tw2113
    Contributor

    Can confirm odd behavior but do not have an answer for a solution.

  2. milutinjaric commented on Jun 2, 2021

    @milutinjaric
    Author

    Instead of copying values I've tried swap group boxes approach and so far it works ok.

    It requires changed function makeRepeatableSortable from issue #1428

         cmb.shiftRows = function( evt ) {
    
            evt.preventDefault();
     
             var $this = $( this );
             var moveUp = $this.hasClass( 'move-up' ) ? true : false;
             var $from = $this.parents( '.cmb-repeatable-grouping' );
             var $goto = moveUp ? $from.prevAll( '.cmb-repeatable-grouping:first' ) : $from.nextAll( '.cmb-repeatable-grouping:first' );
    
             cmb.triggerElement( $this, 'cmb2_shift_rows_enter', $this, $from, $goto );
    
             if ( ! $goto.length ) {
                return;
            }
    
            cmb.triggerElement( $this, 'cmb2_shift_rows_start', $this, $from, $goto );
    
             var fromIterator = $from.attr('data-iterator');
             var gotoIterator = $goto.attr('data-iterator');
    
             $from.attr( 'data-iterator', gotoIterator ).find( cmb.repeatEls ).each( function () {
                var $this = $( this );
                var name  = $this.attr( 'name' );
                if ( 'undefined' !== typeof name ) {  
                    var $newName = name.replace( '[' + fromIterator + ']', '[' + gotoIterator + ']' );
                    $this.attr( 'name', $newName );
                }
             });
    
    
            $goto.attr( 'data-iterator', fromIterator ).find( cmb.repeatEls ).each( function () {
                var $this = $( this );
                var name  = $this.attr( 'name' );
                if ( 'undefined' !== typeof name ) {  
                    var $newName = name.replace( '[' + gotoIterator + ']', '[' + fromIterator + ']' );
                    $this.attr( 'name', $newName );
                }
            });
    
    
            if( moveUp ){
                $goto.before( $from.clone() );
            }
            else {
                $goto.after( $from.clone() );
            }
    
            $from.remove();
            cmb.makeRepeatableSortable( moveUp ? $goto.prevAll( '.cmb-repeatable-grouping:first' ) : $goto.nextAll( '.cmb-repeatable-grouping:first' ) )
    
            // shift done
            cmb.triggerElement( $this, 'cmb2_shift_rows_complete', $this, $from, $goto );
    
         };
  3. jtsternberg commented on Jun 3, 2021

    @jtsternberg
    Member

    Your solution is interesting, but it will need to be tested with every field type (which supports being in a repeatable group) to make sure there aren't any regressions. We have to be very careful about backwards compatibility with CMB2, or we could break things for a lot of people.

  4. milutinjaric commented on Jun 3, 2021

    @milutinjaric
    Author

    I could not agree more, test everything before adding it. Since it works for my application I'm serving modified script via WP hook so I'm good.

  5. added a commit that references this issue on Jun 3, 2021
    7468908
  6. meo8 commented on Jan 8, 2022

    @meo8

    Hi All,

    I'm also running into this issue. Any updates?

    Thank you!

  7. added a commit that references this issue on Feb 15, 2022
    150c2c9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions