views:

511

answers:

2

I have the question regarding rich faces and beans. I have a jsp page that is using richfaces and inside it I have the: rich:extendedDatatable component, that takes data from my MainBean as ArrayList (this bean queries the mySQL and puts results to the ArrayList that populates the dataTable later on). There are 4 columns in datatable, first 3 are h:outputLabels and the last one is checkbox. Now I have a question: how can I get information from selected row ? I mean, when user clicks checkbox, I want to take the id/name or whatever that is associated to this particular row, then when user clicks on Apply changed a4j: button I will update the database and when user logs in back again he will see updated info: e.g. checkbox is selected/not selected now because the user checked that. I believe that is a simple query for someone who worked with it. For me ex. flash developer it would be easy in as3, but here I didnt find the solution yet, please help.

Update:

Let me explain and publish the code. When user logs in, then I make a query to database where I have 2 tables. First one is with "activities" (act_id, name, description, date) and second one is called "common" (that stores user_id and act_id in it). My idea is to store data that tells me, which user is assigned to each activity. In this case, e.g. user with id 1, is using activity 1 2 and 3, and user with id 2 is using activity 2 and 4, then when query from database returns a result, I simply create an ArrayList with apropriate data. So when user clicks on checkbox, then insert query is made, when user unselect the checkbox, then delete query to database is made (based on user id and act_id) here is the code for query method, first I must get user_id:

public String login() {
    Statement stmt2 = null;
    Statement stmt3 = null;
    ResultSet rs = null;
    ResultSet rs2 = null;
    ResultSet rs3 = null;
    String sql = "SELECT * from user";
    String sql2 = "SELECT * from activities";
    try {
      conn = DriverManager.getConnection(jdbcUrl, user, pass);
      stmt = conn.createStatement();
      stmt2 = conn.createStatement();
      boolean selected = true;

      rs = stmt.executeQuery(sql);
      rs2 = stmt2.executeQuery(sql2);
       while(rs.next()) {
        if (username != null && password != null) {
          if (rs.getString("username").equals(username)
              && rs.getString("password").equals(password)) {
            id = rs.getString("user_id");
            stmt3 = conn.createStatement();
            while(rs2.next()){
              selected = false;
              String aid = rs2.getString("act_id");
              String name = rs2.getString("name");
              String desc = rs2.getString("desc");
              String date = rs2.getString("date");
              String sql3 = "SELECT * from common where uid="+id+"";
              rs3 = stmt3.executeQuery(sql3);
              while(rs3.next()) {
                 if(rs3.getString("aid").equals(aid)){
                   activities.add(new Activity(name, desc, date, true));
                   selected = true;
                 }

              }
              if(!selected)
                activities.add(new Activity(name, desc, date, false));
            }
            return "success";
          }
        }
    } 
    }catch (SQLException sqle) {
      sqle.printStackTrace();
    } finally {
      try {
        rs.close();
        stmt.close();
        conn.close();
      } catch (SQLException e) {
      }
    }
    return "failure";
  }

And rich faces view:

<a4j:form>
<rich:extendedDataTable id="activities" value="#{mainBean.activities}" var="acts" sortMode="single">
        <rich:column label="Name" sortable="true" sortBy="#{acts.name}">
        <f:facet name="header"> 
                 <h:outputText value="Name" />
          </f:facet>
        <h:outputLabel value="#{acts.name}" />
        </rich:column>
        <rich:column label="Description" sortable="true" sortBy="#{acts.description}">
        <f:facet name="header"> 
                 <h:outputText value="Description" />
          </f:facet>
        <h:outputLabel value="#{acts.description}" />
        </rich:column>
        <rich:column label="Date" sortable="true" sortBy="#{acts.date}">
        <f:facet name="header"> 
                 <h:outputText value="Date" />
          </f:facet>
        <h:outputLabel value="#{acts.date}" />
        </rich:column>  
        <rich:column label="Selected" sortable="true" sortBy="#{acts.selected}">
        <f:facet name="header"> 
                 <h:outputText value="Selected" />
          </f:facet>
        <h:selectBooleanCheckbox value="#{acts.selected}" />
        </rich:column>
        </rich:extendedDataTable>
        <h:commandButton value="Apply changes" action="#{mainBean.addActivity}" />
        </a4j:form>
+1  A: 

JSF has already updated the beans in the arraylist. Just persist it the usual way in the action method:

public void save() {
    mainBeanDAO.save(mainBeans);
}
BalusC
and remember to set `selectionMode="none"` so that the built-in selection in the extendedDataTable doesn't mess in.
Bozho
Could you look for example below please?
ortho
A: 

(post removed, was actually an update to the question)

ortho
Hi, Stackoverflow is an Q)
BalusC