datatable - how to get the index of data from data Table in jsf? -


this question has answer here:

i new java ee , dont have idea jsf , all. making simple java web application fetches data database , shows in datatable. need edit data selected user datatable need value of row selected/clicked. havent been able it. can 1 please me code ? hope tell me how can following codes.

showrecords.xhtml

h:datatable  value="#{studentlist.studentl()}" var="student" styleclass="studenttable"                          columnclasses=",,,fixedwidth">                 <h:column>                     <f:facet name="header">student id</f:facet>                     <h:outputtext value="#{student.studentid}"></h:outputtext>                 </h:column>              <h:column>                 <f:facet name="header">name</f:facet>                 <h:outputtext value="#{student.fname}"></h:outputtext>             </h:column> 

student.java

@managedbean(name="student")  public class student {  @id  private string studentid;    private string fname, lname, mname="noname"; /*******getters , setters** , database transaction****/  } 

studentlist.java

@managedbean(name="studentlist") @sessionscoped  public class studentlist {     public list<student> studentl(){          list<student> list = new arraylist<student>();         preparedstatement ps = null;         resultset rs = null;         connection con = null;          try{            class.forname("org.apache.derby.jdbc.clientdriver");            con = drivermanager.getconnection("jdbc:derby://localhost:1527/tourmanager","administrator","pass");            string sql = "select * student";            ps = con.preparestatement(sql);            rs = ps.executequery();             while(rs.next()){            student student1 = new student();            student1.setfname(rs.getstring("fname"));            student1.setlname(rs.getstring("lname"));            student1.setstudentid(rs.getstring("studentid"));  list.add(student1);          // return list;            }          }catch(exception e){             e.printstacktrace();         }           return list;      } public void editstudent() throws ioexception{         int index = integer.parseint(facescontext.getcurrentinstance().getexternalcontext().getrequestparametermap().get("index").tostring());         system.out.println(" selected row "+index);     }     } 

you can pass parameter using expression language.

in bean, have function like

public void editstudent(string studentid) {   // id } 

now, using expression language, can call method using #{yourbean.editstudent('id')}

since iterating through data using datatable, can access student's id var variable.

<h:datatable  value="#{studentlist.studentl()}" var="student" ...> ...     <h:commandbutton value="edit" action="#{studentlist.editstudent(student.studentid)}" /> ... </h:datatable> 

the expression language access getstudentid() (getter method studentid) method specific student.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -