NejcZdovc / ng2-select2

Select 2 wraper for angular2
MIT License
123 stars 93 forks source link

How to set initial value, when list-data is provided via ajax-search? #107

Open wautersj opened 7 years ago

wautersj commented 7 years ago

Hi!

I'm trying to get get this to work: Data I can select out, is only available when typed in the search box (via ajax). (So no data via [data] attribute). The selected value will be saved.

When, a value was chosen, and the user returns to the view with the Select, that value should be visible in the Select.

When a value changes, I only get the ID. But how can I set the initial value? (I guess I need an object providing both ID, and Text?)

How does this mechanic work?

gunboxer commented 6 years ago
this.options.initSelection = function(element, callback: (data: any) => void) {
                        callback({id: result.id, text: result.name});
                    };

That is how I did it. In case if you getting initial value in some async request there is possible to change "data" property right after you set initial selection. I did this:

this.data = [];