• Post Reply Bookmark Topic Watch Topic
  • New Topic

Populate Text Fields based on dropdown

 
Viky Kizz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have list of employees in my database and their associated information like employeeId,supervisor and process. I have class named EmployeeDetails having same properties. I am fetching details of all users using below code:



In employeeList, we have list of objects having data of employees. I donot know how to get which object is selected in dropdown and based on that rest of the text fields are populated. Example:
Name ID Supervisor
-------------------------------------------------------
Pawan Kumar 3033045 Vimal Kumar
Vimal Kumar 3040901 Dinesh Hemrajani

If Pawan Kumar is selected from dropdown then '3033045' and 'Vimal Kumar' should get populated.

Please help!!!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things:
  • You should not be using Java scriptlets in your JSP. That is an obsolete and discredited practice from over 12 years ago. (12 years! ) You should be using the JSTL and EL.
  • In order to handle changes made to the selected value, you will use JavaScript.
  •  
    Viky Kizz
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks.....i will remove JSTL or EL.

    What i am not getting is how would i pass EmployeeDetails object to javascript and then use it assign value to my textfields. As, i selected one of the EmployeeName from dropdown the object from which employeename is fetched need to be passed to javascript and the use that object to use rest of the getters method. But, we cannot use java objects in javascript.

    So, is there a way i can refer the object of which name is selected in dropdown from javascript .
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65530
    108
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Viky Kizz wrote:Thanks.....i will remove JSTL or EL.

    Remove? You should be using JSTL/EL in place of Java scriptlets.

    What i am not getting is how would i pass EmployeeDetails object to javascript and then use it assign value to my textfields. As, i selected one of the EmployeeName from dropdown the object from which employeename is fetched need to be passed to javascript and the use that object to use rest of the getters method. But, we cannot use java objects in javascript.

    That's correct -- you cannot mix the Java and the JavaScript. Once the page is sent to the browser, all Java is done. See this article if you want an explanation of that.

    What you'd need to do is to handle changes to the select element with a change handler, that goes back to the server via an Ajax request. The Ajax request can either return a pre-formated HTML fragment to replace the form (you can use a JSP fragment for this), or it can return JSON data for the field values, and JavaScript would be used to update the fields values. For this usage, where only the field values need to be replaced, I'd probably go with the JSON route.

    For all of this, I'd use jQuery's help with the JavaScript. Life is too short to deal with cross-browser issue in event handling and Ajax.

    So, is there a way i can refer the object of which name is selected in dropdown from javascript .

    The value of the options should be the id of the represented items so that you know which item was selected.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!