【问题标题】:Creating a table based on json/object javascript基于 json/object javascript 创建表
【发布时间】:2017-08-30 10:15:32
【问题描述】:

您好,我想创建一个基于 json 对象格式的 dummydata 的表(学校时间表)。对象如下所示。

this._days=[
     {
           "day": "",
           "config": {}
        },

        {
           "day": "Monday",
           "config": {
               'timing': [
                   {'time': '9:00AM-10:00AM', 
                   'schedule': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}
                   },
                   {'time': '10:00AM-11:00AM', 
                   'schedule': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}
                   },
                   {'time': '11:00AM-11:30AM', 
                   'schedule': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}
                   },
                   {'time': '12:00PM-12:30PM', 
                   'schedule': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}
                   }
                ]
            }
        },
        {
           "day": "Tuesday",
           "config": {
               'timing': [
                   {'time': '9:00AM-10:00AM', 
                   'schedule': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}
                   },
                   {'time': '10:00AM-11:00AM', 
                   'schedule': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}
                   },
                   {'time': '11:00AM-11:30AM', 
                   'schedule': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}
                   },
                   {'time': '12:00PM-12:30PM', 
                   'schedule': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}
                   }
                ]
            }
        },
    ...

基于我想创建一个表的 dummydata,这样即使对象的大小增加(例如:周六课或额外时间),表也应该自动调整。 该表应该看起来像一个正常的学校时间表,左侧有日期标​​题和时间。我创建了一个带有硬编码值的基本表,

<table width="100%" align="center" height=100%;>
        <tr class="day">
            <th>Time</th>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thrusday</th>
            <th>Friday</th>
            <th>Saturday</th>
        </tr>
        <tr class="time">
            <th>10:00 - 11:00</th>
                <td>Physics-1</td>
                <td>English</td>
                <td></td>
                <td>Chemestry-1</td>
                <td>Alzebra</td>
                <td>Physical</td>
        </tr>

        <tr class="time">
            <th>11:00 - 12:00</th>
                <td>Math-2</td>
                <td>Chemestry-2</td>
                <td>Physics-1</td>
                <td>Hindi</td>
                <td>English</td>
                <td>Soft Skill</td>
        </tr>

        <tr class="time">
            <th>12:00 - 01:00</th>
                <td>Hindi</td>
                <td>English</td>
                <td>Math-1</td>
                <td>Chemistry</td>
                <td>Physics</td>
                <td>Chem.Lab</td>
        </tr>

        <tr class="time">
            <th>01:00 - 02:00</th>
                <td>Cumm. Skill</td>
                <td>Sports</td>
                <td>English</td>
                <td>Computer Lab</td>
                <td>Header</td>
                <td>Header</td>

        </tr>

        <tr class="time">
            <th>02:00 - 03:00</th>
                <td>Header</td>
                <td>Header</td>
                <td>Header</td>
                <td>Header</td>
                <td>Header</td>
                <td>Header</td>
        </tr>
    </table>

我尝试过类似的方法

<div>
    <table style="width:100%; height:200px;">
        <tr>
            <th *ngFor="let row of _days" style="background: grey; color:white"> 
                <h3><b>{{row.day}}</b></h3>
            </th>
        </tr>
        <tr *ngFor="let row of _days">
            <td style="background: grey;color:white">
                <h3><b>{{row.config.timing[row].time}}</b></h3>
            </td>
        </tr>
    </table>
</div>

如何在 javascript 或 Angular 2 (typescript) 中实现这一点?先谢谢各位了

【问题讨论】:

  • 我尝试过这样的事情。 &lt;div&gt; // &lt;table style="width:100%; height:200px;"&gt; // &lt;tr&gt; // &lt;th *ngFor="let row of _days" style="background: grey; color:white"&gt; // &lt;h3&gt;&lt;b&gt;{{row.day}}&lt;/b&gt;&lt;/h3&gt; // &lt;/th&gt; // &lt;/tr&gt; // &lt;tr *ngFor="let row of _days"&gt; // &lt;td style="background: grey;color:white"&gt; // &lt;h3&gt;&lt;b&gt;{{row.config.timing[row].time}}&lt;/b&gt;&lt;/h3&gt; // &lt;/td&gt; // &lt;/tr&gt; // &lt;/table&gt; // &lt;/div&gt;
  • 但没有按预期工作

标签: javascript json angular typescript html-table


【解决方案1】:

您可以在 Angular 2 中使用primeng 数据表。

在您的模块中使用

import {DataTableModule,SharedModule} from 'primeng/primeng';

在 HTML 中给

  <p-dataTable [value]="cars">
    <p-column field="vin" header="Vin"></p-column>
    <p-column field="year" header="Year"></p-column>
    <p-column field="brand" header="Brand"></p-column>
    <p-column field="color" header="Color"></p-column>
</p-dataTable>

在组件中

public cars: Car[];

constructor(private http: Http) { }

ngOnInit() {
    this.http.get('data.json')
    .map(res => res.json())
    .subscribe(data => this.cars = data,
                err => console.log(err),
                () => console.log('Completed'));
}
}

您可以在 Angular2 中使用 PrimeNG 数据表 https://www.primefaces.org/primeng/#/datatable

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • 当然,也会包含基本代码@MartinSchneider。
  • 嗨湿婆。了解primeng数据表。实际上我试图包括primeng的日程安排组件。这正是时间表的样子。但是因为我买不起 jquery 我没用过。
猜你喜欢
  • 2018-06-17
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 2015-09-10
  • 2017-07-22
  • 1970-01-01
相关资源
最近更新 更多