【问题标题】:Create a table from scope object values从范围对象值创建表
【发布时间】:2016-03-28 12:47:09
【问题描述】:

我有一个表,我正试图从我在控制器中创建的范围对象创建。

我希望标题从“rowHeader”中获取值,这可以正常工作。但问题是我不想从“cellValue”属性中获取我的单元格值。

在我的小提琴中,我添加了一个“所需的表”,这就是我希望我的第一种方法中的结果。如果可能的话..

如您所见,我也想在其中一列上使用过滤器。

我想使用这种方法的原因是我可以使用复选框列表来隐藏/显示列,当然也可以在控制器中轻松设置我的表格

小提琴:http://jsfiddle.net/HB7LU/21469/

<!doctype html>
<html ng-app="plunker">

<head>
 <script data-require="angular.js@*" data-semver="1.2.0" src="http://code.angularjs.org/1.2.0/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>

<body>
<div ng:controller="MainCtrl">

<p>My table</p>
<table border="1">
        <thead style="font-weight: bold;">
            <tr>
                <th class="text-right" ng-repeat="column in columnsTest" ng-if="column.checked" ng-bind="column.rowHeader"></th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="row in rows" border="1">
                <td ng-repeat="column in columns" ng-if="column.checked" ng-bind="row[column.cellValue]"></td>
            </tr>
        </tbody>
    </table>

<p>Visible Columns:</p>
<br />
<div class="cbxList" ng-repeat="column in columnsTest">
  <input type="checkbox" ng-model="column.checked">{{column.rowHeader}}
</div>


</div>




<script>
var app = angular.module('plunker', []);

app.filter('percentage', function () {
return function (changeFraction) {
    return (changeFraction * 100).toFixed(2) + "%";
}
});


app.controller('MainCtrl', function($scope) {
  $scope.columnsTest = [
    { checked: true, cellValue: 'ModelName', rowHeader: 'Name' },
    { checked: true, cellValue: 'value1', rowHeader: 'PL' },
    { checked: true, cellValue: 'value1 / value2 | percentage', rowHeader: '+/-' }
  ];

  $scope.rows = [
  { value1: 100, value2: 5, ModelName: "This is a cell value" },
  { value1: 15, value2: 5, ModelName: "This is a cell value2" },
  { value1: 38, value2: 2, ModelName: "This is a cell value3" }
  ];

  });
  </script>
 </body>

 </html>

【问题讨论】:

  • 你能把它放进 plunker (plnkr.co) 吗?
  • “但问题是我不希望从 'cellValue' 属性中获取我的单元格值。”你能解释一下吗?这句话中有一个错字,所以我很困惑你要做什么。

标签: angularjs html-table


【解决方案1】:

代码中有错别字导致问题:

<tr ng-repeat="row in rows" border="1">
  <td ng-repeat="column in columns"
      ng-if="column.checked" ng-bind="row[column.cellValue]"></td>
</tr>

您没有名为 columns 的范围变量,请将其更改为 columnsTest。

<tr ng-repeat="row in rows" border="1">
  <td ng-repeat="column in columnsTest"
      ng-if="column.checked" ng-bind="row[column.cellValue]"></td>
</tr>

【讨论】:

    猜你喜欢
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多