2

I am using the jquery select 2 plugin. http://ivaynberg.github.io/select2/#ajax

I have the search working, and I am able to pull in the results and select things fine. But when I reload the page I want the previous selection populated. I have the id and what I want to display, but it seems the way to do it is using an initSelection call.

This seems a bit overkill, as I already have the text I want to show....

$(".user-search").select2({
    minimumInputLength: 2,
    ajax: {
        url: "/users/search",
        dataType: 'json',
        data: function (term, page) {
            return { keyword: term };
        },
        results: function (data, page) {
            return { results: data };
        }
    }
});

<input name="user_id" class="user-search" type="text" style="width:100%" value="{{ $user->id }}" />

Is there some way I can do $user->name? maybe set it on the input as a data-display....

1 Answer 1

1

I have done the following....

...code from the opening question,
initSelection: function(element, callback) {
    var id=$(element).val();
    if (id!=="") {
      callback({id:id, text : $(element).attr('data-display')});
    }
}

and done

<input..... data-display="{{ $user->name }}">

it works, is that a good way?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.