Template on Select2

Support General Template on Select2

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #3815
    wefit.lucaswefit.lucas
    Participant

    Hello,

    Is it possible to something like this: https://select2.github.io/examples.html#templating with the select_advanced through the js_options?

    Thanks.

    #3819
    Anh TranAnh Tran
    Keymaster

    I haven't tried the templating option for select2, but ideally, you can pass any params to the js_options. So just try it 🙂

    #3838
    wefit.lucaswefit.lucas
    Participant

    Actually, I don't think the option js_options is working at all. Just tried adding allowClear and placeholder properties and it is still rendering with the default options (data-options="{"allowClear":true,"width":"none","placeholder":"Select an item"}"). Can you check, please? Tks.

    #3840
    Anh TranAnh Tran
    Keymaster

    Strange, this is the code I use to test and it works:

    add_filter( 'rwmb_meta_boxes', 'your_prefix_select_demo' );
    function your_prefix_select_demo( $meta_boxes ) {
    	$meta_boxes[] = array(
    		'title' => __( 'Select Field Demo', 'your-prefix' ),
    
    		'fields' => array(
    			array(
    				'name'       => __( 'Select Advanced', 'your-prefix' ),
    				'id'         => 'select_advanced',
    				'type'       => 'select_advanced',
    				'options'    => array(
    					'value1' => __( 'Label1', 'your-prefix' ),
    					'value2' => __( 'Label2', 'your-prefix' ),
    				),
    				'js_options' => [
    					'placeholder' => 'Select whatever you want',
    					'allowClear'  => false,
    				],
    			),
    
    		),
    	);
    
    	return $meta_boxes;
    }

    The placeholder and allowClear just work as expected.

    Please try again.

    #3852
    wefit.lucaswefit.lucas
    Participant

    These options worked, thanks. But I'm still having troubles with the templating. I don't know much PHP, so I'm having trouble to understand how this parameter would work. Taking your code as example:

    add_filter( 'rwmb_meta_boxes', 'your_prefix_select_demo' );
    function your_prefix_select_demo( $meta_boxes ) {
    	$meta_boxes[] = array(
    		'title' => __( 'Select Field Demo', 'your-prefix' ),
    		'fields' => array(
    			array(
    				'name'       => __( 'Select Advanced', 'your-prefix' ),
    				'id'         => 'select_advanced',
    				'type'       => 'select_advanced',
    				'options'    => array(
    					'value1' => __( 'Label1', 'your-prefix' ),
    					'value2' => __( 'Label2', 'your-prefix' ),
    				),
    				'js_options' => [
    					'placeholder' => 'Select whatever you want',
    					'allowClear'  => false,
    					'templateSelection'  => formatState,
    				],
    			),
    		),
    	);
    	return $meta_boxes;
    } 

    Being the formatState originally a JS function how am I supposed to use it? Should I pass the function call as a String and have it on a .js file or should I convert it to a PHP function? If I convert it to PHP function, how would I be able to get the 'state' object that is passed on the select2 example?

    Sorry for the trouble. thanks.

    #3857
    Anh TranAnh Tran
    Keymaster

    I've just checked the select2 documentation and see the problem. The templateSelection must be a JavaScript function. But when we set up in the PHP, we can only send a string and the JavaScript understand that as a string (even the function name, but it is just a string) and the template function could not be called.

    Currently, I haven't got a solution for this yet :(.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.