【问题标题】:Displaying the same data in different ways以不同的方式显示相同的数据
【发布时间】:2019-01-22 08:25:42
【问题描述】:

我有一个 json 文件中的数据列表。我想显示所有月份,但我希望匹配的年份只显示为重要的一年。 我以不同的名称发布了这个问题,因为没有人看到它。 看图片: 该图像显示了相同的数据,但它被分成了年和月。我不想一遍又一遍地重复 2017 这个词,我希望它只是在粉红色块的中间说 2017。问题是它需要与数据相对应,因为数据一直在变化,它说 2017 年很容易变成 2018 年,所以它需要说出数据指定的正确年份。

JSON:

[
{
    "month": "2017-03-01",
    "modelVolume" [{
           "id": 1,
           "model": P213
    }]
}
{
    "month": "2017-03-01",
    "modelVolume" [{
           "id": 1,
           "model": P213
    }]
}
{
    "month": "2017-03-01",
    "modelVolume" [{
           "id": 1,
           "model": P213
    }]
}
{
    "month": "2017-03-01",
    "modelVolume" [{
           "id": 1,
           "model": P213
    }]
}
{
    "month": "2017-03-01",
    "modelVolume" [{
           "id": 1,
           "model": P213
    }]
}
{
    "month": "2017-03-01",
    "modelVolume" [{
           "id": 1,
           "model": P213
    }]
}
{
    "month": "2017-03-01",
    "modelVolume" [{
           "id": 1,
           "model": P213
    }]
}
{
    "month": "2017-03-01",
    "modelVolume" [{
           "id": 1,
           "model": P213
    }]
}
{
    "month": "2017-03-01",
    "modelVolume" [{
           "id": 1,
           "model": P213
    }]
}]

我只使用“月”数据来解决这个问题。

HTML:年份:

<div class="row justify-content-md-center">
    <div class="col-md-auto">
      <table id="year">
        <tr>
          <th class="y17" class="y18" *ngFor="let value of groupVolumes(groupKeys()[0])" [ngClass]="getYear(value.month)">{{ value.month | date: "yyyy" | uppercase }}</th>
        </tr>
      </table>
      </div>
    </div>

月份:

<div class="row justify-content-md-center">
     <div class="col-md-auto" class="scroll">
       <table id ="t2">
          <tr>
            <th class="line">Line 1</th>
            <th class="y18" *ngFor="let value of groupVolumes(groupKeys()[0])" [ngClass]="getYear(value.month)">{{ value.month | date: "MMM" | uppercase }}</th>
          </tr>

【问题讨论】:

  • 而不是做一个 ngFor,为什么不为那个 groupkeys 集合的长度做一个 colspan 你应该结合类属性 - 你不应该有 2

标签: html json angular data-manipulation


【解决方案1】:

这是一个满足您需求的简单原型:
为简单起见,我将 JSON 文件的内容存储在名为 data 的字段变量中。
据我了解,这个文件的内容会动态变化,所以你需要将字段data声明为这个组件的输入,并实现抽象方法ngOnChanges,每次输入时都会调用processData data 更改。
代码如下:

import {Component} from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <table id="year">
      <tr>
        <th *ngFor="let year of availableYears" [attr.colspan]="displayedData[year].length">
          {{ year }}
        </th>
      </tr>

      <tr>
        <ng-container *ngFor="let year of availableYears">
          <td *ngFor="let data of displayedData[year]">
            {{ data.month | date: "MMM" | uppercase }}
          </td>
        </ng-container>
      </tr>
    </table>`,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  // raw data
  public data = [
    {
      month: '2017-02-01',
      modelVolume: [{
        id: 1,
        model: 'P213'
      }]
    },
    {
      month: '2017-07-01',
      modelVolume: [{
        id: 1,
        model: 'P213'
      }]
    },
    {
      month: '2017-11-01',
      modelVolume: [{
        id: 1,
        model: 'P213'
      }]
    },
    {
      month: '2016-09-01',
      modelVolume: [{
        id: 1,
        model: 'P213'
      }]
    },
    {
      month: '2016-10-01',
      modelVolume: [{
        id: 1,
        model: 'P213'
      }]
    },
    {
      month: '2017-03-01',
      modelVolume: [{
        id: 1,
        model: 'P213'
      }]
    },
    {
      month: '2018-08-01',
      modelVolume: [{
        id: 1,
        model: 'P213'
      }]
    },
    {
      month: '2018-05-01',
      modelVolume: [{
        id: 1,
        model: 'P213'
      }]
    },
    {
      month: '2015-04-01',
      modelVolume: [{
        id: 1,
        model: 'P213'
      }]
    }];

  // displayedData is the object that will be used in the template to map the data of each year
  public displayedData = {};

  // availableYears will contain the range of years of data
  public availableYears = [];

  constructor() {
    this.processData();
  }

  private processData() {
    this.data.forEach((item: any) => {
      const itemYear = new Date(item.month).getFullYear();
      if (!this.displayedData[itemYear]) {
        // add the year when it's still not inside availableYears
        this.availableYears.push(itemYear);

        // store the first item corresponding to that year in an array
        this.displayedData[itemYear] = [item];
      } else {
        // if the year already exists in the object we simply append the item to the array
        this.displayedData[itemYear].push(item);
      }
    });

    // optional sort to display years in order
    this.availableYears.sort();
  }
}

【讨论】:

    猜你喜欢
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    相关资源
    最近更新 更多