ionic 2: Display array of items in a 3x3 table

萝らか妹 提交于 2020-05-30 10:11:08

问题


I have an array of items I would like to display in a 3x3 table like display as follows:

In order to achieve that, I sliced my array into 3 arrays of 3 and displayed it as follows:

<ion-grid no-padding class="home-gallery">
    <ion-row *ngFor="let row of items | async">
        <ion-col *ngFor="let item of row">
            <div class="products_list">
                <div class="products_list_img">
                    <img [src]="item.previewImage" (click)="showItem($event, item)"/>
                </div>    
            </div>
        </ion-col>
    </ion-row>
</ion-grid>

Is there a way to achieve this display while keeping my array flattened?


回答1:


You don't need 3 arrays, just need to understand more about the grid system. It's a 12 columns row, so if it's going to pass 12 columns it pushes the item down and "create" another row, so with a single array do

<ion-grid no-padding class="home-gallery">
    <ion-row>
        <ion-col col-4 *ngFor="let item of items | async"> <!-- JUST ADD A col-4 attribute -->
            <div class="products_list">
                <div class="products_list_img">
                    <img [src]="item.previewImage" (click)="showItem($event, item)"/>
                </div>    
            </div>
        </ion-col>
    </ion-row>
</ion-grid>

So the col-4 will specify to your grid that this tag will take up to 4 columns of the 12 for all screens size.

Hope this helps




回答2:


Use Flexbox to display the results like:

.home-gallery {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
}

And only use one *ngFor to fetch your single array;




回答3:


Usually, doing this in template is a bad idea, because template becomes too complicated and hard to read:

<ion-grid no-padding class="home-gallery">
    <ion-row *ngFor="let rowN of [0,1,2]">
      <ng-container *ngFor="let colN of [0,1,2]">
        <ion-col *ngFor="let item of (flatList | async).slice(rowN*3+colN, rowN*3+colN+1)">
            <div class="products_list">
                <div class="products_list_img">
                    <img [src]="item.previewImage" (click)="showItem($event, item)"/>
                </div>
            </div>
        </ion-col>
      </ng-container>
    </ion-row>
</ion-grid>


来源:https://stackoverflow.com/questions/45821300/ionic-2-display-array-of-items-in-a-3x3-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!