javascript - 通过 dom-repeat 打印 DOM 数组

标签 javascript node.js polymer

我有一个数组graphData。现在我想使用 dom-repeat 来打印它。

<template is='dom-repeat' items='{{internalgraphData}}'>
                        <tr>
                            <td>{{item.asset}}</td>
                            <td>{{item.percentage}}%</td>
                            <td>{{item.investment}}</td>
                            <td>{{item.currentvalue}}</td>
                            <td class="u-value-align"><span class="red-text darken-4">{{item.gain}}</span></td>
                            <td><img src="../images/arrow-red.png"></td>
                            <td class="black-text"><a class="waves-effect waves-light btn white blue-text back" href="equity.html">Select</a></td>
                        </tr>
                    </template>

我的 json 如下...我尝试使用以下标记值“internalgraphData”创建 dom-repeat 结构 我打印了internalgraphData,看起来我成功地将数据从json传输到本地标记值结构“internalgraphData”

 Polymer({  is: 'my-menu',
........
ready : function(graphid,graphData) {
                            this.graphData = [
                                ['Equity', 50,25000,37000,240],
                                ['Bonds', 35,12500,10000,-480],
                                ['Alternates',5, 2000,2000,340],
                                ['Cash',10,1000,1000,-140]
                            ];

                            if (this.graphData.length > 0)
                            {
                                for(i = 0; i< this.graphData.length; i++)
                                {
                                    this.internalgraphData.push({
                                        "asset" : this.graphData[i][0],
                                        "percentage" : this.graphData[i][1],
                                        "investment" : this.graphData[i][2],
                                        "currentvalue" : this.graphData[i][3],
                                        "gain" : this.graphData[i][4]

                                    }); 
                                }

但是我看到 dom-repeat 没有打印。 谁能帮助我们如何实现这一目标?

注意:如果我显式添加标记值...表格就会出现...

this.internalgraphData = [
                        /*['Equity', 50,25000,37000,240],
                        ['Bonds', 35,12500,10000,-480],
                        ['Alternates',5, 2000,2000,340],
                        ['Cash',10,1000,1000,-140]*/

                        {asset: 'Equity', percentage: 50, investment: 25000, currentvalue: 37000, gain: 240 },
                        {asset: 'Equity', percentage: 35, investment: 12500, currentvalue: 10000, gain: -480 },
                        {asset: 'Equity', percentage: 5,  investment: 2000, currentvalue: 2000, gain: 340 },
                        {asset: 'Equity', percentage: 10, investment: 1000, currentvalue: 1000, gain: -140 }
                    ];

最佳答案

this.internalgraphData.push({});

循环中的上述操作不会反射(reflect)在 polymer 数据更改中。下面是在数组中对象的推荐方法。

this.push('internalgraphData', {title:'hey'}); 如果你不能这样做。请使用以下方式

this.updatedgraphData  = JSON.parse(JSON.stringify(this.internalgraphData ));

***updatedgraphData*** 上使用 dom-repeat

关于javascript - 通过 dom-repeat 打印 DOM 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38765558/

相关文章:

polymer - Polymer "reflectToAttribute"和 "notify"之间的确切区别是什么?

gulp - 如何使用 Polymer 实时重新加载代码

javascript - Headless JS 站点测试,每个页面都进行测试

javascript - Node.js:使用异步库并行运行两个任务,并在前两个任务完成后运行最后一个任务

node.js - 更改 API 返回对象中的输出名称

javascript - Node http代理: forward traffic to external https site - crashes

javascript - Polymer - Iron Ajax - 如何处理错误响应

javascript - 平行元件选择器

javascript - 允许向对象添加和删除功能的设计模式

javascript - 使用 mocha,我如何运行名​​称中*没有* (slow) 的所有测试?