分页列表
简介
通常,用户请求的数据可能会填充多个内容页面,因此 Web 应用程序必须提供一种供用户浏览数据页面的方式。此模式出现在搜索结果、产品浏览页面等中。在这里,我们使用 amp-bind
和 amp-list
组件实现分页导航。
设置
首先,我们包含 amp-bind
来跟踪和更改页面状态。
<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
接下来,我们包含 amp-list
来请求和显示产品列表。
<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
最后,我们包含 amp-mustache
来呈现 <amp-list>
中的 Mustache 模板。
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
样式
不需要其他样式,但您可以选择添加样式来更改布局或页面的外观。
实现
我们使用 <amp-list>
元素来呈现产品卡片。 <amp-state>
元素初始化并跟踪响应中的页数。如果任何产品被 <amp-list>
的底部截断,溢出元素可确保用户能够显示更多内容。
第 {{currentPage}} 页,共 {{pageCount}} 页
{{#products}} {{title}} {{/products}}
{{copy}}
<amp-list class="paged-amp-list" layout="fixed-height" height="720" src="https://amp.org.cn/documentation/examples/interactivity-dynamic-content/paged_list/search" [src]="'https://amp.org.cn/documentation/examples/interactivity-dynamic-content/paged_list/search?page=' + pageNumber" binding="no" reset-on-refresh single-item>
<template type="amp-mustache">
<p class="info">Page {{currentPage}} of {{pageCount}}</p>
<div class="items">
{{#products}}
<div class="item">
<amp-img layout="responsive" class="image" width="320" height="240" src="{{image}}"></amp-img>
<strong class="title">{{title}}</strong>
<p class="copy">{{copy}}</p>
</div>
{{/products}}
</div>
</template>
<div overflow>
<button>Show more</button>
</div>
</amp-list>
<div class="navigation">
<button class="prev" hidden [hidden]="pageNumber < 2" on="tap: AMP.setState({ pageNumber: pageNumber - 1 })">
Previous
</button>
<button class="next" [hidden]="page ? pageNumber >= page.items.pageCount : false" on="tap: AMP.setState({ pageNumber: pageNumber ? pageNumber + 1 : 2 })">
Next
</button>
</div>
<amp-state id="page" src="https://amp.org.cn/documentation/examples/interactivity-dynamic-content/paged_list/search" [src]="'https://amp.org.cn/documentation/examples/interactivity-dynamic-content/paged_list/search?page=' + pageNumber">
</amp-state>
需要进一步解释吗?
如果此页面上的解释未能涵盖您所有的问题,请随时与其他 AMP 用户联系,讨论您的具体用例。
转到 Stack Overflow 无法解释的功能?AMP 项目强烈鼓励您的参与和贡献!我们希望您成为我们开源社区的长期参与者,但我们也欢迎您为自己特别感兴趣的问题做出一次性贡献。
在 GitHub 上编辑示例-
作者: @cvializ