为对象计算的聚合物
Polymer computed for an object
我在Polymer中有一个web组件,我正在做一些基本组件。我想做一个基本的表,在那里我可以设置一个JSON,然后用这些数据打印表。
目前,我已经准备好了"json",可以先探测它。
<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/iron-ajax/iron-ajax.html">
<dom-module id="table-custom">
<template>
<style>
.table {
border: 1px solid;
}
</style>
<iron-ajax auto url="../data.json" last-responde="{{data}}" handle-as="json">
</iron-ajax>
<table class="table">
<tr class="rows">
<template is="dom-repeat" items="{{dataContent.titles}}">
<th> {{item.name}}</th>
<th> {{item.surName}}</th>
<th>{{item.lastName}}</th>
</template>
</tr>
<template is="dom-repeat" items="{{dataContent.contents}}">
<tr>
<template is="dom-repeat" items="{{valuesContent}}">
<td>{{item}}</td>
</template>
</tr>
</template>
</table>
</template>
<script>
Polymer({
is: 'table-custom',
ready: function() {
this.dataContent = {
contents: [{
name: 'Juan',
surname: 'Garrafaeustaquio',
lastname: 'Sin gas'
}, {
name: 'Paco',
surname: 'Lindort',
lastname: 'chocolate'
}, {
name: 'Pepe',
surname: 'Pilot',
lastname: 'no ve'
}],
titles: [{
name: 'Name',
surName: 'Surname',
lastName: 'Lastname',
age: 'Edad'
}]
};
},
properties: {
valuesContent: {
type: Object,
computed: 'computedValuesContent(dataContent)'
}
},
computedValuesContent: function(dataContent) {
var myArray = dataContent.contents;
myArray.forEach(function(content) {
});
}
});
</script>
</dom-module>
我需要做一个计算函数,因为我想获得template
和dom-repeat
中的值,模板只有一个changer td
元素,所有元素都在那里绘制,并且没有必要像th
中那样通过逐个添加来修改模板。
如何使计算的函数返回值并绘制在td
中
这应该可以工作(检查jsbin)
<!doctype html>
<html>
<head>
<base href="https://polygit.org/components/">
<script src="webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="polymer/polymer.html">
</head>
<body>
<dom-module id="table-custom">
<template>
<style>
.table {
border: 1px solid;
}
</style>
<table class="table">
<tr class="rows">
<template is="dom-repeat" items="{{dataContent.titles}}">
<th>{{item.name}}</th>
<th>{{item.surName}}</th>
<th>{{item.lastName}}</th>
</template>
</tr>
<template is="dom-repeat" items="{{dataContent.contents}}">
<tr>
<template is="dom-repeat" items="{{computedValuesContent(item)}}">
<td>{{item}}</td>
</template>
</tr>
</template>
</table>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'table-custom',
ready: function() {
this.dataContent = {
contents: [{
name: 'Juan',
surname: 'Garrafaeustaquio',
lastname: 'Sin gas'
}, {
name: 'Paco',
surname: 'Lindort',
lastname: 'chocolate'
}, {
name: 'Pepe',
surname: 'Pilot',
lastname: 'no ve'
}],
titles: [{
name: 'Name',
surName: 'Surname',
lastName: 'Lastname',
age: 'Edad'
}]
};
},
computedValuesContent: function(dataContent) {
var values= [];
for (key in dataContent) {
values.push(dataContent[key]);
}
return values;
}
});
});
</script>
</dom-module>
<table-custom></table-custom>
</body>
</html>
相关文章:
- 根据元素和容器大小计算边距
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 用于搜索的聚合物嵌套绑定
- 使用D3.js计算带有字母间距的文本长度
- 使用CSS或JavaScript计算分页符的数量
- 可以't计算自定义谷歌地图的js
- 聚合物0.5.5:核心列表中的条件模板和/或模板动态参考
- 如何计算每个元素's的高度,并将这些值用作函数中的变量
- JavaScript计算帮助(乘以时间)
- 如何计算对象文字中的键
- 为对象计算的聚合物
- 聚合物计算函数刷新值
- 加载脚本后的聚合物计算绑定
- 在请求完成之前无法计算聚合物 iron-ajax 标头属性
- 聚合物-使用计算绑定的多个手风琴
- 通过计算绑定,在聚合物中使用dom-repeat从Firebase动态获取文件url
- 定义基于数组突变事件和其他属性的聚合物计算属性
- 计算绑定不适用于点击(聚合物)
- 永久dom-if的聚合物预计算
- 聚合物0.9在重复模板中计算