合并具有相同值的div(merge divs with same value)

2019-10-28 19:57发布

我想合并有才能看到周和其编号相同数量的div。

这是我的HTML看起来像

<div class="wrapper clearfix">
        <div class="left">
            <label class="kw">WeekNo.</label>
        </div>
        <div class="right week-number" *ngFor="let day of days">
            <label class="number-label"><span>{{day.weekNumber}} </span></label>
        </div>
    </div>

我必须使用*ngIf或者只是CSS? 我已阅读本如何合并两个div ,但是我不知道在哪里TI包括if语句

这是我使用的类

export class Day {
    number: number;
    weekDay: number;
    name: string;
    weekNumber: number;

    constructor(number: number, weekDay: number, name: string, weekNumber: number
       ) {
            this.number = number;
            this.weekDay = weekDay;
            this.name = name;
            this.weekNumber = weekNumber;
        }
}

这是它应该如何看afet合并

Answer 1:

您可以通过在项目创建GROUPBY管包括引导4做下面的方式。 这是一个普通的工作演示(你会做造型) 演示 :

groupby.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'groupBy'})
export class GroupByPipe implements PipeTransform {
    transform(collection: any[], property: string): any[] {
        // prevents the application from breaking if the array of objects doesn't exist yet
        if(!collection) {
            return null;
        }

        const groupedCollection = collection.reduce((previous, current)=> {
            if(!previous[current[property]]) {
                previous[current[property]] = [current];
            } else {
                previous[current[property]].push(current);
            }

            return previous;
        }, {});

        // this will return an array of objects, each object containing a group of objects
        return Object.keys(groupedCollection).map(key => ({ key, value: groupedCollection[key] }));
    }
}

HTML代码

<div class="table table-borderless ">
  <div class="col-md-12 text-center border"><span>{{currentMonthName}}</span>&nbsp; <span>{{currentYear}}</span></div>

    <div class="d-flex">
      <div class=" text-center m-2 border" *ngFor="let day of days | groupBy:'weekNumber'">
        {{day.key}}
      <table class="table">
          <tbody>
            <tr>
              <th scope="row"  *ngFor="let x of day.value ">{{x.number}}</th>
            </tr>
            <tr>
              <th scope="row"  *ngFor="let y of day.value ">{{y.name}}</th>
            </tr>

          </tbody>
      </table>
      </div>
    </div>

</div>


文章来源: merge divs with same value
标签: html css angular