获取铁页中的页数
Get the number of pages in iron-pages
我想在铁页中显示当前选择的页数和所有页数(例如"page 1 of 100")。
获取前者很容易,因为您可以查找 selected-changed
事件。对于后者,我不知道如何获得它。我正在考虑获取 iron-pages
中子元素的数量,但这似乎并不可靠。
你能做的就是确保每个页面都有一个对应的数字作为属性。您可以通过评估子元素(忽略后备页面)来获取页面数。
一个工作示例:
<!DOCTYPE html>
<html>
<head>
<base href="//polygit.org/components/">
<script src="webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="polymer/polymer.html">
<link rel="import" href="iron-pages/iron-pages.html">
<dom-module id="example-component">
<template>
<style>
:host {
display: block
}
</style>
<input value="{{pageName::input}}" placeholder="Type a page name here.">
<iron-pages id="ironPages" selected="[[pageName]]" attr-for-selected="page-number" fallback-selection="fallback">
<div page-number="1">One</div>
<div page-number="2">Two</div>
<div page-number="3">Three</div>
<div page-number="fallback">
Change pages by typing '1', '2', or '3' in the input above.
</div>
</iron-pages>
<div id="pageNumbering">Page [[currentPage]] of [[totalPages]]</div>
</template>
<script>
class ExampleComponent extends Polymer.Element {
static get is() {
return 'example-component';
}
static get properties() {
return {
currentPage: {
type: Number,
value: 0,
},
totalPages: {
type: Number,
value: 0,
}
};
}
connectedCallback() {
super.connectedCallback();
// fallback doesn't count
this.totalPages = this.$.ironPages.children.length - 1;
this.addEventListener('iron-select', this._onPageChanged);
}
_onPageChanged() {
let currentPage = this.$.ironPages.selectedItem.getAttribute('page-number');
if (currentPage == "fallback") {
this.$.pageNumbering.hidden = true;
}
if (currentPage <= this.totalPages) {
currentPage = parseInt(currentPage);
this.set('currentPage', currentPage);
this.$.pageNumbering.hidden = false;
}
}
}
customElements.define(ExampleComponent.is, ExampleComponent);
</script>
</dom-module>
</head>
<body>
<example-component></example-component>
</body>
</html>
我想在铁页中显示当前选择的页数和所有页数(例如"page 1 of 100")。
获取前者很容易,因为您可以查找 selected-changed
事件。对于后者,我不知道如何获得它。我正在考虑获取 iron-pages
中子元素的数量,但这似乎并不可靠。
你能做的就是确保每个页面都有一个对应的数字作为属性。您可以通过评估子元素(忽略后备页面)来获取页面数。
一个工作示例:
<!DOCTYPE html>
<html>
<head>
<base href="//polygit.org/components/">
<script src="webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="polymer/polymer.html">
<link rel="import" href="iron-pages/iron-pages.html">
<dom-module id="example-component">
<template>
<style>
:host {
display: block
}
</style>
<input value="{{pageName::input}}" placeholder="Type a page name here.">
<iron-pages id="ironPages" selected="[[pageName]]" attr-for-selected="page-number" fallback-selection="fallback">
<div page-number="1">One</div>
<div page-number="2">Two</div>
<div page-number="3">Three</div>
<div page-number="fallback">
Change pages by typing '1', '2', or '3' in the input above.
</div>
</iron-pages>
<div id="pageNumbering">Page [[currentPage]] of [[totalPages]]</div>
</template>
<script>
class ExampleComponent extends Polymer.Element {
static get is() {
return 'example-component';
}
static get properties() {
return {
currentPage: {
type: Number,
value: 0,
},
totalPages: {
type: Number,
value: 0,
}
};
}
connectedCallback() {
super.connectedCallback();
// fallback doesn't count
this.totalPages = this.$.ironPages.children.length - 1;
this.addEventListener('iron-select', this._onPageChanged);
}
_onPageChanged() {
let currentPage = this.$.ironPages.selectedItem.getAttribute('page-number');
if (currentPage == "fallback") {
this.$.pageNumbering.hidden = true;
}
if (currentPage <= this.totalPages) {
currentPage = parseInt(currentPage);
this.set('currentPage', currentPage);
this.$.pageNumbering.hidden = false;
}
}
}
customElements.define(ExampleComponent.is, ExampleComponent);
</script>
</dom-module>
</head>
<body>
<example-component></example-component>
</body>
</html>