我正在为电台构建播放数据报告工具。这个想法是从播放软件的API获取播放历史记录,然后手动添加从播放软件外部播放的曲目。该API每天从00:00到24:00输出一个播放日志。
使用时,用户登录系统,设置其表演的日期和时间,然后将在该时间范围内播放的歌曲提取到报告中。我不知道如何根据“ date” -value中的小时从json过滤结果。下面的HTML,jQuery,JSON示例。
我搜索了类似问题的其他问题,但没有找到答案。我对此很陌生,所以我不知道下一步要去哪里。非常感谢任何帮助或技巧,以改善我的代码!
jquery:
$('.gettracks').click(function() {
const date = document.getElementById('date').value;
const starthh = document.getElementById('starthh').value;
const startmm = document.getElementById('startmm').value;
const endhh = document.getElementById('endhh').value;
const endmm = document.getElementById('endmm').value;
$.getJSON(
'https://www.playlogurl.com/playlog.php&date=' + date,
function(data) {
var items = [];
$.each(data, function(key) {
items.push(
"<tr id='" +
key +
"'><td>" +
data[key].date +
'</td>' +
'<td>' +
data[key].artist +
'</td>' +
'<td>' +
data[key].song +
'</td>' +
'<td>' +
data[key].length +
'</td>' +
'</tr>'
);
});
$('<tbody/>', {
class: 'list',
html: items.join('')
}).appendTo('.my-new-list');
}
);
});
HTML snippets:
<input type="text" id="starthh" /> :
<input type="text" id="startmm" />
<input type="text" id="endhh" /> :
<input type="text" id="endmm" />
<button class="gettracks">Get Tracks</button>
<table class="my-new-list"></table>
JSON:
{
"0": {
"date": "2019-01-28 07:56:34",
"artist": "Michal Jackson",
"song": "Beat It",
"album": "Beat It",
"length": "4:48",
... other values
},
"1": {
"date": "2019-01-28 08:12:39",
"artist": "Iron Maiden",
"song": "Run To The Hills",
"album": "Trooper",
"length": "4:28",
... other values
},
...
"30": {
"date": "2019-01-28 12:02:39",
"artist": "Ministry",
"song": "No Glory",
"album": "Trooper",
"length": "4:28",
... other values
},
"31": {
"date": "2019-01-28 12:15:03",
"artist": "Killing Joke",
"song": "Pylon",
"album": "Euphoria",
"length": "4:48",
... other values
},
....
}
我只想将在开始时间和结束时间之间播放的歌曲从html输入“ starthh”和“ endhh”(例如12:00-16:00)推送到html。
答案 0 :(得分:0)
我知道了。不知道这是否是最好的解决方案,但似乎可行。刚刚将其添加到$ each-loop的开头:
$.each(data, function(key) {
let hoursfirst = data[key].date.charAt(11);
let hourssecond = data[key].date.charAt(12);
let hours = hoursfirst + hourssecond;
hours = parseInt(hours);
if (hours < starthh || hours >= endhh) {
return;
}
...
}