【发布时间】:2016-08-31 22:24:01
【问题描述】:
第一次使用这个 Web 组件...我能够将 var 中的 JSON 数据绑定到 vaadin-grid(带有聚合物 1.0),但是缺少一些关于从 url 获取 JSON 数据到网格。
这是我可以创建的最简单的示例,它使用硬编码的 JSON,现在使用 Vaadin 网站上的一些示例来尝试从 URL 中提取数据。
<head>
// import statements same as in my example that works with hard coded JSON
<script>
function getJSON(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
callback(JSON.parse(xhr.responseText));
}
};
xhr.open('GET', url, true);
xhr.send();
}
</script>
</head>
<body>
<h2>Vaadin Grid - example </h2><br>
<vaadin-grid selection-mode="multi">
<table>
<col name="name">
<col name="city">
</table>
</vaadin-grid>
<script>
var grid = grid || document.querySelector('vaadin-grid');
HTMLImports.whenReady(function() {
grid.items = function(params, callback) {
var url = 'https://.../simple-data.json';
getJSON(url, function(data) {
callback(data[0]);
});
};
});
</script>
simple-data.json URL 会返回这个:
[ { "name": "Jose Romero", "city": "Monteray" }, { "name": "Chuy Diez", "city": "Los Angeles" }, { "name": "Inez Vega", "city": "San Diego" } ]
我哪里错了?提前致谢。
【问题讨论】: