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....

Wizzard
  • 12,582
  • 22
  • 68
  • 101

1 Answers1

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?

Wizzard
  • 12,582
  • 22
  • 68
  • 101