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

fix: trigger change detection on initialization with value #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions lib/ng2-select2.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ export class Select2Component implements AfterViewInit, OnChanges, OnDestroy, On
constructor(private renderer: Renderer) { }

ngOnInit() {
if(this.cssImport) {
if (this.cssImport) {
const head = document.getElementsByTagName('head')[0];
const link: any = head.children[head.children.length-1];
const link: any = head.children[head.children.length - 1];

if(!link.version) {
if (!link.version) {
const newLink = this.renderer.createElement(head, 'style');
this.renderer.setElementProperty(newLink, 'type', 'text/css');
this.renderer.setElementProperty(newLink, 'version', 'select2');
Expand All @@ -60,11 +60,11 @@ export class Select2Component implements AfterViewInit, OnChanges, OnDestroy, On
}

async ngOnChanges(changes: SimpleChanges) {
if(!this.element) {
if (!this.element) {
return;
}

if(changes['data'] && JSON.stringify(changes['data'].previousValue) !== JSON.stringify(changes['data'].currentValue)) {
if (changes['data'] && JSON.stringify(changes['data'].previousValue) !== JSON.stringify(changes['data'].currentValue)) {
await this.initPlugin();

const newValue: string = this.element.val() as string;
Expand All @@ -74,7 +74,7 @@ export class Select2Component implements AfterViewInit, OnChanges, OnDestroy, On
});
}

if(changes['value'] && changes['value'].previousValue !== changes['value'].currentValue) {
if (changes['value'] && changes['value'].previousValue !== changes['value'].currentValue) {
const newValue: string = changes['value'].currentValue;

this.setElementValue(newValue);
Expand All @@ -85,7 +85,7 @@ export class Select2Component implements AfterViewInit, OnChanges, OnDestroy, On
});
}

if(changes['disabled'] && changes['disabled'].previousValue !== changes['disabled'].currentValue) {
if (changes['disabled'] && changes['disabled'].previousValue !== changes['disabled'].currentValue) {
this.renderer.setElementProperty(this.selector.nativeElement, 'disabled', this.disabled);
}
}
Expand All @@ -96,6 +96,10 @@ export class Select2Component implements AfterViewInit, OnChanges, OnDestroy, On

if (typeof this.value !== 'undefined') {
this.setElementValue(this.value);
this.valueChanged.emit({
value: this.element.val(),
data: this.element.select2('data')
});
}

this.element.on('select2:select select2:unselect', () => {
Expand All @@ -113,8 +117,8 @@ export class Select2Component implements AfterViewInit, OnChanges, OnDestroy, On
}

private async initPlugin() {
if(!this.element.select2) {
if(!this.check) {
if (!this.element.select2) {
if (!this.check) {
this.check = true;
console.log("Please add Select2 library (js file) to the project. You can download it from https://github.com/select2/select2/tree/master/dist/js.");
}
Expand All @@ -135,7 +139,7 @@ export class Select2Component implements AfterViewInit, OnChanges, OnDestroy, On

Object.assign(options, this.options);

if(options.matcher) {
if (options.matcher) {
let oldMatcher: any = await this.requireOldMatcher();
options.matcher = oldMatcher(options.matcher);
this.element.select2(options);
Expand All @@ -147,26 +151,26 @@ export class Select2Component implements AfterViewInit, OnChanges, OnDestroy, On
this.element.select2(options);
}

if(this.disabled) {
if (this.disabled) {
this.renderer.setElementProperty(this.selector.nativeElement, 'disabled', this.disabled);
}
}

private async requireOldMatcher() : Promise<any> {
private async requireOldMatcher(): Promise<any> {
return new Promise<any[]>(resolve => {
jQuery.fn.select2.amd.require(['select2/compat/matcher'], (oldMatcher: any) => {
resolve(oldMatcher);
});
});
}

private setElementValue (newValue: string | string[]) {
if(Array.isArray(newValue)) {
private setElementValue(newValue: string | string[]) {
if (Array.isArray(newValue)) {
for (let option of this.selector.nativeElement.options) {
if (newValue.indexOf(option.value) > -1) {
this.renderer.setElementProperty(option, 'selected', 'true');
}
}
}
} else {
this.renderer.setElementProperty(this.selector.nativeElement, 'value', newValue);
}
Expand Down