NejcZdovc / ng2-select2

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

Selec2Ajax not working #113

Open Alavi1412 opened 6 years ago

Alavi1412 commented 6 years ago

I want to implement the select2 Ajax search but When I import Select2AjaxOption I got this error: /node_modules/ng2-select2/ng2-select2"' has no exported member 'Select2AjaxOptions' And also there is no document for using ajax search

diegodesouza commented 6 years ago

I haven't used the ajax functionality, but there was a question opened before about this, https://github.com/NejcZdovc/ng2-select2/issues/59 perhaps this would shed some light in your issue.

gunboxer commented 6 years ago

I'm using Select2AjaxOptions it works very well.

public getSelect2Ajax(url: string, delay: number, pageSize: number, idProp: string, nameProp: string, sortingCol: string): Select2AjaxOptions {
        const that = this;
        return {
            getToken: function(settings: JQueryAjaxSettings, tokenCallBack: () => void): void {
                that.keycloakService.getToken().then(
                    (token) => {
                        if (settings && settings.headers) {
                            settings.headers['Authorization'] = 'Bearer ' + token;
                        }
                        tokenCallBack();
                    });
            },
            url: this.processInputUrl(url),
            processResults: function(data, params) {
                params.page = params.page || 1;

                return {
                    results: data.content.items.map((enity: { [key: string]: string; }) => {
                        return {
                            id: enity[idProp],
                            text: enity[nameProp],
                            entity: enity};
                    }),
                    pagination: {
                        more: (params.page * pageSize) < data.content.total
                    }
                };
            },
            delay: delay,
            cache: true,
            data: function (params: Select2QueryOptions) {
                return {
                    filterNamePattern: params.term, // search term
                    offset: (params.page || 1) - 1,
                    sortField: sortingCol,
                    isDictionary: true
                };
            },
            headers: {'Authorization': '', 'Content-Type': 'application/json'},
            dataType: 'json'} as Select2AjaxOptions;
    }