从fxLayoutGap删除行的最后一个元素的填充

时间:2018-09-04 13:57:55

标签: angular angular-material angular-flex-layout

我在使用fxLayoutGap时遇到问题。我列表的最后一列有填充,我不想要。

如何保留fxLayoutGap(卡之间的空间)并从最后一列中删除填充?谢谢!

 <div fxFlexFill fxLayout="row wrap" fxLayout.xs="column" fxLayout.sm="column" fxLayoutGap="20px grid" style="margin-right:-20px;">
                        <div fxHide.lt-sm="true" fxFlex="calc(25%-20px)" fxFlex.md="33">
                            <div fxFlexFill fxLayoutAlign="center center">
                                <mat-card (click)="addAdvert()" class="mat-card-add-button">
                                    <div fxLayout="row" fxLayoutAlign="center center" fxFlex="100%">
                                        <span style="font-size:32px;text-align:center">+<br />test</span>
                                    </div>
                                </mat-card>
                            </div>
                        </div>
                        <div fxFlex="calc(25%-20px)" fxFlex.md="33" *ngFor="let product of products | slice:0:3">
                            <div style="border:1px solid #ccc" fxFlexFill fxLayoutAlign="center stretch">
                                <mat-card class="ad">
                                    <img mat-card-image src="https://material.angular.io/assets/img/examples/shiba2.jpg" alt="Photo of a Shiba Inu">
                                    <mat-card-title>test</mat-card-title>
                                    <mat-card-content>
                                        <p>
                                            test
                                        </p>
                                    </mat-card-content>
                                    <mat-divider [inset]="true"></mat-divider>
                                    <mat-card-actions align="end">
                                        <button mat-icon-button matTooltip="edit" matTooltipShowDelay="800">
                                            <mat-icon>mode_edit</mat-icon>
                                        </button>

                                        <button mat-icon-button matTooltip="delete" matTooltipShowDelay="800">
                                            <mat-icon>delete</mat-icon>
                                        </button>
                                    </mat-card-actions>
                                </mat-card>
                            </div>
                        </div>
                    </div>

问题(红色):

enter image description here

编辑:

我使用了Abdul Rafay的解决方案,但这是一个新问题:

enter image description here

4 个答案:

答案 0 :(得分:2)

就做这个小技巧。

移除 fxLayoutGap 并为子元素添加边距,并将相同边距的负值添加到父元素。

<div fxLayout="row wrap" style="margin-right: -16px;">
    <div fxFlex="calc(50% - 16px)" style="margin-right: 16px;">

    </div>
    <div fxFlex="calc(50% - 16px)" style="margin-right: 16px;">

    </div>
</div>

答案 1 :(得分:1)

编辑:尝试在此处通过ngFor为元素分配类:

<div fxFlexFill fxLayout="row wrap" fxLayout.xs="column" fxLayout.sm="column" fxLayoutGap="20px grid" style="margin-right:-20px;">
  <div fxFlex="calc(25%-20px)" fxFlex.md="33"
    *ngFor="let product of products;let i = index"
    [ngClass]="'product-'+getClassName(i)">
    ...

.ts

  counter: number = 0;

  getClassName(index) {
    if(index%4 === 0)
      this.counter = 0;
    this.counter++;
    switch(this.counter) {
      case 1:
        return "first"
      case 2:
        return "second"
      case 3:
        return "third"
      default:
        return "fourth"
    }
  }

然后添加以下css样式:

.product-first[fxFlex] {
  padding-right: 20px !important;
}
.product-second[fxFlex] {
  padding-left: 6.66px !important;
  padding-right: 13.34px !important; 
}
.product-third[fxFlex] {
  padding-left: 13.34px !important;
  padding-right: 6.66px !important;
}
.product-fourth[fxFlex] {
  padding-left: 20px !important;
  padding-right: 0 !important;
}

我不确定这是否是最好的方法,但它是否有效。

旧的ans::只需添加css样式:

[fxFlex]:last-of-type { padding-right:0px !important; }

如果有不止一行:

[fxFlex]:nth-child(4n) { padding-right:0px !important; }

如果fxFlex元素更多,则可以分配一个类:

<div fxFlexFill fxLayout="row wrap" fxLayout.xs="column" fxLayout.sm="column" fxLayoutGap="20px grid" style="margin-right:-20px;">
    <div class="padding-fix" fxHide.lt-sm="true" fxFlex="calc(25%-20px)" fxFlex.md="33">
    ...

.padding-fix[fxFlex]:nth-child(4n) { padding-right:0px !important; }

答案 2 :(得分:0)

您可以使用ngFor中的索引指定行中的最后一项,可以说4列:

[class.flexLastItem]="((i-3) % 4 === 0)"

最后就是CSS

.flexLastItem{ margin-right: 0 !important; }

答案 3 :(得分:0)

在 stackoverflow 上找到:您必须在间隙中添加网格。

fxLayoutGap="5rem grid"

查看文档: https://github.com/angular/flex-layout/wiki/fxLayoutGap-API

相关问题