【问题标题】:Dropdown populated from json data从 json 数据填充的下拉列表
【发布时间】:2018-07-19 14:39:14
【问题描述】:

我希望下拉菜单显示我的数据中的 2017 年和 2018 年。 2017 年和 2018 年在我的 json 数据文件中重复了很多。但我希望在选择时显示所有 2017 年数据,并在选择时显示所有 2018 年数据。目前它显示所有数据并且下拉列表被过度填充。

这是下拉菜单的 Html 代码:

<div class="row justify-content-center">
        <div class="col-4s">
        <p>Financial Year:</p>
        </div>
        <div class="col-4s">
            <select>
                <option *ngFor="let volumes of volumes">{{ volumes.month | 
                 date: 'yyyy' }}</option>
            </select>
        </div>
    </div>

Angular4 代码: 导出类 VehicleVolumeEditComponent 实现 OnInit {

volumes: Volumes[];
groupedVolumes : any;

constructor(private volumeService: VolumeService, private router: Router) { 
    this.volumeService.getVolumes().subscribe(volumes => {
        this.volumes = volumes;
        this.groupedVolumes = this.group(this.volumes);
        this.dataOk = true;
    }
}

json 文件:

[
{
    "id": 1,
    "month": "2017-03-01"
}
{
    "id": 2,
    "month": "2017-04-01"
}
{
    "id": 3,
    "month": "2017-05-01"
}
{
    "id": 4,
    "month": "2017-06-01"
}
{
    "id": 5,
    "month": "2017-07-01"
}
{
    "id": 6,
    "month": "2017-08-01"
}
{
    "id": 7,
    "month": "2017-09-01"
}
{
    "id": 8,
    "month": "2017-10-01"
}
{
    "id": 9,
    "month": "2017-11-01"
}
{
    "id": 10,
    "month": "2017-12-01"
}
{
    "id": 11,
    "month": "2018-01-01"
}
{
    "id": 12,
    "month": "2018-02-01"
}
{
    "id": 13,
    "month": "2018-03-01"
}
]

【问题讨论】:

  • 您应该在 component.ts 文件的一个变量中维护唯一年份数组并使用它
  • 问题是什么时候连接到数据库,那些年会一直在变化。所以需要从json数据中填充
  • 请发布您的 component.ts 代码和卷的结构
  • 我已经添加了我的 component.ts 代码和我的卷文件

标签: arrays json angular dropdown


【解决方案1】:

我会像这样过滤掉数组中的唯一值 -

this.unique_volumes = this.volumes.slice().map(value => value.month = new Date(value.month).getFullYear()).filter((years, index, array) => array.indexOf(years) === index);

然后在 HTML 中,我会遍历 unique_volumes 而不是 volumes

<option *ngFor="let volumes of unique_volumes">{{ volumes }}</option>

【讨论】:

  • 这个有问题。 value.month 显示错误:无法将数字分配给日期。
  • @L.C 还使用上面提供的内容更新 HTML 代码。您必须使用日期管道更改volumes.month,只需{{ volumes }}。
  • 这个问题是 slice() 函数。这需要一个数字。
【解决方案2】:

试试这个

import {DatePipe} from '@angular/common';
.
.
volumes: Volumes[];
years: [] = [];
groupedVolumes : any;

constructor(private volumeService: VolumeService, private router: Router, private datePipe: DatePipe) { 

}


ngOnInit(){
    this.volumeService.getVolumes().subscribe(volumes => {
        this.volumes = volumes;
        for(let volume of volumes){
          if(this.years.indexOf(datePipe.formatDate(volume.month, 'yyyy')) === -1)
           this.years.push(datePipe.formatDate(volume.month, 'yyyy'));
        }
        this.groupedVolumes = this.group(this.volumes);
        this.dataOk = true;
    }

}

在 html 中使用:

<div class="row justify-content-center">
    <div class="col-4s">
    <p>Financial Year:</p>
    </div>
    <div class="col-4s">
        <select>
            <option *ngFor="let year of years">{{ year }}</option>
        </select>
    </div>
</div>

【讨论】:

  • 这似乎是正确的,但我正在努力让它发挥作用。 DatePipe 似乎有问题。它只有一个转换函数而不是 formatDate。它也不喜欢年份:[ ] = [ ]
猜你喜欢
  • 2021-06-19
  • 2021-04-04
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 2014-12-25
相关资源
最近更新 更多