简体   繁体   English

使用下拉列表从JSON数据填充表

[英]Populate table with data from json using dropdowns

I'm making a little web app project with bus time tables. 我正在制作一个带有公交时刻表的小型Web应用程序项目。 I'm trying to populate table with data from json using dropdowns. 我试图使用下拉列表中的json数据填充表格。 I don't know if I'm doing the json part right, but I'm stuck with parsing data so that the table would show the correct times for selected bus and stop. 我不知道我是否在正确执行json部分,但我坚持解析数据,以便表格显示所选公共汽车和车站的正确时间。

It has to go like this: select bus number, select bus stop, then correct times appear below in the table. 必须这样:选择公交车号,选择公交车站,然后正确的时间出现在下表中。

HTML select part: HTML选择部分:

Bus No.: <select ng-model="selectedNr" ng-options="x for (x,y) in busData"></select>

Stop name: <select ng-model="selectedStop" ng-options="x for (x,z) in selectedNr.stops"></select>

Then the table: 然后是表:

<table class="time-table">
  <tr ng-repeat="time in busData[selectedNr].stops[selectedStops].time">
    <th>{{time.hour}}</th>
    <td ng-repeat="minute in time.minutes">{{minute}}</td>
  </tr>

Angular part: 角部分:

app.controller("ngCtrl", function ($scope) {
"use strict";
$scope.busData = {
   "bus1":{
       "id":1,
       "stops":{
           "stop1":{
               "id":1,
               "stopName":"stop1",
               "time":[
                   {
                       "hour": 1,
                       "minutes": [11, 21,31,41,51]
                   },
                   {
                       "hour": 2,
                       "minutes": [12, 22,32,42,52]
                   }
               ]


           },
            "stop2":{
               "id":2,
               "stopName":"stop2",
               "time":[
                   {
                       "hour": 3,
                       "minutes": [11, 21,31,41,51]
                   },
                   {
                       "hour": 4,
                       "minutes": [12, 22,32,42,52]
                   }
               ]

           }
       }
   }, (and so on...)

Embeded Plunker 嵌入式柱塞

When you select a bus, the SelectedNr is not the index of the selected element but the whole sub-element in the array so you don't need to ng-repeat for each busData[selectedNr] but just for each selectedNr . 选择总线时,SelectedNr不是选定元素的索引,而是数组中整个子元素的索引,因此您无需为每个busData[selectedNr]进行ng-repeat ,而只需为每个selectedNr进行ng-repeat

Here is a corrected version of the main part in your HTML. 这是HTML中main部分的更正版本。 There is nothing to change with your JSON. JSON没有任何变化。

<main class="content">
    <section class="filter-wrapper">
        <h2>Bus No.:
        <span><select ng-model="selectedNr" ng-options="x for (x,y) in busData"></select></span>
        </h2>
        <h4>Stop name: <span><select ng-model="selectedStop" ng-options="x for (x,z) in selectedNr.stops"></select></span>
        </h4>
    </section>
    <table class="time-table">
        <tr ng-repeat="time in selectedStop.time">
            <th>{{time.hour}}</th>
            <td ng-repeat="minute in time.minutes">{{minute}}</td>
        </tr>
    </table>
</main>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM