Ionic - 类别

时间:2017-07-18 05:03:56

标签: angular typescript ionic-framework ionic2 ionic3

我正在开发一个带有移动设备的网络/移动应用程序,我们在上面有一个代表类别的水平滚动选项卡。在移动设备上它很好,但在网络上我需要在右侧添加2个闪光灯,在左侧添加一个闪光灯。当用户点击它们时,滚动应该移动到那个方向。

<ion-scroll scrollX="true">
       <ion-segment [(ngModel)]="SelectedSubCategory">
         <ion-segment-button value="" (ionSelect)="SelectSubCategory('')">
                <h6>
                   All Groups
                 </h6>
         </ion-segment-button>
         <ion-segment-button value="{{item.CategoryId}}" (ionSelect)="SelectSubCategory(item.CategoryId)" *ngFor="let item of SubCategories">
            <h6 class="subcategorytext">
                {{item.CategoryName}}
            </h6>
         </ion-segment-button>
       </ion-segment>
     </ion-scroll>

有可能实现吗?

1 个答案:

答案 0 :(得分:19)

事件尽管这些问题可能被认为是另一个问题的重复,但我仍然会添加这个答案,因为我认为有更好的方法来处理类别(至少从UI / UX的角度来看)。 / p>

最终结果如下:

Categories Demo

基本上,我们使用Ionic滑块组件来显示类别,但每张幻灯片最多显示3个类别。

查看:

在视图中我们只需添加一个带有行的工具栏,其中包含3列:一列用于左箭头,另一列用于滑块,最后一列用于右箭头。另请注意,我们在slidesPerView="3"元素中设置了ion-slides属性。

<ion-header>
    <ion-navbar color="primary">
        <button ion-button menuToggle>
            <ion-icon name="menu"></ion-icon>
        </button>
        <ion-title>App Name</ion-title>
    </ion-navbar>
    <ion-toolbar>
        <ion-row class="filters">
            <ion-col class="col-with-arrow" (click)="slidePrev()" no-padding col-1>
                <ion-icon *ngIf="showLeftButton" name="arrow-back"></ion-icon>
            </ion-col>
            <ion-col no-padding col-10>
                <ion-slides (ionSlideDidChange)="slideChanged()" slidesPerView="3">
                    <ion-slide (click)="filterData(category.id)" *ngFor="let category of categories">
                        <p [class.selected]="selectedCategory?.id === category.id">{{ category.name }}</p>
                    </ion-slide>
                </ion-slides>
            </ion-col>
            <ion-col class="col-with-arrow" (click)="slideNext()" no-padding col-1>
                <ion-icon *ngIf="showRightButton" name="arrow-forward"></ion-icon>
            </ion-col>
        </ion-row>

    </ion-toolbar>
</ion-header>

组件代码

然后我们只需要处理选择任何类别时或当前幻灯片更改时要执行的操作:

// Angular
import { Component, Injector, ViewChild } from '@angular/core';

// Ionic
import { IonicPage, Slides } from 'ionic-angular';

// Models
import { CategoryModel } from './../../models/category.model';

@Component({ ... })
export class HomePage {
    @ViewChild(Slides) slides: Slides;

    public selectedCategory: CategoryModel;
    public categories: Array<CategoryModel>;
    public showLeftButton: boolean;
    public showRightButton: boolean;

    constructor(public injector: Injector) { ... }

    // ...

    private initializeCategories(): void {

        // Select it by defaut
        this.selectedCategory = this.categories[0];

        // Check which arrows should be shown
        this.showLeftButton = false;
        this.showRightButton = this.categories.length > 3;
    }

    public filterData(categoryId: number): void {
        // Handle what to do when a category is selected
    }

    // Method executed when the slides are changed
    public slideChanged(): void {
        let currentIndex = this.slides.getActiveIndex();
        this.showLeftButton = currentIndex !== 0;
        this.showRightButton = currentIndex !== Math.ceil(this.slides.length() / 3);
    }

    // Method that shows the next slide
    public slideNext(): void {
        this.slides.slideNext();
    }

    // Method that shows the previous slide
    public slidePrev(): void {
        this.slides.slidePrev();
    }
}

<强>样式:

    .filters {

        ion-col {
            text-align: center;
            font-size: 1.6rem;
            line-height: 1.6rem;

            ion-icon {
                color: #ccc;
            }

            &.col-with-arrow {
                display: flex;
                justify-content: center;
                align-items: center;
            }
        }

        p {
            color: #fff;
            margin: 0;
            font-size: 1.6rem;
            line-height: 1.6rem;
        }

        .selected {
            font-weight: 700;
        }
    }