ngx-typeahead icon indicating copy to clipboard operation
ngx-typeahead copied to clipboard

Handling non-array response from endpoint?

Open lentzi opened this issue 7 years ago • 5 comments

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)

lentzi avatar Apr 23 '18 11:04 lentzi

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

orizens avatar May 29 '18 07:05 orizens

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?

i-n-g-m-a-r avatar Dec 13 '18 16:12 i-n-g-m-a-r

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

orizens avatar Dec 13 '18 21:12 orizens

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

i-n-g-m-a-r avatar Dec 14 '18 10:12 i-n-g-m-a-r

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

kooison avatar May 08 '20 12:05 kooison