sasuri
sasuri

Reputation: 992

PrimeFaces showing one item of list on a row

I'm using datatable to retrieve records from database. Datatable is showing all the items on rows as expected. What I want is how to choose only a couple or single one of them, could it be by specifying the index of the item in datatable? or by the SQL query?

bean :

public JcalendarController getSelectedUser() {
    return selectedday;
}

public void setSelectedUser(JcalendarController selectedday) {
    this.selectedday = selectedday;
}

List<String> user_spinner_list = new ArrayList<String>();
List<JcalendarController> calendarlist = new ArrayList<JcalendarController>();

public void delete() {
    System.out.println("JadminBeans >> delete() ---------- id= ");
    JcalendarDAO.deleteDay(selectedday);
}

public List<JcalendarController> getMessages() {
    System.out.println("List<JcalendarController> getMessages()");
    calendarlist = JcalendarDAO.getdays();
    return calendarlist;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public int getDay() {
    return day;
}

public void setDay(int day) {
    this.day = day;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public UploadedFile getFile() {
    return file;
}

public void setFile(UploadedFile file) {
    this.file = file;
}

//--------------------------------------------------------------------------
//---------------------------- user_spinner() ----------------------------//
public List<String> user_spinner() {
    System.out.println("List<JcalendarBeans> user_spinner()");

    user_spinner_list = JcalendarDAO.AllUsarname_spinner();
    return user_spinner_list;
}

//---------------------------- ImageUpload() ----------------------------//
public void ImageUpload() {
    JcalendarController CC = new JcalendarController(this.username, this.day, this.file);
    System.out.println(this.username + " " + this.day + " " + this.file);
    calendarlist.add(CC);
    JcalendarDAO.add_image_DAO(CC);
}

//---------------------------- TextUpload() ----------------------------//
public void TextUpload() {
    JcalendarController CC = new JcalendarController(this.username, this.day, this.text);
    System.out.println(this.username + " " + this.day + " " + this.text);
    calendarlist.add(CC);
    JcalendarDAO.add_text_DAO(CC);
}

DAO :

public static List<JcalendarController> getdays() {

    List<JcalendarController> ccs = new ArrayList<JcalendarController>();

    try {
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery("select * from calendar");

        while (rs.next()) {
            JcalendarController cc = new JcalendarController();
            cc.setId(rs.getLong("id"));
            cc.setDay(rs.getInt("day"));
            cc.setText(rs.getString("text"));
            cc.setUsername(rs.getString("username"));

        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return ccs;
}

XHTML

<p:dataTable value="#{Jcalendar.messages}" var="o" paginator="true" selection="#{Jcalendar.selectedUser}"
             rowKey="#{o.id}" style="margin-bottom:20px"
             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
             rowsPerPageTemplate="24,48,144">

    <f:facet name="header">
        <h:outputText value="Data showing from database" />
    </f:facet>

    <p:column selectionMode="single" />
    <p:column>
        <f:facet name="header">
            <h:outputText value=" Id" />
        </f:facet>

        <h:outputText value="#{o.id}" />
    </p:column>

    <p:column>
        <f:facet name="header">
            <h:outputText value="Day" />
        </f:facet>

        <h:outputText value="#{o.day}" />
    </p:column>

    <p:column>
        <f:facet name="header">
            <h:outputText value="Text" />
        </f:facet>

        <h:outputText value="#{o.text}" />
    </p:column>

    <p:column>
        <f:facet name="header">
            <h:outputText value="Username" />
        </f:facet>

        <h:outputText value="#{o.username}" />
    </p:column>

    <p:column>
        <f:facet name="header">
            <h:outputText value="Image" />
        </f:facet>

        <h:outputLink value="DisplayImage?id=#{o.id}" target="_blank">
            <h:graphicImage value="DisplayImage?id=#{o.id}" width="50" height="50"></h:graphicImage>
        </h:outputLink>
    </p:column>

    <f:facet name="footer">
        <p:commandButton value="Delete" action="#{Jcalendar.delete()}" ajax="false" update=":form:msgs"/>
    </f:facet>
</p:dataTable>

Upvotes: 0

Views: 731

Answers (1)

Master Slave
Master Slave

Reputation: 28569

The datatable will show the values of the messages property, so whatever is in the list returned by the getMessages() will be rendered inside the table.

For what concerns you question, it is best to make a DAO method and return from the DB only the needed records, putting it in your words, use the SQL.

One optimization hint, you should avoid using any logic inside your getter methods as during the life-cycle of a single request, it can be called multiple times. In your case it would contact a DB upon every call. You can check this post to learn more, and find a better way to initialize your list.

Upvotes: 2

Related Questions