javascript - 使用来自 url 的 JSON 填充 Meteor 中的选择下拉菜单?

标签 javascript json meteor

如何使用 Meteor 和位于以下位置的以下 JSON 创建近期彩票结果页面: LOTTERY DATA

以下代码会成功调用数据,如控制台所示,但我不够聪明,无法弄清楚如何将此数据显示到 Meteor 的页面上。

Meteor.http.call("GET", "http://data.ny.gov/resource/d6yy-54nr.json", function (err, result){
my_json = result.content;
 console.log(my_json);
});

控制台正在记录以下内容:

[ {
  "draw_date" : "2015-01-17T00:00:00",
  "winning_numbers" : "15 16 23 27 36 09",
  "multiplier" : "2"
}
, {
  "draw_date" : "2015-01-14T00:00:00",
  "winning_numbers" : "02 04 10 41 53 22",
  "multiplier" : "5"
}
, {
  "draw_date" : "2015-01-10T00:00:00",
  "winning_numbers" : "02 09 19 28 29 19",
  "multiplier" : "5"
}

最初只要在页面上显示任何结果就很棒了。但理想情况下,我希望能够有一个包含所有可用 draw_date(s) 的选择下拉菜单,并且当用户做出选择时,将显示中奖号码。

你能帮我解决这个问题吗?

最佳答案

这段代码将做到这一点: 我已将 JSON 的结果用作硬编码对象,因为 Meteor 无法作为外部库加载。

var showData = [ {
  "draw_date" : "2015-01-17T00:00:00",
  "winning_numbers" : "15 16 23 27 36 09",
  "multiplier" : "2"
}
, {
  "draw_date" : "2015-01-14T00:00:00",
  "winning_numbers" : "02 04 10 41 53 22",
  "multiplier" : "5"
}
, {
  "draw_date" : "2015-01-10T00:00:00",
  "winning_numbers" : "02 09 19 28 29 19",
  "multiplier" : "5"
}];

//set a default option to the select.
var html = "<option value='' disabled default>Select a date</option>";

//iterate over each lottery drawing and add it to the select.
//The date will be displayed, the index of the array element will be the value.
showData.forEach(function(element, index){
   var date = new Date(element.draw_date);
   html += "<option value='"+index+"'>"+ date.getDate() + "/" + (parseInt(date.getMonth())+1) + "/" + date.getFullYear()+ "</option>";
   
});

//insert the option into the select.
document.getElementById("selectDate").insertAdjacentHTML("beforeend", html);
//add an onchange event handler to the select.
document.getElementById("selectDate").addEventListener("change", displayWinningNumbers, false);

function displayWinningNumbers(e)
{
  //when a option is selected, test the value. If 0 or higher return the array entry with the winning numbers.
  if(e.target.value >= 0)
  {
     alert(showData[e.target.value].winning_numbers); 
  }
}
<select  id="selectDate">
  
</select>

关于javascript - 使用来自 url 的 JSON 填充 Meteor 中的选择下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28035709/

相关文章:

javascript - 图像加载事件未在 iPhone (IOS) 上触发

javascript - Angular 超时不起作用

javascript - 在灯箱的 JQuery 选择器中使用 JS 变量时出现语法错误

json - node.js 和 json 运行时错误

javascript - 在 JavaScript 中使用 if 语句构建选择器

javascript - 添加更多链接到meteor-accounts-ui-bootstrap-3的下拉菜单

javascript - jQuery 使用页面滚动移动一个 div

python - 如何从Python中的以下内容获取表名

arrays - 从 Mongoose 数组中删除元素

meteor - 通过 meteor 存在获得大量在线用户