微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 聚合物1.0 dom-repeat显示索引从1开始

我正在使用polymer 1.0来创建购物/结账购物车.我的客户可能希望将订单发送到多个地址.在收据中,我需要列出所有带索引的地址:

地址1
约翰·多伊
1234 Main St

地址2
简史密斯
999百老汇

如何让索引从1开始而不是0?

<template is="dom-repeat" items="{{shipping_details}}" as="address">
        <div>Address <span>{{index}}</span></div>
        <div>{{address.name}}</div>
        <div>{{address.line1}}</div>
    </template>

这是我实际代码的简化,但原理是一样的.我试图创建一个js函数,它将索引作为参数并将innerHTML设置为索引,但我不知道它应触发什么事件或如何让它调用函数.

<div>Address <span on-some-event="displayIndex(index)"></span></div>

我是所有这一切的新手,所以你不能详细介绍如何使这项工作.提前谢谢你的帮助!

解决方法

您可以使用 computed binding.

而不是这个:

< span on-some-event =“displayIndex(index)”>< / span>

做这个:

<跨度> {{的displayIndex(指数)}}< /跨度>

displayIndex是这样的:

function (index) {
    return index + 1;
}

注意:
displayIndex可以

>元素原型的方法

<dom-module id="cart-addresses">
    <template is="dom-repeat" items="{{shipping_details}}" as="address">
        <div>Address <span>{{displayIndex(index)}}</span></div>
        <div>{{address.name}}</div>
        <div>{{address.line1}}</div>
    </template>
    <script>
        polymer({
            is: 'cart-addresses',...
            displayIndex: function(index) {
                return index + 1;
            }
            ...
        });
    </script>
</dom-module>

>附加到自动绑定模板的方法

<template is="dom-bind">
        ...             
        <template is="dom-repeat" items="{{shipping_details}}" as="address">
            <div>Address <span>{{displayIndex(index)}}</span></div>
            <div>{{address.name}}</div>
            <div>{{address.line1}}</div>
        </template>
    </template>
    <script>
        var template = document.querySelector('template[is="dom-bind"]');

        ...

        template.displayIndex = function (index) {
            return index + 1;
        };
    </script>

原文地址:https://www.jb51.cc/js/150986.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐