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>

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...