orizens / ngx-typeahead

A simple but yet powerful typeahead component for Angular (css framework agnostic)
https://www.npmjs.com/package/ngx-typeahead
MIT License
61 stars 29 forks source link

Handling non-array response from endpoint? #17

Open lentzi opened 6 years ago

lentzi commented 6 years ago

Does the component only work if the request returns an array? My endpoint returns an object in the form of { statusCode: "200", entity: Array[] }. Is there a way to tell the component to handle response.entity instead of response, because currently I'm getting an error when the component tries calling response.map() when response is actually an object. (I've looked through all your public resources, including the demo)

orizens commented 6 years ago

hi @lentzi r u referring to the jsonp way of this component?

i-n-g-m-a-r commented 5 years ago

Is there a current status on this? It seems that the library is specifically designed to handle suggestions from Google. Things like this:

function toJsonpSingleResult(response) {
    return response[1];
}

Where does the [1] part come from? Maybe from this - the actual suggestions by Google, inside [1] :

ng_jsonp_callback_0 && ng_jsonp_callback_0(
    [
        "berlin",
        [
            ["berlin",0,[131]], ["berlin take my breath away",0,[131]], 
            ["berlin marathon 2018",0], ["berlin wall",0,[131]],
            ["berlin techno",0,[131]], ["berlin stirbt",0,[131]],
            ["berlin calling",0], ["berlin wall coming down",0,[131]],
            ["berlin station trailer",0,[131]], ["berlin wall documentary",0,[131]]
        ],
        {"k":1,"q":"onZ0QTsumy3nJdCP2FQEiahNa08"}
    ]
)

Is the library intended to be extensible?

orizens commented 5 years ago

@i-n-g-m-a-r i'm thinking about a proper handler for this

i-n-g-m-a-r commented 5 years ago

@orizens maybe something like this:

Client component:

    [taResultsDecorator]="resultsDecorator"

...

    resultsDecorator(results: any) : any[] {
        return ["foo", "bar", "qux", "baz"];
    }

NgxTypeAheadComponent:

    @Input()
    taResultsDecorator = (results: any) : any[] => results;

...

    assignResults(results: any[]) {
        const labelForDisplay = this.taListItemLabel;
        this.resultsAsItems = this.taResultsDecorator(results); // decorate results
        this.results = this.resultsAsItems.map( // use decorated results
            (item: string | any) => (labelForDisplay ? item[labelForDisplay] : item)
        );
        this.suggestionIndex = NO_INDEX;
        if (!results || !results.length) {
            this.activeResult = this.searchQuery;
        }
    }
kooison commented 4 years ago

It’s a great idea, my application query result from Solr and it can only return in object form. Any plan to fix this?