ngTable仅显示第一列

时间:2018-05-02 21:13:51

标签: javascript angularjs ngtable

我正在尝试使用ngTableParams更新我的ngTable,但只显示第一列。

js code:

var dataset = [
  {name: "Hello50", bunny:"Bunny1", age: 50},
  {name: "Hello49", bunny:"Bunny2", age: 49},
  {name: "Hello48", bunny:"Bunny3", age: 48},
  {name: "Hello47", bunny:"Bunny4", age: 47},
  {name: "Hello46", bunny:"Bunny5", age: 46},
];

/** @export */
this.tableParams = new ngTableParams({count: 10}, { data: dataset});

和html:

>   <table ng-table="ctrl.tableParams" class="table" show-filter="true">
>     <tr ng-repeat="user in $data">
>       <td data-title="'Name'" > {{user.name}} </td>
>       <td data-title="'Bunny'" > {{user.bunny}} </td>
>     </tr>   </table>

仅显示第1个(名称)列。

如果我打印出来

<h1>{{ctrl.tableParams}}<br></h1>

然后显示:

  

{ “数据”:[{ “名称”: “Hello50”, “VA”: “Bunny1”, “UA”:50},{ “名称”: “Hello49”, “VA”: “Bunny2”, “UA”:49},{ “名称”: “Hello48”, “VA”: “Bunny3”, “UA”:48},{ “名称”: “Hello47”, “VA”: “Bunny4”,“UA “:47},{” 名称 “:” Hello46" , “VA”: “Bunny5”, “UA”:46}]}

为什么它是“va”和“ua”?我期待那里的“bunnny”和“年龄”。

请帮忙。谢谢!

2 个答案:

答案 0 :(得分:2)

根据docs,我见过的唯一错字是:

this.tableParams = new ngTableParams({count:10},{ data :dataset});

您应该使用数据集。

请参阅以下代码段。

&#13;
&#13;
    angular.module("sample", ["ngTable"])
      .controller("myController", myController);

		var dataset = [
      {name: "Hello50", bunny:"Bunny1", age: 50},
      {name: "Hello49", bunny:"Bunny2", age: 49},
      {name: "Hello48", bunny:"Bunny3", age: 48},
      {name: "Hello47", bunny:"Bunny4", age: 47},
      {name: "Hello46", bunny:"Bunny5", age: 46},
    ];

    myController.$inject = ["NgTableParams"];

    function myController(NgTableParams) {

      this.tableParams = new NgTableParams({}, {
        dataset : dataset
      });
    }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://unpkg.com/ng-table@3.0.1/bundles/ng-table.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<div ng-app="sample" class="container-fluid">
  <div class="row">
    <div class="col-md-3" ng-controller="myController as demo">
      <h3>ngTable</h3>
      <table ng-table="demo.tableParams" class="table table-condensed table-bordered table-striped">
        <tr ng-repeat="user in $data">
          <td data-title="'Name'" filter-data="demo.nameFilter">{{user.name}}
          </td>
          <td data-title="'Bunny'" filter-data="demo.descriptionFilter">{{user.bunny}}
          </td>
          <td data-title="'Age'" filter-data="demo.descriptionFilter">{{user.age}}
          </td>
        </tr>
      </table>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用dataset:dataset而不是data:dataset

body

codesample - https://codepen.io/nagasai/pen/NMgGMb

&#13;
&#13;
this.tableParams = new ngTableParams({count: 10}, { dataset: dataset});
&#13;
var app = angular.module('myApp', ['ngTable']);
app.controller('myCtrl', function($scope, NgTableParams) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
  var dataset = [
  {name: "Hello50", bunny:"Bunny1", age: 50},
  {name: "Hello49", bunny:"Bunny2", age: 49},
  {name: "Hello48", bunny:"Bunny3", age: 48},
  {name: "Hello47", bunny:"Bunny4", age: 47},
  {name: "Hello46", bunny:"Bunny5", age: 46},
];

/** @export */
$scope.tableParams = new NgTableParams({count: 10}, { dataset: dataset})
});
&#13;
&#13;
&#13;