我正在尝试创建预约时段选择网格。我已将所有约会按日期分组。我想将下面的对象数组显示为可点击的网格。我拥有我需要的一切,但我很难在列中显示它。我目前的输出:
如您所见,它在正确的日期标题旁边显示了正确的约会,但是它在同一行中添加了下一个约会,依此类推。我希望每个日期及其相应的约会显示在单独的列中。顶部的日期标题,下面列出了约会。我只是使用一个 main.js 文件和一个 index.html 文件。
下面的分组预约空档数组看起来像这样:
2020-09-25: [{0:{AppointmentDateTime: "2020-09-25T13:00:00Z"}}]
2020-09-28: [{0:{AppointmentDateTime: "2020-09-28T08:00:00Z"}},{1:{AppointmentDateTime: "2020-09-28T10:30:00Z"}}, {2:{AppointmentDateTime: "2020-09-28T11:00:00Z"}}]
2020-09-29: [{0:{AppointmentDateTime: "2020-09-29T08:00:00Z"}},{1:{AppointmentDateTime: "2020-09-29T09:00:00Z"}}, {2:{AppointmentDateTime: "2020-09-29T11:00:00Z"}}]
这是我到目前为止所拥有的:
let result = await response.json();
let groupedAppointments = {}
result.forEach((item) => {
const date = item.AppointmentDateTime.split('T')[0]
if (groupedAppointments[date]) {
groupedAppointments[date].push(item);
} else {
groupedAppointments[date] = [item];
}
})
var grid = clickableGrid(groupedAppointments, groupedAppointments, function(el, item) {
console.log("You clicked on element:", el);
selectedAppointment = item.AppointmentDateTime;
console.log(selectedAppointment);
el.className = 'clicked';
if (lastClicked) lastClicked.className = '';
lastClicked = el;
});
document.getElementById("clickable-grid").appendChild(grid);
function clickableGrid(groupedAppointments, index, callback) {
var i = 0;
var grid = document.createElement('table');
grid.className = 'grid';
Object.keys(groupedAppointments).forEach((item) => {
var days = groupedAppointments[item];
var tableRowHeader = grid.appendChild(document.createElement('tr'));
var th = tableRowHeader.appendChild(document.createElement('th'));
th.innerHTML = item;
我希望这是足够的细节。有任何问题就问。非常感谢所有帮助。
撒科打诨
白板的微信
相关分类