基于json / object javascript创建表 [英] Creating a table based on json/object javascript

查看:261
本文介绍了基于json / object javascript创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi I want to create a table(school timetable) based on a dummydata which is in json object format. The object looks like below.

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上我想创建一个表,这样即使对象增加的大小(例如:星期六类或额外时间),表也应该自动调整。
该表应该看起来像普通的学校时间表,左侧有天数标题和时间。我创建了一个带有硬编码值的基本表,

Based on the dummydata I want to create a table, so that even if the size the object increases(ex: saturday class or extra time) the table should auto adjust. The table should look like a normal school time table with days headers and time on the left. I created a basic table with hardcoded values,

<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(打字稿)中实现这一点?在此先感谢你们

How to achieve this in javascript or Angular 2 (typescript)? Thanks in advance guys

推荐答案

你可以在角度2中使用primeng数据表。

you can use primeng datatable in angular 2.

模块中使用

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>

in component

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 $ b中使用PrimeNG数据表$ b https://www.primefaces.org/primeng/#/datatable

You may make use of PrimeNG datatable in Angular2 https://www.primefaces.org/primeng/#/datatable

这篇关于基于json / object javascript创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆