java - 如何使用 html 从 JSON 文件中选择数据
问题描述
我有一个问题,我不知道如何从 JSON 文件中选择 html 中的特定数据。html 文件对 json 使用 xhttp,然后解析数据。目前我可以在表格中显示页面上 json 中的所有信息,如下所示。有没有一种方法可以制作一个 html 菜单或下拉框,我可以从所有数据中选择某些日期,或者制作几个不同的过滤器并以某种方式过滤数据?
我试图只能选择某些比赛日,如图像 2,但我希望能够添加我想看到的选项,如按团队或日期过滤这可能吗?任何帮助表示感谢
解决方案
您可以使用 jquery 函数过滤器 ( http://api.jquery.com/filter/ ) 或 grep ( http://api.jquery.com/jquery.grep/ )。明天我将提供一个工作示例,因为我现在正在使用我的智能手机。用 javascript 看看这个例子:https ://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_filter 。
这是你可以做到的:只有“Matchday 1”的比赛只是像这样过滤结果的轮数数组
xhttp.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
var peopleTxt = this.responseText;
peopleObj = JSON.parse(peopleTxt); //.filter();
peopleObj.rounds = peopleObj.rounds.filter(a=>a.name=="Matchday 1");
html = Mustache.to_html(tp, peopleObj);
document.getElementById("demo").innerHTML = html;
}
};
推荐阅读
- excel - Excel Countif 函数选择离散的单元格范围
- ruby-on-rails - 使用carrierwave-video 上传.MOV 文件?
- javascript - 做前端 2 年了,仍然不确定何时/如何使用类
- activemq-artemis - JMS 主题订阅者在未连接时不会收到消息
- android - 如何在不使用任何构建工具的情况下使用 aapt2 编译 Android 应用程序?
- php - 是否可以在 PHP MYSQL 中存储可点击的图像?
- svg - svg 在浏览器和 imagemagick 中呈现不同(使用 rsvg 转换)
- css - Angular-ui-grid - 文本装饰不适用于行模板子项
- ios - UIImageWriteToSavedPhotosAlbum 错误:使用未解析的标识符
- python-2.7 - ubuntu playbook 中的 Python 非 ASCII 字符 '\xe2'