根据选择填充并显示数据表

时间:2018-12-18 18:25:55

标签: primefaces

我要在用户从选择菜单中选择用户后填充并显示数据表,如下所示:

<div align="center">
 <h:selectOneMenu value="#{dtEditView.user}" >
 <f:selectItem itemLabel="FastWeb" itemValue="FastWeb"/>
 <f:selectItem itemLabel="george" itemValue="george"/>
 </h:selectOneMenu><p:spacer/>
<p:commandButton value="Submit" action="#{dtEditView.enabletable()}" update="data"/>
<p:outputPanel id="data">
                    <p:dataTable id="users1" var="user" value="#{dtEditView.users1}" editable="true"  resizableColumns="true" style="margin-bottom:20px" widgetVar="usersTable" rendered="#{dtEditView.showtable}">
<p:column headerText="No" filterBy="#{user.no}" filterMatchMode="contains" sortBy="#{user.no}">
                        <p:cellEditor>
                            <f:facet name="output"><h:outputText value="#{user.no}" /></f:facet>
                            <f:facet name="input"><p:inputText id="modelInput" value="#{user.no}" style="width:100%"/></f:facet>
                        </p:cellEditor>
                    </p:column>

                    <p:column headerText="A/C No" filterBy="#{user.USERID}" filterMatchMode="contains" sortBy="#{user.USERID}">
                        <p:cellEditor>
                            <f:facet name="output"><h:outputText value="#{user.USERID}" /></f:facet>
                            <f:facet name="input"><p:inputText id="modelInput2" value="#{user.USERID}" style="width:100%"/></f:facet>
                        </p:cellEditor>
                    </p:column> 
</
</p:dataTable>
                    </p:outputPanel>

我的托管bean

@ManagedBean(name="dtEditView")
@ViewScoped
public class EditView implements Serializable {
private List<User> users1;
@ManagedProperty("#{quantumService}")
    private QuantumService service;
@PostConstruct
    public void init() {
        try {
           users1 = service.createUsers(user);

        } catch (SQLException ex) {
            Logger.getLogger(EditView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


 public List<User> getUsers1() {
        return users1;
    }

该类太大,因此只提供部分代码来演示我到目前为止所做的事情。我将选择的项目传递给该方法,该方法从数据库查询用户并创建一个列表,然后依次使用该列表来填充我的数据表。

1 个答案:

答案 0 :(得分:0)

以上代码的工作原理很吸引人,我希望您认为这是否是最好的方法。

  

我的Jsf页面如下:-

<?xml version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>User's Page</title>
    </h:head>
    <h:body>
            <h:form id="form">
<h:selectOneMenu value="#{dtEditView.user}" >
 <f:selectItem itemLabel="FastWeb" itemValue="FastWeb"/>
 <f:selectItem itemLabel="george" itemValue="george"/>
<p:growl id="msgs" showDetail="true"/>
                <fieldset>
                    <legend>Select user you want to manage his/her users and click on 'Submit'</legend>

                <div align="center">
                    <h:selectOneMenu value="#{dtEditView.user}" >
                    <f:selectItem itemLabel="FastWeb" itemValue="FastWeb"/>
                    <f:selectItem itemLabel="george" itemValue="george"/>
                </h:selectOneMenu><p:spacer/>
                <p:commandButton value="Submit" action="#{dtEditView.enabletable()}" update="data"/>
                </div>
                <p:outputPanel id="data">
                    <p:dataTable id="users1" var="user" value="#{dtEditView.users1}" editable="true"  resizableColumns="true" style="margin-bottom:20px" widgetVar="usersTable" rendered="#{dtEditView.showtable}">
                   <p:column headerText="No" filterBy="#{user.no}" filterMatchMode="contains" sortBy="#{user.no}">
                        <p:cellEditor>
                            <f:facet name="output"><h:outputText value="#{user.no}" /></f:facet>
                            <f:facet name="input"><p:inputText id="modelInput" value="#{user.no}" style="width:100%"/></f:facet>
                        </p:cellEditor>
                    </p:column>

                    <p:column headerText="A/C No" filterBy="#{user.USERID}" filterMatchMode="contains" sortBy="#{user.USERID}">
                        <p:cellEditor>
                            <f:facet name="output"><h:outputText value="#{user.USERID}" /></f:facet>
                            <f:facet name="input"><p:inputText id="modelInput2" value="#{user.USERID}" style="width:100%"/></f:facet>
                        </p:cellEditor>
                    </p:column> 
 </p:dataTable>
                    </p:outputPanel>
                     </fieldset>
 </h:form>

    </h:body>
</html>
  

ManagedBean

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import pack.User;
    @ManagedBean(name="dtEditView")
    @ViewScoped
    public class EditView implements Serializable {

        private List<User> users1;
        private List<Device> devices;
        private Boolean showtable;
        private String user;

        @ManagedProperty("#{quantumService}")
        private QuantumService service;

        public Boolean getShowtable() {

            return showtable;
        }

        public void setShowtable(Boolean showtable) {
            this.showtable = showtable;
        }
         //I pass my selected user at this level, I had done this on @PostConstruct but at that level, the value is still null
        public String enabletable() throws SQLException {
            showtable = true;
            users1 = service.createUsers(user);
            return "";
            }
            public List<User> getUsers1() {

            return users1;
        }
           public String disabletable() {
            showtable = false;
            return "";
            }

        public String getUser() {
            return user;
        }

        public void setUser(String user) {
            this.user = user;
        }

        public void setService(QuantumService service) {
            this.service = service;
        }


        }
    }

> Service ManagedBean
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import pack.Device;
    @ManagedBean(name = "quantumService")
    @ApplicationScoped
    public class QuantumService {

        public List<User> createUsers(String senderid) throws SQLException{
        jdbcutils.DBConnectMain utils=new jdbcutils.DBConnectMain();
        Connection con = utils.getConnection();
        PreparedStatement ps = con.prepareStatement("SELECT * FROM users where usertype is null and senderid=?");
        ps.setString(1, senderid);
        ResultSet rs = ps.executeQuery();
            List<User> list = new ArrayList<>(); 
            while(rs.next()){

                list.add(new User(rs.getString("id"),rs.getString("Name"),rs.getString("Gender"),rs.getString("Title"),rs.getString("Phone"),rs.getString("ID Card No"),rs.getString("Phone1"),rs.getString("Phone2"),rs.getString("Nationality"),rs.getString("DOB"),rs.getString("CardNo"),rs.getString("street"),rs.getString("Serial"),rs.getString("hired"),rs.getString("Badgenumber"),rs.getString("usertype"),rs.getString("password")));

                    }

            return list;
        }

    }
  

这是我的代码,其中省略了User bean,我希望这很清楚。我仍在等待反馈,这是实现此目标的最佳方法吗?