2014-07-21 76 views
0

比方說,我有一個月的所有日子的列表,我想打印它作爲日曆。我想一個星期在每一行,然後連續突破Datatable每行7個項目

在我下面的例子

,我會在每一行一天,像這樣: enter image description here

這是獲得7天的最佳途徑JSF中每行都是這樣的? enter image description here

示例代碼:

查看:

<h:dataTable value="#{myController.dayList}" var="day"> 
      <h:column> 
       <h:outputText value="#{day}"/> 
      </h:column> 

</h:dataTable> 

Backbean:

@ManagedBean(name = "myController") 
@SessionScoped 
public class MyController { 

    private List <int> dayList; 

    public MyController() 
    { 
     dayList = getAllDaysInMonth(); 
    } 

    public List <int> getAllDaysInMonth() 
    { 
    ..... 
    } 

    public List <int> getDayList() 
    { 
     return dayList; 
    } 

    public void setDayList(List <int> dayList) 
    { 
    this.dayList = dayList; 
    } 

} 

回答

1

,如果你想在你的項目中使用Primefaces,你可以這樣做。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:p="http://primefaces.org/ui"> 
<body> 
    <p:dataGrid value="#{myController.dayList}" var="day" columns="7"> 
     <p:panel style="text-align: center; background-color: skyblue; width: 100px; height: 100px;"> 
      Day ${day} 
     </p:panel> 
    </p:dataGrid> 
</body> 
</html> 

控制器類

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 

import javax.annotation.PostConstruct; 
import javax.enterprise.context.SessionScoped; 
import javax.inject.Named; 

@Named(value = "myController") 
@SessionScoped 
public class MyController implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    List<Integer> dayList = new ArrayList<>(); 

    public MyController() { 
    } 

    @PostConstruct 
    public void init() { 
     for(int i = 1; i <= 31; i++) { 
      dayList.add(new Integer(i)); 
     } 
    } 

    public List<Integer> getDayList() { 
     return dayList; 
    } 

    public void setDayList(List<Integer> dayList) { 
     this.dayList = dayList; 
    } 
} 

enter image description here

看起來不是很漂亮,但確實你需要什麼。

Patrick

+0

謝謝!我使用primefaces,您的解決方案效果很好!再次感謝! –