• Resolved dkurth

    (@dkurth)


    obscure question, but is there anyway I can change the repeatable record fields from “Record 1”, “Record 2” to something like one of my field values, if set? And to have all the repeatable fields closed, instead of open?

    UPDATE:
    Found the closed state…by adding the ‘closed’=> true, to the group field type.

    Now the question it is just the change title. In the same group field, I see the “group title” with a number.

    'group_title' => __( 'Record {#}', 'mmd' ),

    what I would like is to put the value of one of my fields, or a call back, so I can place the value into that field.

    Ideally:

    'group_title' => __( '{field-name}', 'mmd' ),

    But a call back to fill it, would also be most excellent.

    • This topic was modified 5 years, 8 months ago by dkurth.
    • This topic was modified 5 years, 8 months ago by dkurth.
    • This topic was modified 5 years, 8 months ago by dkurth.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter dkurth

    (@dkurth)

    Found this a few minutes ago…and posted in that question.
    https://github.com/CMB2/CMB2-Snippet-Library/blob/master/javascript/dynamically-change-group-field-title-from-subfield.php

    I tried it, but it is not working. Do yo see anything incorrect?

    $group_field_id = $cmb->add_field( array(
    	'id'          => 'MMDListsRecord',
    	'type'        => 'group',
    	'description' => __( 'Individual Directory Listings', 'mmd' ),
    	'options'     => array(
    		'group_title'       => __( 'Record {#}', 'mmd' ), // since version 1.1.4, {#} gets replaced by row number
    		'add_button'        => __( 'Add Another Record', 'mmd' ),
    		'remove_button'     => __( 'Remove Record', 'mmd' ),
    		'sortable'          => true,
    		'closed'            => true,
    	),
      'after_group' => 'mmdlist_add_js_for_repeatable_titles',
    ) );
    
    function mmdlist_add_js_for_repeatable_titles() {
    	add_action( is_admin() ? 'admin_footer' : 'wp_footer', 'mmdlist_add_js_for_repeatable_titles_to_footer' );
    }
    
    function mmdlist_add_js_for_repeatable_titles_to_footer() {
    	?>
    	<script type="text/javascript">
    	jQuery( function( $ ) {
    		var $box = $( document.getElementById( 'MMDListsRecord' ) );
    		var replaceTitles = function() {
    			$box.find( '.cmb-group-title' ).each( function() {
    				var $this = $( this );
    				var txt = $this.next().find( '[id$="title"]' ).val();
    				var rowindex;
    				if ( ! txt ) {
    					txt = $box.find( '[data-grouptitle]' ).data( 'grouptitle' );
    					if ( txt ) {
    						rowindex = $this.parents( '[data-iterator]' ).data( 'iterator' );
    						txt = txt.replace( '{#}', ( rowindex + 1 ) );
    					}
    				}
    				if ( txt ) {
    					$this.text( txt );
    				}
    			});
    		};
    		var replaceOnKeyUp = function( evt ) {
    			var $this = $( evt.target );
    			var id = 'title';
    			if ( evt.target.id.indexOf(id, evt.target.id.length - id.length) !== -1 ) {
    				$this.parents( '.cmb-row.cmb-repeatable-grouping' ).find( '.cmb-group-title' ).text( $this.val() );
    			}
    		};
    		$box
    			.on( 'cmb2_add_row cmb2_remove_row cmb2_shift_rows_complete', replaceTitles )
    			.on( 'keyup', replaceOnKeyUp );
    		replaceTitles();
    	});
    	</script>
    	<?php
    }
    

    I even tried adding the prefix to the call. Nothing. I must be missing something.

    • This reply was modified 5 years, 8 months ago by dkurth.
    • This reply was modified 5 years, 8 months ago by dkurth.
    Plugin Author Justin Sternberg

    (@jtsternberg)

    var $box = $( document.getElementById( 'MMDListsRecord' ) ); is wrong. the ID there should be the box ID, not the group field ID.

    Thread Starter dkurth

    (@dkurth)

    So the id of the field you want to use. Got it.
    Yet when I replace:

    var $box = $( document.getElementById( 'title' ) );'
    
    (the name of the field), nothing happens
    
    

    Here is a sample of one of the group fields. The one I want to use.

    $cmb->add_group_field( $group_field_id, array(
    	                             'name'            => 'test',
    	                             'id'              => 'title',
    	                             'type'            => 'title',
    							  'sanitization_cb' => false,
    							  'escape_cb'       => false,
    							  'on_front'        => false,
    								 									 
                                ) );  
    
    Plugin Author Justin Sternberg

    (@jtsternberg)

    No, not the ID of the group field or the individual group sub-fields. The Box ID. So if you’re using this snippet, then it would be mmd_records_test.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    Please review the provided snippet to see what I mean.

    Thread Starter dkurth

    (@dkurth)

    BINGO! Thank you.

    (See how language is different from one person to the next. When you showed in the sample “yourprefix_group_titles_metabox” I took that to be the group title id.

    Thread Starter dkurth

    (@dkurth)

    Awesome job

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘REPEATABLE FIELDS TITLE NAME & OPEN STATE’ is closed to new replies.