Search code examples
angularionic-frameworkionic2event-handlingionic3

How to bind events of <ion-segment> and <ion-slides> in ionic 3?


I am making an e-commerce app. On my home page i am displaying my products by category. i am using both ion-segment and ion-slides so that user will be able to scroll and select both at a time . it was fine when i had three categories , all were displaying at home but when i increased category other categories are behind screen . categories products are displaying fine but category name is inside and not visible. i want to scroll both category name and products displayed at same time on screen. that is i want to get the active segment visible on the screen, when the segment is off screen .thanks in advance :-) here is the problem

<ion-content>
<ion-segment color="dark" [(ngModel)]="tabs" style="width:640px;">
    <ion-segment-button *ngFor="let i of productcategory" (click)="selectTab(i)" value="{{i.inc}}" >{{i.category_name}}</ion-segment-button>
    <div id="slide" class="slide"></div>
</ion-segment>

<ion-slides #pageSlider (ionSlideWillChange)="changeWillSlide($event)">
    <ion-slide *ngFor="let i of productcategory">

            <ion-list>
                    <div *ngFor="let pr of products; let i=index" text-wrap class="swipe-tabs">

                            <div class="swipe-tabs-1">  
                                    <img src="http://example.com/{{pr.tbl_product_image}}" />
                                    <button ion-button (click)="removefromcart(pr,i)">-</button>
                                    <button ion-button color="light" *ngIf="check">{{quan[i]}}</button>
                                    <button ion-button color="light" *ngIf="!check">0</button>
                                    <button ion-button (click)="addToCart(pr,i)">+</button>
                                </div>
                        <div class="swipe-tabs-2">
                            <h2>{{pr.tbl_product_name}} </h2>

                            <ion-list  radio-group  >
                                <ion-item *ngFor="let p of pr.pricevariant; let j=index;">
                                    <ion-label>{{p.weight}} &nbsp; &nbsp; <span class="colr-red">₹ {{p.dprice}}</span></ion-label>
                                    <ion-radio  [value]="p.weight" (ionSelect)="getdata(pr,p,i,j)" ></ion-radio>                                
                                </ion-item>
                            </ion-list>
                        </div>
                        <div class="border-hr"></div>
                    </div>

                </ion-list> 
    </ion-slide>
</ion-slides>


Solution

  • I did it by making a content in header and adjusting some events during sliding:-

    <ion-header>
    <ion-content #scroll scrollX="true" scrollY="true" style="height: 50px;">
        <ion-segment class="SwipedTabs-tabs">
            <ion-segment-button *ngFor='let tab of tabs ; let i = index ' value="IngoreMe" (click)="selectTab(i)" [ngClass]='{ "SwipedTabs-activeTab" : ( this.SwipedTabsSlider  && ( this.SwipedTabsSlider.getActiveIndex() === i || (  tabs.length -1 === i&& this.SwipedTabsSlider.isEnd()))) }'
                [ngStyle]="{'width.px': (this.tabElementWidth_px)}">
                {{tab}}
            </ion-segment-button>
        </ion-segment>
        <!-- here is our dynamic line  "indicator"-->
        <div id='indicator' class="SwipedTabs-indicatorSegment" [ngStyle]="{'width.px': (this.tabElementWidth_px)}"></div>
    </ion-content>
    

    <ion-slides #SwipedTabsSlider (ionSlideDrag)="animateIndicator($event)" (ionSlideWillChange)="updateIndicatorPosition()" (ionSlideDidChange)="updateIndicatorPosition()" (pan)="updateIndicatorPosition()" [pager]="false">
    

    In typescript:-

        this.tabs=["SNACKS","SWEETS","NAMKKEN","BAKERY","MISC","ETC"]; 
    
     selectTab(index) {
    this.SwipedTabsIndicator.style.webkitTransform = 'translate3d('+(100*index)+'%,0,0)';
     this.scroll.scrollTo(index*this.tabElementWidth_px,0,500);
    this.SwipedTabsSlider.slideTo(index, 500);}
    
    updateIndicatorPosition() {
    this.scroll.scrollTo(this.SwipedTabsSlider.getActiveIndex()*this.tabElementWidth_px,0,200);
    
      // this condition is to avoid passing to incorrect index
    if( this.SwipedTabsSlider.length()> this.SwipedTabsSlider.getActiveIndex())
    {
      this.SwipedTabsIndicator.style.webkitTransform = 'translate3d('+(this.SwipedTabsSlider.getActiveIndex() * 100)+'%,0,0)';
    }
    this.j=this.SwipedTabsSlider.getActiveIndex();
    }
    

    And in css:

    page-home {
    .segment-ios .segment-button {
        overflow: inherit !important;
        color: #fff !important;
        padding: 0 10px;
        border-right: 1px solid white !important;
        width: auto !important;
    }
    .SwipedTabs-indicatorSegment {
        -webkit-transition: 0.3s all;
        -moz-transition: 0.3s all;
        -o-transition: 0.3s all;
        transition: 0.3s all;
        transform-origin: top 0 left 0;
        height: 1px;
        position: relative;
        top: -2px;
        background-color: white !important;
    }
    .SwipedTabs-tabs ion-segment-button {
        border: none !important;
        color: black!important;
        background-color: blue!important;
        display: -webkit-inline-box;
    }
    .SwipedTabs-tabs ion-segment-button.SwipedTabs-activeTab {
        color: white !important;
        font-size: 17px;
        font-weight: bold;
    }
    .SwipedTabs-tabs {
        width: fit-content !important;
        border-bottom: solid 1px #e6e6e6 !important;
    }
    .scroll-content {
        display: flex;
        left: -3px;
        right: 0;
        top: 3px;
    }
    .swiper-container {
        overflow-y: scroll !important;
    }
    scroll-content {
        margin-top: 35px;
        margin-bottom: -297px;
    }
    .SwipedTabs-tabs ion-segment-button {
        color: #fff !important;
        padding: 0 10px;
    }
    

    }