我認爲最好的辦法是有一個時間表類是這樣的:
var TimeTable = function(currentCell) {
var days = ["mon", "tue", "wed", "thu", "fri"];
var hours = ["09", "10", "11", "12", "13", "14", "15", "16"];
var currentDay = days.indexOf(currentCell.substring(0,3));
var currentHour = hours.indexOf(currentCell.substring(3));
// nextCell: updates the values on the TimeTable object
var nextCell = function() {
var lastDay = days.length - 1;
if (currentHour === hours.length - 1) {
currentHour = 0;
currentDay++;
} else {
currentHour++;
}
}
// getNextCell: gets the current Cell and increments the next cell
getNextCell = function() {
var currentCell = days[currentDay] + hours[currentHour];
nextCell();
return currentCell;
}
// fill: fills (duration) times the adjacent cells
// input duration: the duration of the event (> 0)
// input text: the text that needs to be filled into each cell
this.fill = function (duration, text) {
for (var i = 0; i < duration; i++) {
$("#" + getNextCell()).text(text);
}
}
}
既然你談到這可能是2或更高的持續時間,我用一個for循環,而不是的,如果在fill
方法,所以它會填滿3個單元,如果持續時間= 3等
最後,你需要更換for
循環是這樣的:
for (var i = 0; i < r.events.length; i++) {
var timeTable = new TimeTable(r.events[i].slot);
text = r.desc + '\n' + r.events[i].type + '\n' + r.events[i].rooms + '\n' + r.events[i].id + r.events[i].duration;
timeTable.fill(r.events[i].duration, text);
}
的代碼是this fiddle
你可以給我們一個快速jsfiddle.net起點。存根r的數據。 –
jsfiddle.net: http://jsfiddle.net/V4sn8/ – Barboro37
快速提問。所以,如果讓我們說wed16,我們得到一個2的事件持續時間。會發生什麼?是創建一個新的TD還是因爲它不超過'16',它沒有獲得+1治療? –