项目实战——日历翻页获取数据render

var reg = /(([\,,,]*\d{4}-\d{2}-(\/*\d{2})+)+)\s*([\(|(]\S+[\)|)])*\s*((\/*\d{2}[:,:]\d{2})+)\s*([\(|(]\S+[\)|)])*/;
 function getData(date,channelId){
	 renderCalendar();
 	 $.post(
 		'/info/calendar/list/json', 
 		{
 			'date':date,
 			'channelId':channelId
 		},
 		function(data){
 			if(data.code === 0 && data.hasOwnProperty('infos')){
 				orgData(data.infos,date);
 			}
 		},'json'
 	);
  };
function orgData(dataList,date){
	var list = [];
	for(var i=0;i<dataList.length;i++){
		var time = dataList[i].infoParamMap.ycsj.value;
		var regList = reg.exec(time);
		var regDate = regList[1].split(",");
		var regTime = regList[5].split("/");
		for(var j=0;j<regDate.length;j++){
			if(regDate[j].indexOf(date)>=0){
				var dateList =regDate[j].substring(regDate[j].lastIndexOf('-')+1,regDate[j].length).split("/");
				var foreDate = regDate[j].substring(0,regDate[j].lastIndexOf('-')+1);
			    for(var k=0;k<dateList.length;k++){
		    		var lastDate =  foreDate + dateList[k];
		    		for(var s =0;s<regTime.length;s++){
				    	list.push({
					    	'title':dataList[i].title,
					    	'date':lastDate,
					    	'time':regTime[s],
					    	'position':dataList[i].infoParamMap.dz.value,
					    	'id':dataList[i].id
				    	});
		    	 	}
			     }				
			 }
		 }
	 }
	 list.sort(_upSort);
	 renderCalendarData(list);
};
 _upSort = function(a, b){//升序排序
 	if (a["date"] < b["date"]){
 		return -1;
 	}else if (a["date"] == b["date"]){
 		if (a["time"]<b["time"]){
 			return -1;
 		}else if (a["time"] == b["time"]){
 			return 0;
 		}else{
 			return 1;
 		}
 	}else{
 		return 1;
 	}
 };
function renderCalendar(){
	var dayObj = getDaysInMonth(parseInt($("#year").text()),parseInt($("#day").text()));
	var dateStr = '';
	var count = 1;
	$("#calHead").nextAll().remove();
	for(var i=0;i<dayObj.line;i++){
		dateStr +="<tr>";
		for(var j=0;j<7;j++){
			count++;
			if(count-dayObj.firstDay <= 0||count-dayObj.firstDay > dayObj.days){
				dateStr +="<td valign='top'><h3></h3></td>";
			}else{
				dateStr +="<td valign='top'><h3>"+(count-dayObj.firstDay)+"</h3><div class='"+$("#year").text()+'-'+$("#day").text()+'-'+Int2Str(count-dayObj.firstDay)+"'></div></td>";
			}
		}
		dateStr +="</tr>";
	}
	$(dateStr).insertAfter("#calHead"); 	
	$("#calendarTable").find("td:nth-child(6),td:nth-child(7)").addClass("bgcolor");
}
function renderCalendarData(list){
	for(var k=0;k<list.length;k++){
		var dzClass = list[k].position=="新清华学堂"?"qingHuaXueTang":list[k].position=="蒙民伟音乐厅"?"yinYueTing":list[k].position=="大礼堂"?"daLiTang":list[k].position=="蒙民伟楼学生活动中心"?"huoDongZHongXin":list[k].position=="新学堂实验剧场"?"shiYanJuCHang":"";
		$("."+list[k].date).append("<a class='"+dzClass+"' href='$base/info/pwzx_hdap/"+list[k].id+"'>"+list[k].title+"</a><span>"+list[k].time+"</span>");
	}
}
 function getDaysInMonth(year,month){
 	month = parseInt(month,10);  //parseInt(number,type)这个函数后面如果不跟第2个参数来表示进制的话,默认是10进制。
 	var temp = new Date(year,month,0);
 	var firstDay = (temp.getDay()-temp.getDate()+1)%7+7;
 	var line = Math.ceil((firstDay+temp.getDate()-1)/7);
	return {'days':temp.getDate(),'firstDay':firstDay,'line':line}; 
 }
function Int2Str(number){
	return (parseInt(number).toString().length==1?'0':'')+number;
};

function preMonth(){//上个月
	var curMonth = parseInt($("#day").text());
	var curYear = parseInt($("#year").text());
	if(curMonth==1){
		$("#day").text(12);
		$("#year").text(curYear-1);
	}else{
		$("#day").text(Int2Str(curMonth-1));
	}
	getData($("#year").text()+'-'+$("#day").text(),'153,154,155,156,159');
};
function nextMonth(){//下个月
	var curMonth = parseInt($("#day").text());
	var curYear = parseInt($("#year").text());
	if(curMonth==12){
		$("#day").text('01');
		$("#year").text(curYear+1);
	}else{
		$("#day").text(Int2Str(curMonth+1));
	}
	getData($("#year").text()+'-'+$("#day").text(),'153,154,155,156,159');
};
$(function(){
	var date = new Date();
 	$("#year").text(date.getFullYear());
 	$("#day").text(Int2Str(parseInt(date.getMonth())+1));
 	getData($("#year").text()+'-'+$("#day").text(),'153,154,155,156,159');
});

相关推荐