Acoustic77,
下面是修改queryCourseData方法。
我使用async:false將您的$ .getJSON轉換爲$ .ajax(這可能是也可能不是我的問題,所以我鼓勵您嘗試將其設置爲true並測試它是否適用於您結束或不)。
然後我注意到你的startTime和endTime格式是{小時:#,分鐘:#},而item.start_time和item.end_time是24小時格式的時間字符串。我寫了一些代碼將前一種格式轉換爲24小時格式(我確信有比這更好的做法)。
我也是後來,在我最初的回答後,注意到你正在將myArray設置爲[]你的內循環的每一步,你正在構建ReqcourseArray和OptcourseArrays。將var var myArray = []移到內部是我的最終修復。
我離開了我的console.log,以便您可以看到結果。是
function queryCourseData(startTime, endTime, optCourses, reqCourses, numOptCourses, numReqCourses)
{
var numClasses = optCourses.length;
var OptclassList = [];
var i = 0;
for(var m = 0; m < numClasses; m++)
{
//[email protected], 2014-06-05
//Your ajax calls were taking too long to call so the code that needed them was
//getting called before the data was ready.
//This could be a problem on my end so you can always set async to true and test from your end.
$.ajax({url:"http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject="+optCourses[m].subject,
async: false,
dataType: 'json',
success:function(result)
{
//[email protected], 2014-06-05
//Your start and end times are objects of the format
//{hour:x, minute:x}
//While your item.start_time and item.end_time are 24 hour time strings.
//I am sure there is a more elgant way to do this but here is a dirty conversion
//from one to the other.
var sTime = (startTime.hour<10?"0"+startTime.hour:startTime.hour) + ":" + startTime.minute+"00";
var eTime = (endTime.hour<10?"0"+endTime.hour:endTime.hour) + ":" + endTime.minute+"00";
$(result).each(function (index, item)
{
if (item.start_time > sTime)
{
if (item.end_time < eTime)
{
if (item.catalog_num == optCourses[m].courseNumber)
{
var coursject = {
title: item.title,
professor: item.instructor.name,
catalog_num: item.catalog_num,
section: item.section,
subject: item.subject,
meeting_days: item.meeting_days,
start_time: item.start_time,
end_time: item.start_time
};
//[email protected]
//Now Pushing Entries Into Array
OptclassList.push(coursject);
i++;
}
}
}
});
}
});
}
var OptcourseArray = [];
for(var j = 0; j < numOptCourses; j++)
{
var catNum = optCourses[j].courseNumber;
//[email protected]
//You were resetting your myArray every time you in the loop below.
//Subsequently, only the last entry would every get added and you were
//getting empty arrays.
var myArray = [];
for(var h = 0; h<OptclassList.length; h++)
{
if (OptclassList[h].catalog_num == catNum)
{
myArray.push(OptclassList[h]);
}
}
OptcourseArray.push(myArray);
}
console.log("--OPT--");
console.log(JSON.stringify(OptcourseArray));
console.log("--OPT--");
var ReqclassList = [];
var g = 0;
for(var n = 0; n < reqCourses.length; n++)
{
//[email protected], 2014-06-05
//Your ajax calls were taking too long to call so the code that needed them was
//getting called before the data was ready.
//This could be a problem on my end so you can always set async to true and test from your end.
$.ajax({url:"http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject="+reqCourses[n].subject,
async: false,
dataType: 'json',
success: function(result)
{
//[email protected], 2014-06-05
//Your start and end times are objects of the format
//{hour:x, minute:x}
//While your item.start_time and item.end_time are 24 hour time strings.
//I am sure there is a more elgant way to do this but here is a dirty conversion
//from one to the other.
var sTime = (startTime.hour<10?"0"+startTime.hour:startTime.hour) + ":" + startTime.minute+"00";
var eTime = (endTime.hour<10?"0"+endTime.hour:endTime.hour) + ":" + endTime.minute+"00";
$(result).each(function (index, item)
{
if (item.start_time > sTime)
{
if (item.end_time < eTime)
{
if ($.trim(item.catalog_num) == $.trim(reqCourses[n].courseNumber))
{
var coursject = {
title: item.title,
professor: item.instructor.name,
catalog_num: item.catalog_num,
section: item.section,
subject: item.subject,
meeting_days: item.meeting_days,
start_time: item.start_time,
end_time: item.start_time
};
//[email protected]
//Now Pushing Entries Into Array
ReqclassList.push(coursject);
g++;
}
}
}
});
}
});
}
var ReqcourseArray = [];
for(var j = 0; j < numReqCourses; j++)
{
var catNum = reqCourses[j].courseNumber;
//[email protected]
//You were resetting your myArray every time you in the loop below.
//Subsequently, only the last entry would every get added and you were
//getting empty arrays.
var myArray = [];
for(var h = 0; h < ReqclassList.length; h++)
{
if ($.trim(ReqclassList[h].catalog_num) == $.trim(catNum))
{
myArray.push(ReqclassList[h]);
}
}
ReqcourseArray.push(myArray);
}
console.log("--REQ--");
console.log(JSON.stringify(ReqcourseArray));
console.log("--REQ--");
return [OptcourseArray, ReqcourseArray];
}
我的測試結果如下:
測試用例:
2 courses
EECS 214-0 (optional)
EECS 223-0 (required)
結果:
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS".
--OPT--
[[{"title":"Data Structures & Data Management","professor":"Morteza Amir Rahimi","catalog_num":"214-0","section":"21","subject":"EECS","meeting_days":"MoWeFr","start_time":"11:00:00","end_time":"11:00:00"}]]
--OPT--
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS". jquery.min.js:4
--REQ--
[[{"title":"Fundamentals of Solid State Engineering","professor":"Koray Aydin","catalog_num":"223-0","section":"01","subject":"EECS","meeting_days":"MoTuWeFr","start_time":"09:00:00","end_time":"09:00:00"}]]
--REQ--
測試用例:
5 courses:
EECS 214-0 (optional)
EECS 223-0 (required)
EECS 110-0 (required)
EECS 213-0 (required)
EECS 203-0 (optional)
結果:
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS". jquery.min.js:4
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS". jquery.min.js:4
--OPT--
[[{"title":"Data Structures & Data Management","professor":"Amartya Banerjee","catalog_num":"214-0","section":"20","subject":"EECS","meeting_days":"MoWeFr","start_time":"09:00:00","end_time":"09:00:00"},{"title":"Data Structures & Data Management","professor":"Morteza Amir Rahimi","catalog_num":"214-0","section":"21","subject":"EECS","meeting_days":"MoWeFr","start_time":"11:00:00","end_time":"11:00:00"}],[{"title":"Introduction to Computer Engineering","professor":"Hai Zhou","catalog_num":"203-0","section":"01","subject":"EECS","meeting_days":"MoWeFr","start_time":"11:00:00","end_time":"11:00:00"}]]
--OPT--
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS". jquery.min.js:4
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS". jquery.min.js:4
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS". jquery.min.js:4
--REQ--
[[{"title":"Fundamentals of Solid State Engineering","professor":"Koray Aydin","catalog_num":"223-0","section":"01","subject":"EECS","meeting_days":"MoTuWeFr","start_time":"09:00:00","end_time":"09:00:00"}],[{"title":"Introduction to Computer Programming","professor":"Aleksandar Kuzmanovic","catalog_num":"110-0","section":"20","subject":"EECS","meeting_days":"MoTuWeFr","start_time":"10:00:00","end_time":"10:00:00"}],[{"title":"Introduction to Computer Systems","professor":"Peter A Dinda","catalog_num":"213-0","section":"20","subject":"EECS","meeting_days":"TuTh","start_time":"14:00:00","end_time":"14:00:00"}]]
--REQ--
請讓我知道如果這有助於:)
Acoustic77,你能possbily提供您所看到的輸出的例子嗎?我嘲笑了一些數據,你的代碼當然似乎正在生成一個數組Array Array:[[{「name」:「class1」,「catalog_num」:「course1」},{「name」:「class2」,「catalog_num」 : 「course1」}],[{ 「名稱」: 「等級2」, 「catalog_num」: 「course2」}],[{ 「名稱」: 「CLASS3」, 「catalog_num」: 「course3」}],[{」名稱「:」class4「,」catalog_num「:」course4「}]] – IrishGeek82
代碼看起來不錯。沒有更多信息,我們可以做的不多。我建議你創建一個http://jsfiddle.net/來重現問題。 –
這裏是一些UI的JSfiddle和上面的完整代碼http://jsfiddle.net/9KDVX/1/(代碼來創建可能的時間表併發送到完整的日曆不包括在內)。我不知道如何格式化我的輸出的評論,但我添加了一個「console.log」行的JavaScript。我一直在測試一個名爲EECS 214-0的可選類。如果你把這些文件放在一個文件夾中,然後用EECS 214-0進行測試,你會看到數組中第一個元素對象的「未定義」 – Acoustic77