html - 在具有多级分组的 HTML 表格中显示数据

标签 html angular html-table

我必须通过使用 rowspan 进行分组来显示 HTML 表格中的一些数据。 下面是预期的 GUI

enter image description here

我有如下所示的 JSON 数据。 JSON 数据 here

enter image description here

Angular 代码

<table class="table table-fixed" border="1">
            <thead>
               <tr>
        <th>Country</th>
        <th>State</th>
        <th>City</th>
        <th>Street</th>
        <th>Male</th>
        <th>Female</th>
        <th>Others</th>
    </tr>
            </thead>
            <tbody>
                <ng-container *ngFor="let country of Countries">
                    <tr *ngFor="let item of [].constructor(country.NoOfStreets); let streetIdx = index">
                        <ng-container *ngFor="let state of country.States; let stateIdx = index">

                            <td [attr.rowspan]="state.NoOfStreets" style="width: 15%">
                                {{state.StateName}}  
                            </td>

                        </ng-container>

                         <ng-container *ngFor="let state of country.States; let stateIdx = index">
                            <ng-container *ngFor="let city of state.Cities; let cityIdx = index">
                                <td [attr.rowspan]="city.NoOfStreets" style="width: 15%">{{city.CityName}}</td>

                                <ng-container *ngFor="let street of city.Streets; let streetIdx = index">
                                <td style="width: 15%">{{street.StreetName}}</td>
                                <td style="width: 15%">{{street.Male}}</td>
                                 <td style="width: 15%">{{street.Female}}</td>
                                  <td style="width: 15%">{{street.Others}}</td>
                            </ng-container>

                            </ng-container>
                        </ng-container>
                    </tr>
                </ng-container>
            </tbody>

        </table>

我无法生成预期的用户界面。我得到了不同的用户界面,但没有正确呈现。我尝试了将近一周的时间,但没有任何效果。

PLUNK 版本是 https://next.plnkr.co/edit/5nYNZ86BiWDke3GE?open=lib%2Fapp.ts&deferRun=1

最佳答案

你想要的是将所有的streats展平成数组,这样你就可以在它上面循环。平面代码将是:

      const concat = (x,y) => x.concat(y)
      const flatMap = (f,xs) => xs.map(f).reduce(concat, [])


        let states = flatMap(c => c.States.map(s => ({Country:c, State: s})), this.Countries);
        let cities = flatMap(c => c.State.Cities.map(s => ({Country:c.Country, State:c.State, City: s})), states);

        this.streets = flatMap(c => c.City.Streets.map(str => ({Country:c.Country,     State:c.State, City: c.City, Street: str})), cities);

然后轻松检查每个国家、州和城市是否在组中排在第一位,例如:

  <tbody>               
                <tr *ngFor="let str in streets">
                    <td *ngIf="firstCountryInGroup(str)" [rowspan]="numberOfCountry(str)">
                        {{str.Country.CountryName}}
                    </td>
                    <td *ngIf="firstStateInGroup(str)" [rowspan]="numberOfStatse(str)">
                        {{str.State.CityName}}
                    </td>
                    <td *ngIf="firstCityInGroup(str)" [rowspan]="numberOfCities(str)">
                        {{str.City.CityName}}
                    </td>
                    <td>{{str.Street.Name}}<td>
                    <td>{{str.Street.Male}}<td>
                    <td>{{str.Street.Female}}<td>
                    <td>{{str.StreetOthers}}<td>
                </tr>               
            </tbody>

关于html - 在具有多级分组的 HTML 表格中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57201535/

相关文章:

javascript - 从数据库填充数据列表

javascript - 帮助编写正则表达式,仅在 <strong> 标签不存在时用 <strong> 标签包围某些文本

html - 在主 div 中居中 div 和内容

angular - 对 Observable 的订阅永远不会触发

javascript - 使用未知数的键处理对象

javascript - Canvas 工具提示出现在 Canvas 外?

Angular 4 : access template element in other component

html - Colspan 扰乱了表格布局

r - 使用 rvest 抓取带有跨度的 html 表

html - 在 colgroup 上设置行跨度?