RadAutoCompleteTextView: Using Async Data Fetch

There are multiple scenarios where we need to represent data loaded from remote provider like a web service.
Dynamic loading of items, based on the user input, could be quite useful when we are working with sets of data that are not available locally.
In order to satisfy this scenario, the autocomplete provides you with API, which allows you asynchronously to load your data and then pass it to the control.

In next section we will discuss the process of working with remote data in details.
For the purpose of the example we will use json data containing description of airports.


Populating the autocomplete asynchronously is quite straightforward task. All you need is a promise which should handle
the data fetch and return a collection of TokenModel objects.
You should assign this promise to the loadSuggestionsAsync property of the RadAutoCompleteTextView object.
The autocomplete executes this promise every time a symbol is typed and then generates
suggestions based on the collection returned by the promise.

Create a page with RadAutoCompleteTextView control. Setup the control in a way that suits you and bind it
to source collection, which in our case is called dataItems.
Do not forget to provide a suggestionItemTemplate which will represent each suggestion.

<navigation:ExamplePage loaded="onPageLoaded" xmlns:navigation="navigation/example-page" xmlns:lv="nativescript-telerik-ui-pro/autocomplete" xmlns="http://www.nativescript.org/tns.xsd">
    <StackLayout ios:backgroundColor="#CDCECE" padding="5">
        <Label text="Select destination:"></Label>
        <lv:RadAutoCompleteTextView id="autocmp" items="{{ dataItems }}" suggestMode="Suggest" displayMode="Plain">

                <lv:SuggestionView suggestionViewHeight="300">
                        <StackLayout orientation="vertical" padding="10">
                            <Label text="{{ text }}"></Label>


Retrieve the RadAutoCompleteTextView object that was initialized in the xml file and set it's loadSuggestionsAsync
property to e function which accepts one parameter (the typed text).
In this function define a promise, load the remote data in it and then return the promise.

Later on the autocomplete will invoke the loadSuggestionsAsync function and when the promise is resolved,
it will use the returned items to complete it's population.

export class ViewModel extends Observable {
    private autocmp;
    private countries = ["Australia", "Albania", "Austria", "Argentina", "Maldives", "Bulgaria", "Belgium", "Cyprus", "Italy", "Japan",
        "Denmark", "Finland", "France", "Germany", "Greece", "Hungary", "Ireland",
        "Latvia", "Luxembourg", "Macedonia", "Moldova", "Monaco", "Netherlands", "Norway",
        "Poland", "Romania", "Russia", "Sweden", "Slovenia", "Slovakia", "Turkey", "Ukraine",
        "Vatican City", "Chad", "China", "Chile"];

    constructor(args) {
        var page = args.object;
        this.autocmp = page.getViewById("autocmp");
        this.autocmp.loadSuggestionsAsync = function (text) {
            var promise = new Promise(function (resolve, reject) {
                http.getJSON("http://www.telerik.com/docs/default-source/ui-for-ios/airports.json?sfvrsn=2").then(function (r: any) {
                    var airportsCollection = r.airports;
                    var items: Array<TokenModel> = new Array();
                    for (var i = 0; i < airportsCollection.length; i++) {
                        items.push(new TokenModel(airportsCollection[i].FIELD2, null));
                }, function (e) {

            return promise;

    get dataItems(): ObservableArray<TokenModel> {
        return this.get("_dataItems");

    set dataItems(value: ObservableArray<TokenModel>) {
        this.set("_dataItems", value);


Want to see this scenario in action?
Check our SDK examples repo on GitHub. You will find this and many other practical examples with NativeScript UI.