0

I am working with Servlet and JSP. I have a JSP form in which I have a drop down list which is getting generated dynamically so as of now I have three options in the drop down list.

HTML CODE--

<form id='form' method='post' action='/test/'>
<select id="type" name="typeOne">

<optgroup id="first" name="First" label="FirstDiv">
    <option value="value1" id="001">valuee1 count</option>
    <option value="value2" id="002">value2</option>
    <option value="value3" id="003">value3
    </option>
</optgroup>
</select>
</form>

Now I need to disable few of the options in my above drop down list using jquery. Below is my jquery in which I am disabling the 001 and 002 options but I have hardcoded the id's in my below jquery -

<script>
    $(document).ready(function () {
        $(function () {
            $("#type option[id='001']").prop("disabled", true);
            $("#type option[id='002']").prop("disabled", true);
        });
    });

</script>

Below is my servlet from which I am passing a list which will have all my id's which I want to disable in my drop down list.

List<String> storeTest = new ArrayList<String>();
storeTest.add("001");
storeTest.add("002");

req.setAttribute("store", storeTest);

Problem Statement:-

Now my question is - how do I iterate store list in the jquery and disable 001 and 002 options in the drop down list?

3
  • first you need to get the data on client side (i.e. to your jQuery) for that either you need to make another ajax call to servlet and return data in parsable format or you pass the data along with main page in some hidden part of page and read it from jQuery and process it Commented Jun 9, 2014 at 6:04
  • 1
    OP is already passing the store list in a request attribute I guess. Commented Jun 9, 2014 at 6:23
  • Yes, it is already passing the value. I need to know how can I use that value from the list in my jsp and further iterate to create a disable jquery statement Commented Jun 9, 2014 at 6:29

1 Answer 1

1

Prepare your selectors in Servlet , then inject it in JSP :

MyServlet.java

List<String> storeTest = new ArrayList<String>();
storeTest.add("001");
storeTest.add("002");
// If you have Java8 : use Lamda list.stream().map(e->'#'+e)
req.setAttribute("store",StringUtils.join(mapForId(storeTest).toArray(),",")); 

this.getServletContext().getRequestDispatcher( "/myPage.jsp" ).forward( req, response );

then in your jsp , adding an expression as following:

myPage.jsp

<script type="text/javascript">
//....
jQuery(document).ready(function () {
        jQuery(function () {
            jQuery(<%=request.getAttribute("store") %>).prop("disabled", true);
            
        });
    });

</script>

Known that mapForId is another method in Servlet :

List<String> mapForId(List l){
  List<String> newArr=new ArrayList<String>();
  for(String attr : l){
      newArr.add("option#"+attr);
  }
  return newArr;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you عبد النور التومي, for the reply, I am using java 6 not java 8? how to do it in Java 6?
UPDATED! if there is a pb of casting object , i hope that u can deal with. And if jQuery(<%=request.getAttribute("store") %>) does not work , try adding ' ' : jQuery('<%=request.getAttribute("store") %>')
I am getting error on .join(",")); do I have to add any maven dependency or create any method?? error is - cannot resolve method join()
Resolved, the mapForId() method was appending option# in front of the id selector. thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.