在 ng2-select2 中使用 AJAX
Use AJAX in ng2-select2
我的 angular2js 项目中有很多下拉菜单。我对这些下拉菜单使用 ng2-select2。在某些下拉列表中,我需要根据用户在 select2 搜索框中键入的字符串从 API 中过滤和获取数据。插件初始化但不触发任何 AJAX.
到目前为止我的代码:
HTML:
<select2 [data]="options | async"></select2>
组件:
import { Component,OnInit } from '@angular/core';
import {Select2OptionData} from 'ng2-select2';
import {AlertService, LTJService} from '../../_services/index';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'nationalities',
templateUrl: '../../views/buyer_profile/nationalities.component.html',
})
export class NationalityComponent implements OnInit {
//public nationalities: Observable<Array<Select2OptionData>>;
public options: Select2Options;
public ajaxOptions: Select2AjaxOptions;
constructor(private alertService: AlertService, private ltjService: LTJService) {
}
ngOnInit(): void {
console.log('In ngOnInit');
this.ajaxOptions = {
url: '<API URL>',
dataType: 'json',
delay: 250,
cache: false,
data: (params: any) => {
console.log("AA", params);
return {
query: params.term,
gotoPage: params.page
}
},
processResults: (data: any, params: any) => {
params.page = params.page || 1;
console.log('data: ', data);
return {
results: $.map(data.data, function(obj) {
return {id: obj.id, text: obj.name};
}),
pagination: {
more: (params.page * 10) < data.objectValue.total_count
}
};
},
};
this.options = {
ajax: this.ajaxOptions
}
}
}
任何人都可以指出我正确的方向吗?
非常感谢任何帮助。
您使用 [data],它只会输入数据而不是选项
<select2 [data]="options | async"></select2>
尝试
<select2 [options]="options | async"></select2>
我的 angular2js 项目中有很多下拉菜单。我对这些下拉菜单使用 ng2-select2。在某些下拉列表中,我需要根据用户在 select2 搜索框中键入的字符串从 API 中过滤和获取数据。插件初始化但不触发任何 AJAX.
到目前为止我的代码:
HTML:
<select2 [data]="options | async"></select2>
组件:
import { Component,OnInit } from '@angular/core';
import {Select2OptionData} from 'ng2-select2';
import {AlertService, LTJService} from '../../_services/index';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'nationalities',
templateUrl: '../../views/buyer_profile/nationalities.component.html',
})
export class NationalityComponent implements OnInit {
//public nationalities: Observable<Array<Select2OptionData>>;
public options: Select2Options;
public ajaxOptions: Select2AjaxOptions;
constructor(private alertService: AlertService, private ltjService: LTJService) {
}
ngOnInit(): void {
console.log('In ngOnInit');
this.ajaxOptions = {
url: '<API URL>',
dataType: 'json',
delay: 250,
cache: false,
data: (params: any) => {
console.log("AA", params);
return {
query: params.term,
gotoPage: params.page
}
},
processResults: (data: any, params: any) => {
params.page = params.page || 1;
console.log('data: ', data);
return {
results: $.map(data.data, function(obj) {
return {id: obj.id, text: obj.name};
}),
pagination: {
more: (params.page * 10) < data.objectValue.total_count
}
};
},
};
this.options = {
ajax: this.ajaxOptions
}
}
}
任何人都可以指出我正确的方向吗? 非常感谢任何帮助。
您使用 [data],它只会输入数据而不是选项
<select2 [data]="options | async"></select2>
尝试
<select2 [options]="options | async"></select2>