UI网格不会显示

时间:2016-08-18 20:48:14

标签: angularjs angular-ui-grid

尝试按照here on their website提供的文档,但仍然无法使其发挥作用。 我正在使用AWS DynamoDB扫描表格,并尝试在此UI grid中显示表格中的信息。

我的控制器:

angular.module('App')
  .controller('GridCtrl', ['modelObjects', '$scope', 'dbCall', function(modelObjects, $scope, dbCall) {

    $scope.gridOptions = {
      enablesorting: true,
      columndefs: [{
        field: 'ref id'
      }, {
        field: 'group'
      }, {
        field: 'cost center'
      }, {
        field: 'cost center description',
      }, {
        field: 'recruitment firm',
        enablesorting: false
      }],
    };

    $scope.updateGrid = function(data) {
      // for (var key in data) {
      //   var item = data[key];
      //   for (var key2 in item) {
      //     console.log(item[key2]);
      //     $scope.gridOptions.data = item[key2];
      //   }
      // }
      $scope.gridOptions.data = data;
    };

    $scope.scanPosition = function() {
      var params = {};
      return dbCall('addGrid', params, $scope.check);
    };

    $scope.check = function(err, data) {
      if (err) {
        $scope.results = "Failure: Unable To Connect To Database";
        console.log(err);
      }
      else {
        $scope.results = "Success: Position Table Scanned";
        console.log(data);
        $scope.updateGrid(data);
      }
    };
    setTimeout(function() {
      $scope.scanPosition();
    }, 50);
  }]);

我的观点:

<!DOCTYPE html>
<html>
    <head></head>
    <body ng-app="App">
        <div class="col-lg-10 col-lg-offset-2 col-sm-9 col-sm-offset-3 col-xs-12">
            <div class="container-fluid">
                <br>
                <div class="panel panel-default" style="align: center; border:1px solid #d4d4d4;">
                    <div class="panel-heading" style="text-align: center;">
                        <h3>Grid Page</h3>
                    </div>
                </div>
                <div ui-grid="gridOptions" class="myGrid"></div>
            </div>
        </div>
    </body>
</html>

我的数据库正在运行;我能够在控制台中循环和显示信息。它只是没有显示在网格中。我没有控制台错误。 Console

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

看起来您需要像这样设置网格数据:

$scope.gridOptions.data = data.Items;

由于data.Items是对象数组。

答案 1 :(得分:1)

尝试将“columndefs”更改为“columnDefs”,将“enableorting”更改为“enableSorting”。这就是我从顶部看到的,但我仍在寻找。您在HTML中if之后也错过了“ng-controller="GridCtrl"。此外,请确保您已将网格脚本包含在模块中并注入模块。”

答案 2 :(得分:1)

除了@GDan之外,HTML文件中没有指定控制器。只有ng-app =“App”。尝试在ui-grid指令周围的一个父div中添加ng-controller =“GridCtrl”。

下面的CodePen和Snippet: http://codepen.io/Lethargicgeek/pen/rLbZGp?editors=1010

angular.module('App', ['ui.grid']);

(function() {
  angular.module('App').controller('GridCtrl', ctrlFn);
  ctrlFn.$inject = ['modelObjects', 'dbCall', '$timeout'];

  function ctrlFn(modelObjects, dbCall, $timeout) {
    var $ctrl = this;
    // BINDING 
    $ctrl.gridOptions = {
      enableSorting: true,
      columnDefs: [{
        field: 'ref id'
      }, {
        field: 'group'
      }, {
        field: 'cost center'
      }, {
        field: 'cost center description',
      }, {
        field: 'recruitment firm',
        enablesorting: false
      }],
    };
    // END BINDING

    // INIT
    $timeout(function() {
      scanPosition();
    }, 1000);
    // END INIT

    function updateGrid(data) {
      $ctrl.gridOptions.data = data;
    };

    function scanPosition() {
      var params = {};
      return dbCall.dbCall('addGrid', params, check);
    };

    function check(err, data) {
      if (err) {
        $ctrl.results = "Failure: Unable To Connect To Database";
        console.log(err);
      } else {
        $ctrl.results = "Success: Position Table Scanned";
        console.log(data);
        updateGrid(data);
      }
    };
  }
})();

// Mocked Service modelObjects
(function() {
  angular.module('App').service('modelObjects', svcFn);

  function svcFn() {}
})();

// Mocked Service dbCall
(function() {
  angular.module('App').service('dbCall', svcFn);

  function svcFn() {
    var svc = this;
    svc.dbCall = dbCall;

    function dbCall(action, params, callBackFn) {
      // Should really use a promise rather than a callbackFn
      callBackFn(null, [{
        'ref id': "fooRef",
        'group': "fooGroup",
        "cost center": "fooCostCenter"
      }]);
    }
  }
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
<script src="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js"></script>

<link href="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<html>

<head></head>

<body ng-app="App">
  <div class="col-lg-10 col-lg-offset-2 col-sm-9 col-sm-offset-3 col-xs-12" ng-controller="GridCtrl as $ctrl">
    <div class="container-fluid">
      <div>$ctrl.results: {{$ctrl.results}}</div>
      <div class="panel panel-default" style="align: center; border:1px solid #d4d4d4;">
        <div class="panel-heading" style="text-align: center;">
          <h3>Grid Page</h3>
        </div>
      </div>
      <div ui-grid="$ctrl.gridOptions" class="myGrid"></div>
    </div>
  </div>
</body>

</html>