2 回答

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊
您不應(yīng)該tr為每個(gè)約會(huì)創(chuàng)建一個(gè)。
你需要兩個(gè)循環(huán)。一個(gè)循環(huán)創(chuàng)建包含所有日期的標(biāo)題行。
然后循環(huán)遍歷數(shù)組索引以創(chuàng)建數(shù)據(jù)行。在其中,每個(gè)日期都有一個(gè)嵌套循環(huán),填充行中的該列。由于日期會(huì)有不同數(shù)量的約會(huì),因此您需要檢查當(dāng)前日期是否有那么多約會(huì)。如果是,則填寫單元格,否則將其留空。
function clickableGrid(groupedAppointments, callback) {
var i = 0;
var grid = document.createElement('table');
grid.className = 'grid';
var longest = 0;
var headerRow = grid.appendChild(document.createElement('tr'));
Object.entries(groupedAppointments).forEach(([item, day]) => {
if (day.length > longest) {
longest = day.length;
}
var th = headerRow.appendChild(document.createElement('th'));
th.innerHTML = item;
});
for (let i = 0; i < longest; i++) {
var tr = grid.appendChild(document.createElement('tr'));
Object.values(groupedAppointments).forEach(item => {
var cell = tr.appendChild(document.createElement('td'));
if (i < item.length) {
let time = item[i].AppointmentDateTime.split('T')[1].split('Z')[0];
cell.innerHTML = time;
cell.addEventListener('click', (function(el, item) {
return function() {
callback(el, item);
}
})(cell, item[i]), false);
}
});
}
return grid;
}
var data = {
"2020-09-25": [{
AppointmentDateTime: "2020-09-25T13:00:00Z"
}],
"2020-09-28": [{
AppointmentDateTime: "2020-09-28T08:00:00Z"
}, {
AppointmentDateTime: "2020-09-28T10:30:00Z"
}, {
AppointmentDateTime: "2020-09-28T11:00:00Z"
}],
"2020-09-29": [{
AppointmentDateTime: "2020-09-29T08:00:00Z"
}, {
AppointmentDateTime: "2020-09-29T09:00:00Z"
}, {
AppointmentDateTime: "2020-09-29T11:00:00Z"
}]
};
document.body.appendChild(clickableGrid(data, function(cell, date) {
console.log("You clicked on " + date.AppointmentDateTime);
}));

TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊
看起來你的表到處都是,你創(chuàng)建了 2 個(gè)表行,而實(shí)際上你只需要為每個(gè)日期使用一個(gè)行(包括第 th 次和約會(huì))并且你只需要在新日期時(shí)轉(zhuǎn)到新行被呈現(xiàn)。我更改了一些命名以使其更精確(項(xiàng)目并沒有說明它是什么,而且當(dāng)您對(duì)不同的參數(shù)使用相同的名稱時(shí)它會(huì)變得混亂)。我注釋掉的行標(biāo)有*.
function clickableGrid(groupedAppointments, index, callback) {
var i = 0;
var grid = document.createElement('table');
grid.className = 'grid';
Object.keys(groupedAppointments).forEach((date) => {
//*var days = groupedAppointments[key]; --> since you don't copy and just refferencing a memory slot
var tr = grid.appendChild(document.createElement('tr')); // renamed to tr because you need to have only one row, both for the header and the data
var th = tr.appendChild(document.createElement('th'));
th.innerHTML = date;
groupedAppointments[date].forEach((appointment) => {
//*var tr = grid.appendChild(document.createElement('tr')); --> cell and table hadder are in the same row
//var rowHeader = tr.appendChild(document.createElement('th'))
var cell = tr.appendChild(document.createElement('td'));
//rowHeader.innerHTML = appointment.SlotName;
cell.innerHTML = appointment.AppointmentDateTime;
cell.addEventListener('click', (function(el, appointment) {
return function() {
callback(el, appointment);
}
})(cell, appointment), false);
})
})
return grid;
}
希望它有所幫助:)
添加回答
舉報(bào)