Johan Nordli
Johan Nordli

Reputation: 1230

Datatable 7 items on each row

Let's say I have a list of all the days in a month and I want to print it as a calendar. I want a week on each line and then a row break

in my example below, I will get a day on each row, like this: enter image description here

Which is the best way to get 7 days on each row like this in JSF? enter image description here

Example code:

View:

 <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;
  }

}

Upvotes: 0

Views: 78

Answers (1)

Patrick
Patrick

Reputation: 4572

if you would like to use Primefaces in your Project, you could do it in this way.

<!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>

The Controller class

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

Looks not very nice, but does what you need.

Patrick

Upvotes: 1

Related Questions