Skip to content Skip to sidebar Skip to footer

Devextreme Load Json As Datasource Using Knockout

I'm new to both javascript and devextreme and currently finding my way through. I created a webpage which loads a json-file, which worked well, but I can't get it done using devext

Solution 1:

The "getData()" method should return a proimise in order to work with dxDataGrid. Probably something like this:

var getData = function () {
    var deferred = $.Deferred();
    var xmlhttp = newXMLHttpRequest(),
                        json;
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState === 4 && xmlhttp.status === 200) {
            json = JSON.parse(xmlhttp.responseText);
            console.log(json);
            //return json;
            deferred.resolve(json); // json should be array here
        }
    };              

    xmlhttp.open('GET', 'data.json', true);
    xmlhttp.send();

    return deferred.promise();
};

DevExtreme works with JQuery promise. I'm not sure about other promise libraries.

Update 1

It looks like you get array of arrays. In this case you need just take first element:

deferred.resolve(json[0]); // json should be array here

Post a Comment for "Devextreme Load Json As Datasource Using Knockout"