从Java创建HTML表,检查TD是否已经在它的右边有一个TD
我正在使用Gagawa(https://code.google.com/archive/p/gagawa/)库动态创建一个HTML表,以显示学校每周的课程安排。问题是,因为我使用rowspan根据课程的持续时间来增加单元格的大小,所以当我尝试(例如)添加在MWF上相遇的课程时,该行的典型布局是
<td>...content...</td> <td></td> <td>...content...</td> <td></td> <td>...content...</td>
<td>
,因此它被放在它的右侧)
我需要一种方法来动态地检测是否放入空白^ {< CD2>},或者使之成为正确的方法,而不是向右移动,它会向下移动(也许在CSS中有这样的方法?)p>
下面是我动态生成HTML表的代码:
public String generateHTMLScheduleTable(Schedule s){
Table scheduleTable=new Table();
scheduleTable.setCSSClass("scheduleTable");
Tr dayRow=new Tr();
Th time=new Th(); time.appendText("Time");
Th mon=new Th(); mon.appendText("Monday");
Th tue=new Th(); tue.appendText("Tuesday");
Th wed=new Th(); wed.appendText("Wednesday");
Th th=new Th(); th.appendText("Thursday");
Th fri=new Th(); th.appendText("Friday");
dayRow.appendChild(time, mon, tue, wed, th, fri);
TreeMap<Integer, String> colors=mapCoursesToColors(s);
String[] days={"m", "t", "w", "r", "f"};
for(int j=8; j<=22; j++){
int timeInt=j%12;
if(timeInt==0){
timeInt=12;
}
String timeHr="" + timeInt;
//System.out.println(timeHr);
String amPm;
if(j>11){
amPm="PM";
}
else{
amPm="AM";
}
for(int k=0; k<2; k++){
String timeMin="";
if(k==0){
timeMin="00";
}
else{
timeMin="30";
}
Tr currRow=new Tr();
Td currCell=new Td();
currCell.appendText(timeHr + ":" + timeMin + amPm);
currRow.appendChild(currCell);
for(int i=0; i<days.length; i++){
Td newCell=new Td();
for(Course c : s.getCourses()){
if((c.getTime().substring(0, c.getTime().indexOf(':')).equals(timeHr) || c.getTime().substring(0, c.getTime().indexOf(':')).equals("0" + timeHr)) && c.getTime().substring(0, c.getTime().indexOf('-')).contains(timeMin) && c.getTime().substring(0, c.getTime().indexOf('-')).contains(amPm)){
if(c.getDays().toLowerCase().contains(days[i])){
String currentColor=colors.get(c.getCRN());
String timeLastHalf=c.getTime().substring(c.getTime().indexOf('-')+1);
int startHr=Integer.parseInt(timeHr);
int endHr=Integer.parseInt(timeLastHalf.substring(0, timeLastHalf.indexOf(':')));
int numCells=endHr-startHr;
numCells=numCells*2;
if(!c.getTime().substring(0, c.getTime().indexOf('-')).contains("00")){
if(timeLastHalf.contains("00")){
numCells=numCells-1;
}
}
else{
if(!timeLastHalf.contains("00")){
numCells=numCells+1;
}
}
if(numCells<2){
numCells=2;
}
newCell.setBgcolor(currentColor);
newCell.setRowspan("" + numCells);
newCell.appendText(c.getTitle());
newCell.appendChild(new Br());
newCell.appendText(c.getCourseAndSection());
newCell.appendChild(new Br());
newCell.appendText(c.getTime());
Input submit=new Input();
submit.setType("submit");
submit.setCSSClass("btn");
submit.setName("" + c.getCRN());
submit.setValue("Remove");
Input moreInfo=new Input();
moreInfo.setType("submit");
moreInfo.setCSSClass("btn");
moreInfo.setName(c.getCRN() + "View");
moreInfo.setValue("More Info");
newCell.appendChild(new Br());
newCell.appendChild(submit);
newCell.appendChild(moreInfo);
}
}
}
currRow.appendChild(newCell);
}
scheduleTable.appendChild(currRow);
}
}
String html=scheduleTable.write();
System.out.println(html);
return html;
}
# 1 楼答案
我不认为我真的理解你的问题,但我的建议是:
您可以从这些建议的代码中选择要使用的代码。将它/它们插入代码中您认为应该放置的位置,可以是在表格显示之后或期间
搜索单个/集合或集合元素,将返回一个对象/数组
同上,但方式不同
或者你也可以看看这个foreach方法给定的here
# 2 楼答案
逻辑(询问OP是否可以,他只需要伪代码)
你的编码很好,你需要做的就是每天添加一个计数器,如果有五个计数器(因为你在Q的例子中说过),然后使用这个计数器遍历由于上面的行间距分配而占用的空间,并在结束时跟踪它,然后在该列下应用一个空的td
Column_count是一个变量,用于跟踪行中列的当前位置
接下来是5个超低指示器,用于指示5列及其溢出状态。既然你说每一行都意味着30分钟,那么你的逻辑是,如果开始时间等于每一行变化导致30分钟增量的时间,那么你就可以在tr中找到一个小时是否必须开始
在解释中,溢出_X用于每个单独的变量名
伪码
如果您事先不知道有多少个类,那么您应该首先遍历这些类,并使用相关数据构建数据库或csv文件或json文件
逻辑的Javascript实现