当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Showing data in angular UI grid

问题描述:

I have a problem a problem with showing data in ui grid table.

I have defined an API from which I want to show data and I can access them through the browser, but the problem is in showing rendered data. Here is my angular controller in which I've defined function for getting data from API:

 getData();

$scope.myData = [];

$scope.gridOptions.data = []

$scope.gridOptions.data = $scope.myData;

function getData() {

$http.get('/load/').success(function (data) {

data.forEach( function( row, index ) {

$scope.myData.push(data);

});

$scope.gridOptions.data = data;

console.log('data from api', data);

})

};

and I got empty grid.

Data are showed in console:

I've also tried to parse data var jsonObj = JSON.parse(data);, but I got an error

Unexpected token o

at Object.parse (native)

网友答案:

Use this function to push your data into an array. Check your data in the console, drill down into your data. (click on the arrow) My data is within the .doc yours may be within another attribute. change the "items[i].doc" to the correct attribute for your data.

// pass data.rows to function items params
// the type is to filter 
function fncArray(items,type) {
    var filtered = [];
    for(var i=0; i < items.length; i++)  {
        if ( items[i].doc.type = type){
            filtered.push(items[i].doc);
        }
    }
    // console.log(filtered);
   return filtered;
}

this is to use without filtering

// pass data.rows to function items params 
function fncArray(items) {
    var newData= [];
    for(var i=0; i < items.length; i++)  {
            newData.push(items[i].doc);
    }
   return newData;
}
网友答案:

you can write $scope.$apply() to trigger changes in controller

网友答案:

Returned data should be ["key":[object, object, object, object]] in this form. 1) convert this data using var jsonObj = JSON.parse(data); 2) Use this expression in success !$scope.$$phase?$scope.$apply():null;

网友答案:

I'm sorry guys, my mistake here. There is no need for parsing. I've defined wrong columnDefs here in controller :facepalm:

分享给朋友:
您可能感兴趣的文章:
随机阅读: