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

前端 未结 1 1732
情歌与酒
情歌与酒 2021-01-14 09:38

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 dat

相关标签:
1条回答
  • 2021-01-14 10:09

    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.

    0 讨论(0)
提交回复
热议问题