功能要求很简单,要一个日历样式的表格,显示每天的工作信息,用最简单的形容方法就是:仿google日历样式。
在很多网站上已经可以看得到相应的效果,所以,这个东西的实现一定不会有难度,就看你是否想得到。百度惯了,懒得自己想。可是百度往往就是那么强大,逼的人不需要去思考。呵呵,网上有很多现成的代码呀。
先贴个参考网址,然后写出自己的想法、思路,最后贴出自己的例子。
参考网址:
http://blog.csdn.net/shanshan209/article/details/6165091
参考以上网址,思路很简单:
1、用一个table,加点样式看上去像日历一样,其他的填充日期和数据的重点就在于样式中的id的定义了。具体的参考以上网址。
2、填充日期:定义好了周一到周日的位置,得到填充日期的开始日期是星期几,根据table中的标签的id进行填充。结束日期是开始日期加上这个月的天数,依次进行填充。
3、填充数据:因为是根据日期填充数据,所以,数据中需要有日期,而table中每个td的id设为日期,日期格式保证一致,这样就可以将相关数据填充到正确的单元格中。
我的例子:
我的例子不是jsp页面的,而是在java代码中生成的一个日历,然后作为邮件的内容发送邮件的,下面,贴出生成邮件内容的部分代码:
public String getMonthBody() throws ParseException{
Date today = new Date();
// Date today = DateUtils.parseDate("2012-11-17 00:01:01");
Boolean haveWorkUnitFlag = false; //记录是否为空判断,默认为空,当记录为空时,返回“”;
SimpleDateFormat yearMonthFormat = new SimpleDateFormat("yyyy年MM月");
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
StringBuffer bodyStrBuf = new StringBuffer();
bodyStrBuf.append("<table id='calTable' style='border-collapse:collapse; border:1px solid #404040;'>");
bodyStrBuf.append("<tr>");
bodyStrBuf.append("<th colspan='7' style='text-align:center;font-weight:bold; background:#E1E1E1; height:30px;'>");
bodyStrBuf.append("<span id='dateInfo' >"+yearMonthFormat.format(today)+"</span>");
bodyStrBuf.append("</th>");
bodyStrBuf.append("</tr>");
bodyStrBuf.append("<tr>");
bodyStrBuf.append("<td style='text-align:center; background:#E6E6E6; border:1px solid #080808; color:#112ABB;'>星期一</td>");
bodyStrBuf.append("<td style='text-align:center; background:#E6E6E6; border:1px solid #080808; color:#112ABB;'>星期二</td>");
bodyStrBuf.append("<td style='text-align:center; background:#E6E6E6; border:1px solid #080808; color:#112ABB;'>星期三</td>");
bodyStrBuf.append("<td style='text-align:center; background:#E6E6E6; border:1px solid #080808; color:#112ABB;'>星期四</td>");
bodyStrBuf.append("<td style='text-align:center; background:#E6E6E6; border:1px solid #080808; color:#112ABB;'>星期五</td>");
bodyStrBuf.append("<td style='text-align:center; background:#E6E6E6; border:1px solid #080808; color:#112ABB;'>星期六</td>");
bodyStrBuf.append("<td style='text-align:center; background:#E6E6E6; border:1px solid #080808; color:#112ABB;'>星期日</td>");
bodyStrBuf.append("</tr>");
Integer lastDay = Integer.valueOf(dayFormat.format(lastDayOfMonth(today)));
Integer month = Integer.valueOf(monthFormat.format(today));
Integer year = Integer.valueOf(yearFormat.format(today));
Integer startDay = today.getDay();
Integer endDay = startDay+lastDay;
Integer dayCount = Integer.valueOf(dayFormat.format(today));
String dayCountShow = dayCount.toString();
int num = 1;
for (int tr=0;tr<6;tr++) { //循环6次,添加6行
bodyStrBuf.append("<tr>");
for (int tdwork=0;tdwork<7;tdwork++) {
if (num<=startDay ||num>endDay || tdwork==5 || tdwork==6) { //当日期是周末,或者不是该月份内的日期时候的样式。
bodyStrBuf.append("<td style='border:1px solid #656565; width:150px; height:120px; vertical-align:top; background:#F2F2F2;'>");
} else {
bodyStrBuf.append("<td style='border:1px solid #656565; width:150px; height:120px; vertical-align:top;'>");
}
if (num >=startDay && num <=endDay) {
if (dayCount>lastDay) {
dayCount=1;
month+=1;
if (month>12) {
year+=1;
dayCountShow=String.valueOf(year)+"年 " + (month) + "月 " + dayCount.toString();
} else {
dayCountShow=(month) + "月 " + dayCount.toString();
}
} else {
dayCountShow = dayCount.toString();
}
bodyStrBuf.append("<div style='background:#E9E9E9;font-size:18px; padding:2px;' >" + dayCountShow);
bodyStrBuf.append("</div>");
Date countDate = DateUtils.parseDate(year+"-"+month+"-"+dayCount);
List<WorkUnitSegmentCountVO> workUnitCountList = this.workUnitService.getNextDayDeployWorkUnit(countDate, countDate);//获取当天日期数据值。
if (workUnitCountList != null && workUnitCountList.size()>0) {
haveWorkUnitFlag = true;
String style = "";
if (num == startDay+1) {
style="style='color: red;'";
} else {
style="style='color: blue;'";
}
for (WorkUnitSegmentCountVO obj : workUnitCountList) {
bodyStrBuf.append("<div style='border:1px solid #E4E4E4;'>");
String workUnitTitle = obj.getWorkUnitTitile();
if (StringUtils.isNotBlank(obj.getSuffix())) {
workUnitTitle += obj.getSuffix();
}
bodyStrBuf.append("<a "+ style +" href='"+obj.getWorkUnitUrl()+"' title='"+obj.getWorkUnitTitile()+"'>" + workUnitTitle + "</a>");
bodyStrBuf.append("</div>");
}
}
dayCount++;
}
bodyStrBuf.append("</td>");
num ++;
}
bodyStrBuf.append("</tr>");
}
bodyStrBuf.append("</table> ");
if (haveWorkUnitFlag == false) {
return "";
}
return bodyStrBuf.toString();
}