aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Total newby javascript question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Total newby javascript question" Watch "Total newby javascript question" New topic
Author

Total newby javascript question

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 579
I have no experience of java script, trying to do the following create two selects, the second select is dependent on the first. I can do thsi in java no problem, but have been told to use JavaScript also has its a very old app it does not have AJAX so its javascript all the way.

Looking on the net I found a JQuery pluging jquery.doubleSelect.js.

Which does what I need it to do, what I need to do is set the values.



These are values hard coded, what I have is a ArrayList of objects each object contains a ararylist of objects which will go in the second select.

I have no idea how to convert acolloction of java objects into a collection of javacript objects to loop through.

I have googled but just got java vs javascripts what the difference none that really show you how to use java an javascrip together.

Thanks for any help
Mike Firkser
Ranch Hand

Joined: Oct 21, 2003
Posts: 247

I assume this is on a jsp page, so I would do something like this :



Hopefully that is what you're looking for


Mike Firkser
Rutgers '84
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Except that you'd use JSTL, because scriptlets are horrible.
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 579
so if I had a object say


City being another object, so for every country selected the second pull down will contain all the citys for that country.
so the javascript code


would translate to


or JSTL



with the countryArray passed into the request object

Can a javascriptArray be a array of objects that can contain an array of java objects.

Or do I need to declare such an object an build it up parameter by paramter.

Is there a real good java script java link I could look at.

Thanks for your Help
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617


No, you still need to build up the JavaScript by looping over the Java array.

You're over-complicating this: remember, JSP just creates HTML, which in turn contains the JavaScript. How would you do this if you were building an HTML table out of each entry? (With the caveat that you *must* use JavaScript-safe values--what if a country had a quote in it?)

Another option, perhaps much cleaner, is to convert the country array to JSON on the server side (there are libraries to do this) and just use that.
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 579
Thanks for the reply is thuis legal it doesnt work yet but is it on at least one of the right tracks for solving the problem.


Takes the country list out of the request for each one sets country name country id and for the second select sets each city an city id from that country city list
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Sort of, but man, that's ugly. That's why I suggested building the JSON on the server side, for which there are several library alternatives.
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 579
Got it working

Yes I know its ugly but it means that I dont have to go back to the server all the data is in the request object. That way the page does not need to be refreshed.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You've misunderstood.

You're already sending the data from the server. Send it as JSON rather than building JSON on the client side.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Tony Evans wrote:JavaScript also has its a very old app it does not have AJAX so its javascript all the way.


FYI: Ajax is JavaScript and it does not matter if the application is old or new.

Eric
 
 
subject: Total newby javascript question