corry
corry

Reputation: 1529

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. enter image description here

Data are showed in console: enter image description here

I've also tried to parse data var jsonObj = JSON.parse(data);, but I got an error Unexpected token o at Object.parse (native)

Upvotes: 1

Views: 1352

Answers (4)

Morgan Hayes
Morgan Hayes

Reputation: 383

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

Upvotes: 1

corry
corry

Reputation: 1529

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

Upvotes: 0

Ananda Naphade
Ananda Naphade

Reputation: 57

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;

Upvotes: 0

oto lolua
oto lolua

Reputation: 499

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

Upvotes: 0

Related Questions