2011-09-08 2 views
0

javascript/asp.net에서 달력을 작성합니다.자바 스크립트 루프에서 ASP.NET Webservice를 호출하면 문제가 발생합니다.

캘린더를 인쇄 할 때 표시 할 항목 (약속)이 있는지 매일 확인하고 싶습니다. 다음은 함수의 일부입니다.

for (var i = 1; i <= monthdays[month - 1]; i++) { 
     curnum++; 
     td = tr.insertCell(-1); 
     td.style.height = "95px"; 
     td.style.width = "131px"; 
     html = "<div id=\"divday" + year + "" + month + "" + i + "\" style=\"position:relative; left:0px; top:0px; width130px; height:95px; background-color:" + colors[i % 2] + "; cursor:pointer; \" onclick=\"openDay(" + year + "," + (month < 10 ? "0" + month : month) + "," + (i < 10 ? "0" + i : i) + ")\">"; 
     html += "<div class=\"blackl\" style=\"position:absolute; left:100px; top:0px; width:30px; height:20px; text-align:right; \">" + i + "</div>"; 
     html += "</div>"; 
     td.innerHTML = html; 
     if (curnum == 7) { 
      tr = tbl.insertRow(-1); 
      curnum = 0; 
     } 
     if(i == 1) RCalendar.GetCalendarEvents(agentid, year, month, i, function (result) { setCalendarEvents(year, month, i, result) }); 

마지막 줄에는 if (i == 1) 디버깅 전용이 없어야합니다. 나는 webservice를 호출하고 evert 하루 (i)에 대한 항목을 작성하고 싶습니다. 여기

콜백 함수이다

function setCalendarEvents(y, m, d, result) { 
    var daydiv = document.getElementById("divday" + y + m + d); 
    for(var i = 0; i < result.length; i++) { 
     var thiscalendaritem = result[i]; 
     daydiv.innerHTML += makeCalendarItem(thiscalendaritem); 
    } 
} 

I는 웹 서비스 호출과 callbackfunction에서, I 및 D는 동일하지 않은 중단 넣으면. 호출 될 때 나는 분명히 1이지만 enCoked 될 때 setCalendarEvents에서 d는 31입니다.

답변

0

1) 다음 줄에 사용되는 비동기 호출이 경우 변경

if(i == 1) RCalendar.GetCalendarEvents(agentid, year, month, i, 
             function (result) { 
              setCalendarEvents(year, month, i, result) 
             }); 

봅니다 예를 들어,이를 좋아합니다 :

if(i == 1) RCalendar.GetCalendarEvents(agentid, year, month, i, 
             setCalendarEvents.bind(null, year, month, i)); 

비동기 요청 후 setCalendarEvents이 호출되면 완성 된 함수 setCalendarEvents는 4 개의 매개 변수 (연도, 월, i - 변경하지 않음)와 result (마지막 매개 변수)을받습니다.

2) 확인이 행해져 Yout 다음 라인 : 연도

"<div id=\"divday" + year + "" + month + "" + i + ... 

var daydiv = document.getElementById("divday" + y + m + d); 

: 2012, 월 : 1 일 : 12-ID이는 올해 들어 divday2012112

같음 : 2012 , 월 : 11 일 : 2 - id과 같습니다. divday2012112 (이전 div와 동일)

+0

매일 매일 대신 서버에서 모든 이벤트를 가져 오는 것에 대한 첫 번째 설명은 의미가 있었고 다음과 같이 수정했습니다. (day-divs를 그리는 루프 이후 ...) 'getAllCalendarItemsInMonth (agentid, year, month); } 함수 getAllCalendarItemsInMonth (에이전트 ID, 연도, 월) { RCalendar.GetAllCalendarItemsInMonth (agentid, year, month, setAllCalendarItemsInMonth); } 'divday + datestring도 감사하지만이 캘린더는 한 번에 한 달씩 만 표시하므로 중요한 문제는 아닙니다. – Jesper

관련 문제