Template on Select2
- This topic has 5 replies, 2 voices, and was last updated 8 years, 8 months ago by
Anh Tran.
-
AuthorPosts
-
August 6, 2016 at 5:06 AM #3815
wefit.lucas
ParticipantHello,
Is it possible to something like this: https://select2.github.io/examples.html#templating with the select_advanced through the js_options?
Thanks.
August 6, 2016 at 8:58 AM #3819Anh Tran
KeymasterI haven't tried the templating option for select2, but ideally, you can pass any params to the
js_options
. So just try it 🙂August 11, 2016 at 10:15 PM #3838wefit.lucas
ParticipantActually, 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.
August 12, 2016 at 9:44 AM #3840Anh Tran
KeymasterStrange, 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
andallowClear
just work as expected.Please try again.
August 16, 2016 at 4:02 AM #3852wefit.lucas
ParticipantThese 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.
August 16, 2016 at 10:06 AM #3857Anh Tran
KeymasterI'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 :(.
-
AuthorPosts
- You must be logged in to reply to this topic.