ionic3項(xiàng)目實(shí)戰(zhàn)教程 - 第7講 ionic3商品列表頁(yè)的實(shí)現(xiàn)

這一講主要包含以下幾個(gè)部分:

  • 1.創(chuàng)建商品列表頁(yè)
  • 2.根據(jù)分類獲取商品列表
  • 2.展示商品列表

1.創(chuàng)建商品列表頁(yè)

執(zhí)行 ionic g page product-list

7-1.png

實(shí)現(xiàn)點(diǎn)擊首頁(yè)分類跳轉(zhuǎn)到該列表頁(yè):

在home.html中增加click事件

  <div class="categories">
    <ion-grid>
      <ion-row wrap>
        <ion-col text-center tappable col-3 *ngFor="let c of categories" (click)="goProductList(c)">
          <i class="iconfont {{c.Icon}} icon"></i>
          <span class="title">{{c.FavoritesTitle}}</span>
        </ion-col>
      </ion-row>
    </ion-grid>
  </div>

home.ts增加goProductList()函數(shù),

  goProductList(item) {
     this.navCtrl.push('ProductListPage', { item: item });
  }

2.根據(jù)分類獲取商品列表

通過(guò)全局服務(wù)類獲取商品列表數(shù)據(jù),同時(shí)實(shí)現(xiàn)了分頁(yè)加載,products-list.ts完整代碼如下:

products-list.ts

import { AppService, AppGlobal } from './../../app/app.service';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
@IonicPage()
@Component({
  selector: 'page-product-list',
  templateUrl: 'product-list.html',
})
export class ProductListPage {
  hasmore = true;
  products: any;
  selectedItem: any;

  spinner1: boolean = true;

  params = {
    pageNo: 1,
    favoritesId: 0,
  }

  constructor(public navCtrl: NavController, public navParams: NavParams, public appService: AppService) {
    this.selectedItem = this.navParams.get("item");
    this.params.favoritesId = this.selectedItem.FavoritesId;
  }

  ionViewDidLoad() {
    this.getFavoritesItems();
    console.log('ionViewDidLoad ProductListPage');
  }
  getFavoritesItems() {
    this.appService.httpGet(AppGlobal.API.getProducts, this.params, d => {
      this.products = d.data;
      this.params.pageNo += 1;
      this.spinner1 = false;
    });
  }

  doInfinite(infiniteScroll) {
    if (this.hasmore == false) {
      infiniteScroll.complete();
      return;
    }
    this.appService.httpGet(AppGlobal.API.getProducts,this.params, d => {
      if (d.data.length > 0) {
        this.products = this.products.concat(d.data);
        this.params.pageNo += 1;
      } else {
        this.hasmore = false;
        console.log("沒(méi)有數(shù)據(jù)啦!")
      }
      infiniteScroll.complete();
    });
  }
}

3.展示商品列表

還記得上一講中封裝的?商品列表組建ion-product嗎?在這里就可以得到復(fù)用。在使用前同樣需要在product-list.module.ts里進(jìn)行導(dǎo)入。

product-list.module.ts

import { ComponentsModule } from './../../components/components.module';
import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ProductListPage } from './product-list';

@NgModule({
  declarations: [
    ProductListPage,
  ],
  imports: [
    IonicPageModule.forChild(ProductListPage),ComponentsModule
  ],
})
export class ProductListPageModule {}

products-list.html的完整代碼

<ion-header>
  <ion-navbar style="opacity: 0.8" no-border-bottom color="primary">
    <ion-title>{{selectedItem.FavoritesTitle}}</ion-title>
  </ion-navbar>
</ion-header>
<ion-content fullscreen>
  <ion-products [products]="products"></ion-products>
  <ion-row>
    <ion-col class="nodata" text-center *ngIf="!hasmore">
      沒(méi)有商品啦 (^?^*)
    </ion-col>
  </ion-row>
  <ion-infinite-scroll (ionInfinite)="doInfinite($event)">
    <ion-infinite-scroll-content></ion-infinite-scroll-content>
  </ion-infinite-scroll>
</ion-content>

好了,試試看效果。

7-2.gif

怎么樣,復(fù)雜的列表頁(yè)就這樣簡(jiǎn)單的實(shí)現(xiàn)了。
看到到這里,相信大家對(duì)封裝的思想應(yīng)該有自己的認(rèn)識(shí)了。

下一講講解如何設(shè)計(jì)商品詳情頁(yè)。

完!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容