Comments (2)
I have the same problem, were you able to fix it?
I'm trying to create a wrapper component that is a select2 with options that are always the same to be used in different components, but I'm also stuck in setting the initial value for this scenario, it works fine when I just use the raw [ngModel] but not in my wrapper
from ng-select2.
I think you might find this helpful. #32
The way select2 works, is that it wraps a regular html select with a fancy user interface. However changing the underlying select does absolutely nothing to the select2 interface, you have to tell it to update with the default data. So if you're using FormGroup with ng-select2, then the underlying select correctly has your default value, but ng-select2 does not show this to you.
If you have server side data like I do, then it's even more difficult, because the actual options don't exist on the client side, so there is nothing to select with the underlying html select. Hence the issue I referenced.
Since then, I had to add another change event to get fired, but here's my code for clarity.
preSelect2(
getUrl: string,
ngSelect2Id: string,
document: Document
): Observable<any> {
return this.http
.get<any>(getUrl, {
headers: new HttpHeaders().append('Content-Type', 'application/json'),
})
.pipe(
tap((res) => {
/*my server simply returns a json of { text: string; id: string; }, the id is set as the actual
value for the underlying option, and the text is used for what the user sees, so what
the select2 element shows as the value*/
if (res) {
const select = document
.getElementById(ngSelect2Id)
?.querySelector('select');
if (select != null) {
select.textContent = '';
const option = document.createElement('option');
option.innerText = res.text;
option.setAttribute('value', res.id);
option.setAttribute('selected', 'selected');
select.appendChild(option);
select.dispatchEvent(new Event('change'));
select.dispatchEvent(
new CustomEvent('select2:select', {
detail: { data: res },
})
);
} else {
console.error('Select2 element not found. Cant preselect value.');
}
}
})
);
}
Here's how you call it:
this.preSelect2(
API_URL + 'Get?id=' + this.myModel.id, // I use simple GET requests on the server for select2 selection
'myNgSelect2Id', // the id you give to the select, eg.: <ng-select2 id="myNgSelect2Id"></ng-select2>
document // built-in property of all @Component in angular
).subscribe((success) => /* do something*/);
from ng-select2.
Related Issues (20)
- Wrong value shown when using "tags" option with initial value of null HOT 6
- Is there async support for this wrapper? HOT 1
- How to trigger change event with server side select2 to download and render a selected item? HOT 1
- No valueChanged Update with Browser Autocomplete HOT 3
- Is it possible to remove default title attribute for selected items? HOT 1
- Create event for blur or focus out HOT 1
- Change the touched attribute to reflect on blur HOT 1
- NG0100: Expression has changed after it was checked HOT 1
- How to change options for ng-select2 that's already initialized? HOT 1
- Trying to install with NPMv7 gives error for incorrect peer dependency HOT 1
- ng build failed ng-select2 version 1.2.7 HOT 1
- Error: The target entry-point "ng-select2" has missing dependencies: - Select2 HOT 5
- When click on clear button, Open drop down again HOT 1
- 2 issues on drop down list HOT 1
- No valueChanged Update with Browser Autocomplete HOT 2
- ng-select2 with Reactive Forms HOT 2
- how I can bind reactive form formControlName in ng-select2 directive HOT 1
- Need to add custom class HOT 1
- Error: The target entry-point "ng-select2" has missing dependencies: - Select2 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ng-select2.