weixin_33724659 2017-09-05 08:31 采纳率: 0%
浏览 55

在ng2-select2中使用AJAX

I have many drop-downs in my angular2js project. I am using ng2-select2 for these drop-down. In some dropdown, I need to filter and fetch data from an API as per the string user types into the select2 search box. The plug-in initializes but doesn't fire any AJAX.

My code so far:

HTML:

<select2 [data]="options | async"></select2>

Component:

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

Can anyone point me into the right direction? Any help is really appreciated.

  • 写回答

1条回答 默认 最新

  • weixin_33698043 2017-09-05 08:37
    关注

    you use [data] which will input only data not option

    <select2 [data]="options | async"></select2> 
    

    try

    <select2 [options]="options | async"></select2>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题