Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for select2 ajax #19

Open
jtsternberg opened this issue Jul 24, 2015 · 5 comments
Open

Support for select2 ajax #19

jtsternberg opened this issue Jul 24, 2015 · 5 comments

Comments

@jtsternberg
Copy link
Contributor

jtsternberg commented Jul 24, 2015

With select2, you can populate options via an ajax search vs providing all the options inline via an object. Any thoughts on updating this lib to allow that?

@mustardBees
Copy link
Owner

@jtsternberg I’m not going to lie, it’s something I’ve thought about… I’m not sure how the implementation would look. It would be ideal in situations where loading a massive options array would be prohibitively expensive. Coming up with something generic would be tough. It sounds more like passing in a callback function which handles the AJAX request which in turn searches a dataset. There are the obvious common use cases, searching posts/terms which could be out of the box. Not sure if I’m overthinking this? Be interested in hearing your thoughts.

@jtsternberg
Copy link
Contributor Author

jtsternberg commented May 25, 2016

Yep, my thought would be an ajax callback, similar to the native CMB2 'options_cb' parameter, which requires you to pass back an array of key/values. This would enable something like this for a taxonomy:

$cmb->add_field( array(
    'name'       => 'Ingredients',
    'id'         => $prefix . 'ingredients',
    'desc'       => 'Select ingredients. Drag to reorder.',
    'type'       => 'pw_multiselect',
    'taxonomy'   => 'cooking_ingredients',
    'options'    => array(), // let's use ajax instead.
    'ajax_cb'    => 'your_prefix_get_cooking_ingredients',
    'attributes' => array(
        'data-maximum-selection-length' => '2',
    ),
) );

...

function your_prefix_get_cooking_ingredients( $search_term, $field ) {
    // if there is no search string, bail here
    if ( empty( $search_term ) ) {
        return;
    }

    $taxonomy = $field->args( 'taxonomy' );

    // add our term clause filter for this iteration
    add_filter( 'terms_clauses', 'your_prefix_wilcard_term_name' );

    // do term search
    $terms = get_terms( $taxonomy, array(
        'number' => 10,
        'hide_empty' => false,
        'name__like' => sanitize_text_field( $search_term ),
        'cache_domain' => 'your_prefix_search_' . $taxonomy,
    ) );

    // and remove the filter
    remove_filter( 'terms_clauses', 'your_prefix_wilcard_term_name' );

    // if we didn't find any terms, bail
    if ( empty( $terms ) ) {
        return;
    }

    $results = array();
    foreach ( $terms as $term ) {

        $name = $term->name;

        if ( $term->parent && ( $parent_term = get_term_by( 'id', $term->parent, $taxonomy ) ) ) {
            $name = $parent_term->name .' / ' . $name;
        }

        $results[ $term->slug ] = $name;
    }

    return $results;
}

/**
 * Make term search wildcard on front as well as back
 */
function your_prefix_wilcard_term_name( $clauses ) {

    // add wildcard flag to beginning of term
    $clauses['where'] = str_replace( "name LIKE '", "name LIKE '%", $clauses['where'] );
    return $clauses;
}

@mbucurcf
Copy link

mbucurcf commented Nov 15, 2017

You can achieve an AJAX search by using data-* attributes:

cmb->add_field( array(
	'name' => 'name'
	'id' => 'id',
	'type' => 'pw_multiselect',
	'select_all_button' => true,
	'remove_default' => 'true',
	'options' => array(),
	'attributes' => array(
		'data-ajax--url' => admin_url( 'admin-ajax.php?action=your_action' ),
		'data-ajax--cache' => 'true',
		'data-ajax--delay' => 300,
		'data-close-on-select' => 'false',
		'data-minimum-input-length' => 3,
	),
) );

See https://select2.org/configuration/data-attributes

You would then need to build the options array with the selected options.

@cindyhont
Copy link

The jquery of wordpress v4.9 is v1.12.4
data-* attributes doesn't work in such case

https://github.com/select2/select2/issues/2969

@yuriy-dev
Copy link

You can achieve an AJAX search by using data-* attributes:

cmb->add_field( array(
	'name' => 'name'
	'id' => 'id',
	'type' => 'pw_multiselect',
	'select_all_button' => true,
	'remove_default' => 'true',
	'options' => array(),
	'attributes' => array(
		'data-ajax--url' => admin_url( 'admin-ajax.php?action=your_action' ),
		'data-ajax--cache' => 'true',
		'data-ajax--delay' => 300,
		'data-close-on-select' => 'false',
		'data-minimum-input-length' => 3,
	),
) );

See https://select2.org/configuration/data-attributes

You would then need to build the options array with the selected options.

Hi,
I've implemented ajax call as suggested above. It works fine.
But I'm not sure how to build options array once I got ajax respond.
From documentation it is not clear how to do it.
Can anybody advice how to do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants