简体   繁体   English

离子2:单击同一离子项目中的其他元素时,将其隐藏在离子列表的离子项目(或离子卡)中

[英]Ionic 2: hide an element in ion-item (or ion-card) of ion-list when other element in the same ion-item is clicked

I have a list of photo objects in a photos object. 我在照片对象中有一个照片对象列表。 These photos are shown in ion-list . 这些照片显示在ion-list And for each item I have a like, comment, and share buttons. 对于每个项目,我都有一个喜欢,评论和分享按钮。

I built the share button with ion-fab so i can open two share buttons (share on facebook, twitter) when the share button in the ion-card is clicked. 我用ion-fab构建了共享按钮,因此当单击ion-card的共享按钮时,我可以打开两个共享按钮(在Facebook,Twitter上共享)。

What I want to do is hide the like and comment buttons when the share ion-fab opens. 我想做的是在共享ion-fab打开时隐藏“赞”和“注释”按钮。

<ion-list>
    <ion-card *ngFor="let photo of photos | async; let i = index;">
        <img [src]="photo.img"/>
        <span>{{photo.caption}}</span>
        <button ion-button>like</button>
        <button ion-button>comment</button>
        <ion-fab right>
            <button ion-fab (click)="shareFabClicked(photo, i)">
                <ion-icon name="share-alt"></ion-icon>
            </button>
            <ion-fab-list side="left">
                <button ion-fab (click)="share(photo, 'facebook')">
                    <ion-icon name="logo-facebook"></ion-icon>
                </button>
                <button ion-fab (click)="share(photo, 'twitter')">
                    <ion-icon name="logo-twitter"></ion-icon>
                </button>
            </ion-fab-list>
        </ion-fab>
    </ion-card>
</ion-list>

Assume that there is sass to style each element in the ion-card . 假设在ion-card每个元素都有样式。

I need help on hiding the like and comment buttons when the share ion-fab opens. 当共享ion-fab打开时,我需要在隐藏“喜欢”和“注释”按钮方面提供帮助。 In general, how do you refer elements in an ion-item (or ion-card ) of an ion-list and manipulate each element without affecting elements of other ion-item s 通常,如何引用ion-listion-item (或ion-card )中的元素并在不影响其他ion-item元素的情况下操纵每个元素

Note: One thing i can do (but chose not to do) is have flags in the photo object to hide and show each button. 注意:我可以做的一件事(但选择不做)是在照片对象中具有标记以隐藏和显示每个按钮。 The reason I did not want to go with this approach is the photos object can be very big and I did not want to add more data to it. 我不想使用这种方法的原因是,照片对象可能很大,并且我不想向其中添加更多数据。

Thank you! 谢谢!

Answering my question based on @Akash Nigam's solution. 根据@Akash Nigam的解决方案回答我的问题。 Thanks @Akash. 谢谢@Akash。 I went further and came up with the solution below and avoided using arrays and adding extra flags to the photos object. 我走得更远,提出了下面的解决方案,避免使用数组并向photos对象添加额外的标志。

The solution involves two TypeScript variables, ngClass, and one sass class. 该解决方案涉及两个TypeScript变量ngClass和一个sass类。 And there is also a reference to the share ion-fab. 并且还提到了共享离子晶圆厂。

In the HTML code, [ngClass]="{'hide' : hide && index==i} is added in the like and comment tags. And #shareFab is added in the ion-fab tag. 在HTML代码中,在like和comment标签中添加了[ngClass]="{'hide' : hide && index==i}而在ion-fab标签中添加了#shareFab

<ion-list>
    <ion-card *ngFor="let photo of photos | async; let i = index;">
        <img [src]="photo.img"/>
        <span>{{photo.caption}}</span>

        <!-- apply the 'hide' class if hide = true and index = i -->
        <button ion-button [ngClass]="{'hide' : hide && index==i}>
            like
        </button>

        <button ion-button [ngClass]="{'hide' : hide && index==i}>
            comment
        </button>

        <ion-fab right #shareFab>
            <button ion-fab (click)="shareFabClicked(photo, i, shareFab)">
                <ion-icon name="share-alt"></ion-icon>
            </button>
            <ion-fab-list side="left">
                <button ion-fab (click)="share(photo, 'facebook')">
                    <ion-icon name="logo-facebook"></ion-icon>
                </button>
                <button ion-fab (click)="share(photo, 'twitter')">
                    <ion-icon name="logo-twitter"></ion-icon>
                </button>
            </ion-fab-list>
        </ion-fab>
    </ion-card>
</ion-list>

And in the sass code, the hide class is defined: 在sass代码中,定义了hide类:

.hide {
  visibility: hidden;
}

And finally, in the TypeScript code, in the share method, the hide and index variables are set to hide/show the like and comment buttons according to the state of the share fab button: 最后,在TypeScript代码的share方法中,根据share fab按钮的状态,将hide和index变量设置为隐藏/显示like和comment按钮:

hide: Boolean; hide:布尔值; index: Number; 索引号;

shareFabClicked(p, i, shareFab: FabContainer) { console.log('share fab clicked'); shareFabClicked(p,i,shareFab:FabContainer){console.log('share fab clicked');

if (shareFab._listsActive) {
  console.log('fab closing');
  this.hide = false;
} else {
  console.log('fab opening');
  this.hide = true;
}
this.index = i;

// TODO: share functionality

} }

So basically, if index is equal to the index of the selected row and hide is true, the like and comment of the same row will be hidden; 因此,基本上,如果index等于所选行的索引并且hide为true,则同一行的like和comment将被隐藏; otherwise those two buttons are visible. 否则,这两个按钮是可见的。

You can have an array for hidden button photos which stores flags for only photos whose like and comment buttons have been hidden: 您可以为隐藏按钮的照片创建一个数组,该数组仅存储其“喜欢”和“评论”按钮被隐藏的照片的标志:

public hidden: boolean[] = [];

In your html: 在您的html中:

<button ion-button *ngIf="!hidden[i]">like</button>
<button ion-button *ngIf="!hidden[i]">comment</button>

In shareFabClicked() function: 在shareFabClicked()函数中:

public shareFabClicked(photo: any, i: number): void {
    if (this.hidden[i]) {
        this.hidden.splice(i, 1); // Remove the flag if the buttons are already hidden, so that they get displayed again.
    } else {
        this.hidden[i] = true;
    }
}

So by using this way, you don't have to add data to the photos object. 因此,通过这种方式,您不必将数据添加到photos对象。 And more importantly your flag array won't ever get too big as you are only storing the flags for buttons which have to be hidden. 更重要的是,您的标志数组永远不会变得太大,因为您只存储必须隐藏的按钮的标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM