0

Following is my jsp code.

<c:forEach items="${nameList}" var="studentList">
        <tr>
            <td style="padding-bottom: .5em;">
                <a id="student" href="number?regNo=<c:out value="${studentList.regNo}"/>">
                    <span class="eachStudent">
                        <span class="studentName"><c:out value="${studentList.name}"/></span><br/>
                        <span class="collName"><c:out value="${studentList.collName}"/></span><br/>
                        <span class="deptName"><c:out value="${studentList.deptName}"/></span><br/>
                    </span>

                </a>
            </td>
        </tr>

     </c:forEach>

The above c:forEach lists

Name         CollName        DeptName
ABCD         coll1             dept1
kfkdb        coll1             dept2
jbdd         coll2             dept3

Following is the code that lists the collName and deptName respectively.

<div>
    Filter students by College (not required):
    <select id="byCollege" name="byCollege" >
        <c:forEach items="${uniqueCollList}" var="uniqueCollList">
            <option value="${uniqueCollList}"/>
                ${uniqueCollList}</option >
        </c:forEach >
    </select >
    </div>

    <div>
    Filter students by Department Name (not required):
    <select id="byDept" name="byDept" >
        <c:forEach items="${uniqueDeptList}" var="uniqueDeptList">
            <option value="${uniqueDeptList}"/>
                ${uniqueDeptList}</option >
        </c:forEach >
    </select >
    </div>

Now when I select a value from first dropdown, I want to filter the results given by foreach using the value selected in dropdown. I would like to do this in front end itself, rather going to the back end. May I know how can I do this?

After filtering the c:foreach result using value of first dropdown, if I select a value in second dropdown, I want the updated result of c:foreach to be filtered using the value selected in second drop down.

How can I do this?

If I want to do this in back end, what should I do?

PS:Following is the controller code that sends the list first time

@RequestMapping(value = "/name", method = RequestMethod.POST, params = { "studentName" })
    public String searchStudentByCollOrDept(@RequestParam(value = "studentName", required = true)String studentName, ModelMap model){

        List<OneStudentResult> nameList = resultService.getStudentList(studentName);
        //TODO,  null value check.
        if(nameList.size() == 0 || nameList == null){

            return "header";
        }
        if(nameList.size() != 0){
            // Iterate the list that we get and add only one time a collName and deptname.So a Treeset.
            Set<String> uniqueCollList = new TreeSet<String>();

            Iterator<OneStudentResult> itr = nameList.iterator();
            while(itr.hasNext()){
                String collName = itr.next().getCollName();
                if(!uniqueCollList.contains(collName)){
                    uniqueCollList.add(collName);
                }               
            }
            uniqueCollList.add(" Select a College ");
            model.addAttribute("uniqueCollList", uniqueCollList);

            Set<String> uniqueDeptList = new TreeSet<String>();
            Iterator<OneStudentResult> itrDeptList = nameList.iterator();
            while(itrDeptList.hasNext()){
                String deptName = itrDeptList.next().getDeptName();
                if(!uniqueDeptList.contains(deptName)){
                    uniqueDeptList.add(deptName);
                }
            }
            uniqueDeptList.add(" Select a Department ");
            model.addAttribute("uniqueDeptList", uniqueDeptList);
        }

        model.addAttribute("nameList", nameList);
        return "nameResult";
    }
sofs1
  • 3,834
  • 11
  • 51
  • 89

1 Answers1

1

I'm afraid there is no simple solution to your problem. You should consider doing this server-side with ajax updates. Client side filtering would either require you to parse the data from the HTML generated by your studentList or it would require you to inject the data as a JSON formatted array. In both cases you would end up with doubled data (server & client). Having the data on the client-side would only allow you to disable some of the option values, not hide them. So if you want real filtering in terms of not-showing some of the options then you should filter your lists on the server. To do so you should post the selected option from your first dropdown "byCollege" to your backend in order to retrieve a filtered "uniqueDeptList" that you use to rebuild the "byDept" checkbox. You might want to use jQuery for both tasks.

Steps:

1. Handle the change-event of the "byCollege" dropdown

2. Post the selected option value to your servlet

3. Filter the data in your servlet and return the filtered data as POST response (omitted in this example)

4. Remove the old select options and recreate them from the filtered data

$("#byCollege").change(function() {
  $("select option:selected").first().each(function() {
    // Get and convert the data for sending
    // Example: This variable contains the selected option-text
    var outdata = $(this).text();
    // Send the data as an ajax POST request
    $.ajax({
      url: "yourjsonservlet",
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(outdata),
      contentType: 'application/json',
      mimeType: 'application/json',
      success: function(data) {
        // Remove old select options from the DOM                     
        $('#byCollege')
          .find('option')
          .remove()
          .end();
        // Parse data and append new select options 
        //(omitted here; e.g. $('#byCollege').append($("<option>").attr(...))                         
      },
      error: function(data, status, er) {
        alert("error: " + data + " status: " + status + " er:" + er);
      }
    });
  });
});
Frank
  • 2,507
  • 1
  • 12
  • 11
  • Wow. Awesome. Well explained and I was expecting the same that it would be difficult to do in client side. Can I get a sample jQuery code for this case? It would be really helpful. – sofs1 Oct 14 '14 at 06:11
  • Thank you very much Frank. I updated the controller code as well. If you could help me some more by giving working code, it will be very useful. I don't know AJAX or JSON. I am in the process of learning js and just completed html and jsp. Thanks a lot again. – sofs1 Oct 15 '14 at 06:44
  • So you're using the Spring MVC framework. That complicates thing a bit as I am not that familliar with Spring. But I will still try to give you some working example. Although it may take a while. – Frank Oct 15 '14 at 08:52
  • BTW: In your code you are iterating the same collection twice! Why do you do this? It would look nicer if you would build both list in one iteration. And you might want to use the "for" keyword to iterate the nameList. This would improve your code through eliminating the need for the additional Iterator. See http://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java. – Frank Oct 15 '14 at 13:45
  • ...and you don't have to double check the initial condition. So you should remove the second if-clause -> [if(nameList.size() != 0)] as your initial guard statement already ensures that the size of the nameList is not 0. – Frank Oct 15 '14 at 13:59
  • I wrote the code as you said. Now I am struck with this problem. Could you help me http://stackoverflow.com/questions/26447194/how-do-i-update-a-cforeach-using-ajax-in-spring-project – sofs1 Oct 20 '14 at 00:04