带有大JSON的简单ngTable,不显示单元格中的任何数据

时间:2016-04-01 14:33:05

标签: javascript angularjs json asp.net-mvc-4 ngtable

我有一个大小约为5 MB的大型json文件,其格式如下。 大约有3000条记录。

我正在使用microsoft mvc 4和angular + ngTable在前端显示这些数据。它需要在所有列上进行搜索和排序。 服务器端代码不接受任何参数,并以下面的格式返回所有3000条记录。

  {
"MR": "Inact",
"EncType": null,
"ClientAlias": 111020.0,
"OrgName": "Zic",
"CharacterAlias": null,
"Account#": 30645147.0,
"MRN": null,
"PlanCode": null,
"Address": "PO Box 123456",
"City": "Richmond ",
"St": "VA",
"ZIP": 23298.0,
"ContactName": "Jonny J",
"Fax": "(111) 111-1111",
"PHONE": "pager 111-1111",
"CriticalValueNotification": null,
"ClientPracticeTesting": null,
"Doctor": "John Smith",
"BeginDate": 36395.0,
"medBeginDate": null
  }

javascript文件(app.js)包含以下代码。 我有一个单独的视图文件,如下面的角度代码所示。

为了便于理解这个问题,我已将所有内容放在下面的一个片段中。

 var flexTable = angular.module("flexTable", ["ngRoute", "ngTable", "ngResource"]);

flexTable.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
    .when('/about', {
        templateUrl: '../../about.html',
        controller: 'tableController'
    })
    .when('/contact', {
        templateUrl: '../../contact.html',
        controller: 'tableController'
    })
    .when('/', {
        templateUrl: '../../table.html',
        controller: 'tableController'
    })
    .when('/table', {
        templateUrl: '../../table.html',
        controller: 'tableController'
    })
    .otherwise({
        redirectTo: '/table'
    });


}]);

flexTable.controller('tableController',['$scope', '$resource' ,'ngTableParams', function ($scope, $resource,ngTableParams) {
    var data = '';
    var api = $resource("/Home/GetData")
    $scope.tableParams = new ngTableParams({}, {
    getData: function (params) {



        var varApiGet = api.get(params.url()).$promise.then(function (data) {
            params.total(data.inlineCount);
            return data.results;
        });
        return varApiGet;
    }
});
}]);

html页面如下所示

  <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

  <!DOCTYPE html>

  <html>
  <head runat="server">
      <meta name="viewport" content="width=device-width" />
      <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" 
          integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
      <link href="../../Assets/css/justified-nav.css" rel="stylesheet" />
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
      <link href="../../Assets/css/ng-table.min.css" rel="stylesheet" />
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
          <script src="http://ajax.aspnetcdn.com/ajax/bootstrap/3.3.6/bootstrap.min.js"></script>
          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
          <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular-sanitize.js"></script>
          <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular-resource.js"></script>
          <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular-route.js"></script>
          <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular-animate.js"></script>
          <script src="../../Assets/js/lib/ng-table.min.js"></script>
          <script src="../../Assets/js/app.js"></script>


  </head>
  <body>
      <div class="container" >
          <!-- The justified navigation menu is meant for single line per list item.
             Multiple lines will require custom code not provided by Bootstrap. -->
  <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Project name</a>
          </div>
          <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
              <li class="active"><a href="#">Home</a></li>
              <li><a href="#about">About</a></li>
              <li><a href="#contact">Contact</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
              <li><a href="#navbar">Default</a></li>
            </ul>
          </div><!--/.nav-collapse -->
        </div>
      </nav>

          <div class="jumbotron">
        <h1>Instructions stuff!</h1>
        <p class="lead">Cras justo odio, dapibus ac facilisis in, egestas eget quam. 
            Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, 
            ut fermentum massa justo sit amet.</p>
        <!--<p><a class="btn btn-lg btn-success" href="#" role="button">Get started today</a></p>-->
    </div>

    <div class="container" ng-controller="tableController">
        <div ng-view>
<div class="row">


    <table ng-table="tableParams" class="table">
        <tr ng-repeat="row in $data track by $index">
            <td title="'EncType'">
                {{row.EncType}}
            </td>
            <td title="'ClientAlias'">
                {{row.ClientAlias}}
            </td>
            <td title="'OrgName'">
                {{row.OrgName}}
            </td>
            <td title="'CharacterAlias'">
                {{row.CharacterAlias}}
            </td>
            <td title="'AddressCity'">
                {{row.AddressCity}}
            </td>
            <td title="'St'">
                {{row.St}}
            </td>
            <td title="'ZIP'">
                {{row.ZIP}}
            </td>
            <td title="'ContactName'">
                {{row.ContactName}}
            </td>
            <td title="'Fax'">
                {{row.Fax}}
            </td>
            <td title="'PHONE'">
                {{row.PHONE}}
            </td>
            <td title="'CriticalValueNotification'">
                {{row.CriticalValueNotification}}
            </td>
            <td title="'Doctor'">
                {{row.Doctor}}
            </td>
        </tr>
    </table>
</div>
        </div>

    </div>

</div>
      <footer class="footer">
          <p>&copy; 2015 Company, Inc.</p>
      </footer>

      </div><!--container-->
  </body>
  </html>

当我在javascript控制器函数中对数据进行硬编码时,表格显示正常,没有搜索和排序功能。当我通过代码中显示的ajax调用进入数据时,它不显示任何数据。只有表格标题。

我可以回答任何人对此有任何疑问。

谢谢, 第

任何帮助表示感谢。

修改

这是适用于我的控制器代码。我还为排序和过滤器添加了一些额外的代码。

flexTable.controller('tableController', ['$scope', '$http', 'ngTableParams', '$filter', '$log'
   , function ($scope, $http, ngTableParams, $filter, $log) { //


   $http.get('/Home/GetData')
        .success(function (data, status) {
            $scope.data = data;
            $scope.tableParams = new ngTableParams({
                page: 1,            // show first page
                count: 10,          // count per page
                sorting: {
                    OrganizationName: 'asc'     // initial sorting
                }
            }
                , {
                    total: $scope.data.length, // length of data
                    getData: function ($defer, params) {
                        // use build-in angular filter
                        var filterData = params.filter() ?
                                            $filter('filter')($scope.data, params.filter()) :
                                            $scope.data;
                        var orderedData = params.sorting() ?
                                            $filter('orderBy')(filterData, params.orderBy()) :
                                            filterData;

                        $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
                    }
                }
             );


        });
}]);

1 个答案:

答案 0 :(得分:0)

您可以使用$http代替$resource。您的回复数据将位于回复的data属性中。请尝试以下方法:

 $scope.tableParams = new ngTableParams({}, {
   getData: function(params) {
     var varApiGet = $http.get(params.url()).then(function(response) {
       var data = response.data;
       params.total(data.inlineCount);
       return data.results;
     });
     return varApiGet;
   }
 });

修改

Here是一个工作小提琴。我将响应模拟为具有一个条目的数组(您在问题中发布的条目)。确保您的回复数据格式正确。