如何从 $http 请求中获取数据以显示在 angular ui.grid 中 [英] How do I get data to show up in angular ui.grid from an $http request

查看:23
本文介绍了如何从 $http 请求中获取数据以显示在 angular ui.grid 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Typescript 和 AngularJS.我有一个包含 ui.grid 指令的模板.

显示网格,稍后 $http 请求获取我希望网格包含的数据.

我尝试将 gridOptions.data 设置为数组,并在 $http 调用的成功函数中将用于 gridOptions.data 的数组设置为返回的数据.

网格不显示任何内容.

有什么想法吗?

我将把 Roman 的答案标记为正确,但由于我指定我使用的是 Typescript,为了完整性,我将展示我的解决方案:

导出类 MyController {构造函数 ( ) {//...用于设置网格、col defs、数据为空数组等的代码...var myDataPromise = this.myHttpService.getDataForGrid();myDataPromise.then((data) => {this.gridOptions.data = data["value"];},(原因)=>{ },(更新) =>{ });}}

我的模板:

我不知道为什么我的原始代码不能正常工作......我认为 Typescript 和 Angular 在一起只是在早期煎炸我的大脑......

解决方案

尝试以下操作:

<html><头><meta charset="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/><body ng-app="app"><div ng-controller="gridController"><!-- 使用 ng-init 调用初始化网格 --><div ui-grid="gridOptions" ng-init="GetGridData(urlList)">

<script src="Scripts/ng/angular.min.js"></script><script src="脚本/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",函数 ($scope, $attrs, $element, $compile, $http, $q){$scope.urlList = "YourSourceUrl";函数 fnGetList(url){var deferred = $q.defer();$http.get(url).成功(功能(数据){deferred.resolve(data);}).错误(函数(错误消息){警报(错误消息);延期.拒绝;});返回 deferred.promise;};$scope.GetGridData = 函数(网址){console.log("在 GetGridData 中:" + "正在获取数据");//仅测试 - 如果您需要静态测试网格,请取消注释.//$scope.loadData = ([//{//"用户名": "Joe.Doe",//"电子邮件": "Joe.Doe@myWeb.com"//},//{//"用户名": "Jane.Doe",//"电子邮件": "Jane.Doe@myWeb.com"//},//]);//返回;fnGetList(url).then(函数(内容){//假设你的 content.data 包含一个格式良好的 JSON如果(内容数据!== 未定义){$scope.loadData = JSON.parse(content.data);}});};$scope.gridOptions ={数据:'加载数据',列定义:[{字段:'用户名',名称:'用户'},{ 字段:'电子邮件',名称:'电子邮件' }]};}]);

如果不想立即填充网格,请删除对

的调用<块引用>

ng-init

并在其他事件上调用相关函数.

希望在不了解您的具体问题的情况下,这将引导您找到解决方案.

干杯.

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...

解决方案

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.

这篇关于如何从 $http 请求中获取数据以显示在 angular ui.grid 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆