【问题标题】:Display Data from Json Array Using AngularJS使用 AngularJS 显示来自 Json 数组的数据
【发布时间】:2015-11-03 05:08:38
【问题描述】:

我正在尝试使用以下代码在表格中显示来自 JSON 数组的数据。但是所有数据都显示在一行中。它应该显示为每行中的每个记录,其中偶数为灰色,奇数为白色。但是我犯了一些我无法识别的错误。

CSS 样式:

table,td  {
    border: none;
    border-collapse: collapse;
    padding: 5px;
    width:1047px;
    height: 64px;
    left:16px;
    position:relative;
}
table tr:nth-child(odd) {
    background-color: #f1f1f1;
}
table tr:nth-child(even) {
    background-color: #ffffff;
}

My View (It is a partial view which is displayed from index.html):

      <h1 id="title">Shuttle Schedule</h1>
        <table width="100%" cellpadding="4" cellspacing="25" style="margin-top:-118px">
            <thead  align="center">
                <th ng-repeat="row in module.csvInput">{{row.label}}</th>
                <th ng-repeat="row in module.csvInput">{{row.label}}</th>

            </thead>
            <tr align="center" >
                <td ng-repeat="row in module.csvInput" >
                    <div ng-repeat="(id, time) in row.times" ng-if="id <= 8">
                    {{time}}
                    </div>

                </td>
                <td ng-repeat="row in module.csvInput">
                    <div ng-repeat="(id, time) in row.times" ng-if="id > 8">
                        {{time}}
                    </div>

                </td>
            </tr>
        </table>
        <h3 class="noservice">No Service 1:20pm to 2:00pm<br> Driver can make additional runs beyond this schedule if needed.<br>
            D1 is 8910 Ridgeline Blvd. D4 is 8744 Lucent Blvd.</h3>
    </div>

我的 JSON 片段:

{
  "modules": [


    {
      "title": "Shuttle Schedule",
      "feature": "shuttle",
      "order": 4,
      "csvInput": [
        {
          "label": "D1 PickUp",
          "times": [
            "8:00 AM",
            "8:30 AM",
            "9:00 AM",
            "9:30 AM",
            "10:00 AM",
            "10:30 AM",
            "11:00 AM",
            "11:30 AM",
            "12:00 PM",
            "12:30 PM",
            "1:30 PM"
          ]
        },
        {
        "label": "D4 PickUp",
        "times": [
          "7:50 AM",
          "8:20 AM",
          "8:50 AM",
          "9:20 AM",
          "9:50 AM",
          "10:20 AM",
          "10:50 AM",
          "11:20 AM",
          "11:50 AM",
          "12:20 PM",
          "12:50 PM",
          "1:10 PM"
        ]
      }
      ],
      "filter": null,
      "icon": "media/shuttle.svg"
    }
  ],
  "settings": {
    "buildings": 2,
    "floor": 4,
    "timeout": "120 (in seconds)",
    "cssOverride": "custom.css",
    "kiosk_coords": "200,200"
  }
}

我的输出:

所需的样式输出:

【问题讨论】:

  • 如果我这样做,则不会显示任何内容。

标签: javascript css json angularjs


【解决方案1】:

您应该先将 ng-repeat 放在 tr 标签上,而不是放在 td 标签上。 所以应该是这样的:

<table> <tr ng-repeat="entity in list"><td>{{entity.lable}}</td> 
<td ng-repeat="time in entity.times">{{time}}</td></tr></table>

编辑: 如果您希望您的数据在 html 中显示,例如: 实体标签:实体时间 实体标签:实体时间 ...

那么你应该这样做:

<table> <div ng-repeat="entity in list"><tr ng-repeat="time in entity.times><td ng-class-odd="'grey'" ng-class-even="'white'">{{entity.lable}}</td> 
<td ng-class-odd="'grey'" ng-class-even="'white'">{{time}}</td></tr></div></table>

【讨论】:

  • 感谢您的回复。但是如果尝试一下,什么都不会显示。它在一行中显示所有内容。
  • 供参考的所需输出与您提供的数据不匹配。请根据数据显示所需的输出,我可以帮助您。
  • 期望的输出将类似于第二个图像样式。 D1 和 D4 Pickup 中的数据将在每一行中以不同的方式显示,而不是在一行中显示所有数据。
  • 请参阅编辑部分...其余样式您始终可以使用 ngClassOdd 和 ngClassEven 指令进行...
  • 我知道样式可以在奇数行和偶数行上完成。它已经应用了。但问题是所有数据都显示在同一个tr的2个td中。而不是每个不同行(tr)中的 2 td。
【解决方案2】:

您需要展开数据以匹配您希望它们的显示方式

$scope.module = data.modules[0];
var csvInput = $scope.module.csvInput;
$scope.headers = csvInput.map(x => x.label );
var rows = csvInput.map(x => x.times.slice(0, 8)).concat(csvInput.map(x => x.times.slice(8)));
var n = Math.max.apply(null, rows.map(x => x.length));
$scope.rows = [];
for ( i = 0; i < n; i++ ) {
  $scope.rows[i] = rows.map(x => x[i]);
}

并显示该数据

<table width="100%" cellpadding="4" cellspacing="25" style="margin-top:-118px">
  <thead align="center">
    <th ng-repeat="row in headers">{{row}}</th>
    <th ng-repeat="row in headers">{{row}}</th>
  </thead>
  <tbody>
    <tr ng-repeat="row in rows">
      <td ng-repeat="col in row track by $index">{{col}}</td>
    </tr>
  </tbody>
</table>

http://jsfiddle.net/r0005a1t/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-23
    • 2015-05-28
    • 1970-01-01
    • 2016-05-27
    • 2018-09-21
    • 1970-01-01
    • 2016-12-29
    • 2016-11-08
    相关资源
    最近更新 更多