How do I get data to show up in angular ui.grid from an $http request

你。 提交于 2019-12-01 06:53:37

问题


I am using Typescript and AngularJS. I have a template that contains a ui.grid directive.

The grid is displayed and at some later time an $http request gets the data I want the grid to contain.

I tried setting the gridOptions.data to an array and in the success function of the $http call I set the array used for gridOptions.data to the returned data.

The grid shows nothing.

Any ideas?

I'm going to mark Roman's answer correct, but since I specified I was using Typescript I'll show my solution for completeness:

export class MyController  {
    constructor ( ) {
        //...code to setup grid, col defs, data as an empty array, etc...
        var myDataPromise = this.myHttpService.getDataForGrid();

        myDataPromise.then((data) => {
            this.gridOptions.data = data["value"];
        },(reason) => { },(update) => { });
    }
}

My template:

<div ui-grid='myController.gridOptions'></div>

I'm not sure why my original code didn't work honestly...I think Typescript and Angular together was just frying my brain early on...


回答1:


Try the following:

<!DOCTYPE html>
<html >
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
</head>
<body ng-app="app">
    <div ng-controller="gridController">
        <!-- Initialise the grid with ng-init call -->
        <div ui-grid="gridOptions" ng-init="GetGridData(urlList)">
        </div>
    </div>

    <script src="Scripts/ng/angular.min.js"></script>
    <script src="Scripts/ng-grid/ui-grid.min.js"></script>
    <link rel="Stylesheet" type="text/css" href="Scripts/ng-grid/ui-rid.min.css" />

    <script type="text/javascript">

        var app = angular.module('app', ['ui.grid']);

        app.controller("gridController",
            ["$scope", "$attrs", "$element", "$compile", "$http", "$q",
            function ($scope, $attrs, $element, $compile, $http, $q)
            {
                $scope.urlList = "YourSourceUrl";

                function fnGetList(url)
                {
                    var deferred = $q.defer();
                    $http.get(url)
                        .success(function (data)
                        {
                            deferred.resolve(data);
                        })
                        .error(function (errorMessage)
                        {
                            alert(errorMessage);
                            deferred.reject;
                        });
                    return deferred.promise;
                };

                $scope.GetGridData = function (url)
                {
                    console.log("In GetGridData: " + "Getting the data");

                    //  Test Only - un-comment if you need to test the grid statically.

                    //$scope.loadData = ([
                    //        {
                    //            "UserName": "Joe.Doe",
                    //            "Email": "Joe.Doe@myWeb.com"
                    //        },
                    //        {
                    //            "UserName": "Jane.Doe",
                    //            "Email": "Jane.Doe@myWeb.com"
                    //        },
                    //]);
                    //return;

                    fnGetList(url).then(function (content)
                    {
                        //  Assuming your content.data contains a well-formed JSON

                        if (content.data !== undefined)
                        {
                            $scope.loadData = JSON.parse(content.data);
                        }
                    });
                };

                $scope.gridOptions =
                {
                    data: 'loadData',
                    columnDef:
                        [
                            { field: 'UserName', name: 'User' },
                            { field: 'Email', name: 'e-mail' }
                        ]
                };

            }
        ]);
</script>

</body>

If you do not want to populate the grid immediately, delete the call to

ng-init

and call the associated function on some other event.

Hopefully, without knowing much more about your specific problem, this will guide you to the solution.

Cheers.



来源:https://stackoverflow.com/questions/27950466/how-do-i-get-data-to-show-up-in-angular-ui-grid-from-an-http-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!