0

I have two combo- boxes and I want to get the values from the option selected in the first combo box and, the list is present in the class object which is to be selected in the first combo-box

E.g I have two Branches Accounts and Legislation Account has further two sub-branches: Finance,financial statement which are present in the Branch Object as a list

Legislation: Questions, Resolution

When the user selects the Account, the second combo box automatically loads the list in Account Object into the second combo-box

1st combo-box -- selected Account 2nd combo-box -- shows Finance, Financial Statement

<option  />
                   <c:forEach items="${branchList}" var="branch">
                     <option value="${branch.branchName}">${branch.branchName}</option>
                    </c:forEach>
                   </select>

<td>Sub Branch</td>
                   <td> <select name="subBranchName" >
                   <c:forEach items="${subBranchList}" var="subBranch">
                     <option value="${subBranch.subBranchName}">${subBranch.subBranchName}</option>
                    </c:forEach>
                   </select> </td>

My Branch Class:

@Entity
@NamedQuery(name="Branch.findAll", query="SELECT b FROM Branch b")
public class Branch  {


    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="BRANCH_ID")
    private long branchId;

    @Column(name="BRANCH_NAME")
    private String branchName;

    //bi-directional many-to-one association to EmployeeDetail
    @OneToMany(mappedBy="branch")
    private List<EmployeeDetail> employeeDetails;

    //bi-directional many-to-one association to SubBranch
    @OneToMany(mappedBy="branch")
    private List<SubBranch> subBranches;

// Getters & Setters

My Controller:

@RequestMapping("createFile")
    public ModelAndView createFile(@ModelAttribute Employee employee , Model model) {

        List<Branch> branchList = branchService.getList();
        model.addAttribute("branchList", branchList);
        ArrayList<String> documentType = new ArrayList<String>();
        documentType.add("PUC");
        documentType.add("Annex");
        model.addAttribute("documentType", documentType);
        String notings = " ";
        model.addAttribute("notings", notings);

            logger.info("branch : "+branchList.get(1).getBranchName());

        List<SubBranch> subBranchList = new ArrayList<SubBranch>();
        subBranchList = subBranchService.getList();
        model.addAttribute("subBranchList", subBranchList);

        List<EmployeeDetail> employeeDetailList = employeeDetailService.getList();
        model.addAttribute("employeeDetailList", employeeDetailList);
        MultipartFile file = null ;

        model.addAttribute("fileUpload", file);


        return new ModelAndView("createNewFile");
    }

The code is working fine when I load the sub-Branches list separately, but I want to load the list of Sub-Branches from the value of Branches i-e when a branch is selected the program automatically loads the list of sub-branches, the list of sub-branches is present in the Branch class that is the value I want to load into the sub-Branch list

I am new at Spring MVC and don't know how to load the values without invoking the controller.

Thanks in advance

Haider Sultan
  • 121
  • 1
  • 8
  • Why don't u try an AJAX call on change of the dropdown selection or try the [cascading dropdown](http://stackoverflow.com/questions/18351921/how-to-populate-a-cascading-dropdown-with-jquery) concept – Jop.pop Aug 24 '15 at 17:45
  • Thank You for the reply Bhanu, the problem is I have to get the values from databases and new values could be entered in the database, so it can't be static and I have only mentioned two values here but I have to load 20 branches and they have further 60,70 sub-branches – Haider Sultan Aug 24 '15 at 17:50
  • In that case, I would prefer an AJAX call to get the values on demand and populate into the dropdown. I don't think you can populate the values as when they come in DB directly. If so, it is completely a different thing and I did read it somewhere.. Google for Websocket – Jop.pop Aug 24 '15 at 17:54
  • I am getting your point but the list I want to get is already in the object of Class Account which is being selected in the first combo-box, I am getting the values from DB and saving in the Object of class Branch, I have to load that list in second combo-box, I hope I have cleared up my problem – Haider Sultan Aug 24 '15 at 18:02
  • Pull the values to a hidden field and load them when required. Once the page is completely rendered, the object in controller becomes invalid. – Jop.pop Aug 24 '15 at 18:08
  • Can you point me towards a link or a topic which could tell me how to do that, will be much appreciated – Haider Sultan Aug 24 '15 at 18:12
  • Does that sound anyway near to you rexpectation? You can [refer this](http://stackoverflow.com/questions/24994127/how-to-create-multilevel-dynamic-dropdown-using-jquery). If you want an exact working example, I can help you before EOD – Jop.pop Aug 24 '15 at 18:27

0 Answers0