File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes How do I obtain dynamic yyyymm dropdown field in jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How do I obtain dynamic yyyymm dropdown field in jsp? " Watch "How do I obtain dynamic yyyymm dropdown field in jsp? " New topic
Author

How do I obtain dynamic yyyymm dropdown field in jsp?

S Shiva
Greenhorn

Joined: Jun 01, 2013
Posts: 26
I want to create a dynamic dropdown date field in JSP.
It will have From date & To date.The following code gives dynamic year till current year.


This code gives years 2013,2012(as loop is only 2 times.If loop is 100 times then it will give year upto 1914.)
I need the field to show current month as well. ie: Todays date is 10/06/2013...The field should show....201306.
then 201305,201304,201303,.....,201212,201211,201210,....% so on.
Also right now year is displayed only once...In the desired output, year should be displayed as many times the month currently is...


Help me with the code..What should I add in the above code?
NOTE:: year and month should be in the same text field. and month no. should follow year upto current year.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2383
    
    7

Well looking at your code you only got the year part of the date. Say if you also get the month, how would you make into a string yyyymm format?


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
S Shiva
Greenhorn

Joined: Jun 01, 2013
Posts: 26
K. Tsang wrote:Well looking at your code you only got the year part of the date. Say if you also get the month, how would you make into a string yyyymm format?


The same way we append two strings..
But the problem is looping year part with current month value..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Step away from the keyboard, stop coding, and write down, in English or your native language, how you would solve this problem using pencil and paper. Until you can do that, you are not ready to start coding.

What is the first thing you need to do?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
S Shiva
Greenhorn

Joined: Jun 01, 2013
Posts: 26
OK Finally I created a code which gives me yyyymm dynamically upto current year month..


Only problem is it doesnot disply month no. 12 with current year(space is taken by it).. Also since this month is 06/2013, it gives blank selection choices from 2013 ,201311,....,201306.. The output now is like this : 2013 ,blank choices(also selectable) upto 201306(current month of current year).This is the image:::
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Wow. There's a lot of extra stuff in there that's not necessary. What's the purpose of monthfield and yearfield, for example? They're never used. (And it's a really really bad practice to name parameters and variables the same thing.)

Did you follow my advice and write out what needs to happen before coding? I cannot imagine that you did and came up with code that's so full of unnecessary dregs.

I advise again. Stop coding. Start writing.
S Shiva
Greenhorn

Joined: Jun 01, 2013
Posts: 26
Bear Bibeault wrote:Wow. There's a lot of extra stuff in there that's not necessary. What's the purpose of monthfield and yearfield, for example? They're never used. (And it's a really really bad practice to name parameters and variables the same thing.)

Did you follow my advice and write out what needs to happen before coding? I cannot imagine that you did and came up with code that's so full of unnecessary dregs.

I advise again. Stop coding. Start writing.



The year field and monthfield are called onload in the form action section(html code...) As seen in the image 2013 is displayed first... I need it to be 2013Current_month..
Bear Bibeault wrote:Stop coding. Start writing.

I cannot do that..I am on job..(on job training)..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

S Shiva wrote:The year field and monthfield are called onload in the form action section(html code...)

They are never used.

Bear Bibeault wrote:Stop coding. Start writing.

I cannot do that..I am on job..(on job training).


You feel that thinking about the best way to solve a problem is not part of your job?
S Shiva
Greenhorn

Joined: Jun 01, 2013
Posts: 26
Bear Bibeault wrote:You feel that thinking about the best way to solve a problem is not part of your job?


Ok.. I am going to start writing code as you suggested...
In the mean time can you say what I am doing wrong & why am I getting six blank selectable lines in the jsp?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Let's take a look at it after you pare the code down to remove the unnecessary aspects.

Or, you could open the browser debugger and single step through the code to find out where it's going wrong. But really, I think it's a waste of time until you refactor the code.
S Shiva
Greenhorn

Joined: Jun 01, 2013
Posts: 26
The only part of code that was unnecessary was the last line of code...
It removes 2013_ _(2 blank spaces of month no. 12)..
Rest all are required...
I call the function in the script

like this... and without monthfield and yearfield, how will I get data?
S Shiva
Greenhorn

Joined: Jun 01, 2013
Posts: 26
I got it.. All I had to do was remove id++ from for loop and put it after value of current yyyymm comes in the option..

Now how to get var from one javascript function to another?
See when first date is selected the second date field should only show dates available after first field..
So it will have to know the value in the first field..
How to get that in second function?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2383
    
    7

S Shiva wrote:I got it.. All I had to do was remove id++ from for loop and put it after value of current yyyymm comes in the option..

Now how to get var from one javascript function to another?
See when first date is selected the second date field should only show dates available after first field..
So it will have to know the value in the first field..
How to get that in second function?


You are getting the 2nd date only to do validation stuff right? The simple way is specify an "id" for first and second date then in javascript use getElementById().
S Shiva
Greenhorn

Joined: Jun 01, 2013
Posts: 26
K. Tsang wrote:You are getting the 2nd date only to do validation stuff right? The simple way is specify an "id" for first and second date then in javascript use getElementById().


Not validation...
When I select foreg:: 201105 in first box.. then all options less than 201105 should not appear in the second text box..
S Shiva
Greenhorn

Joined: Jun 01, 2013
Posts: 26
Thanks guys... I managed it..
I took value selected in first field and put it in in another function, and used an if condition
and I got what I wanted...

Now just asking.. in my year loop, I have used So, it will loop 20 times giving year upto 1994...
But when it will be 2014 then ut will only give year upto 1995.. I donot want that...
How can I permanantly set a lower base value for year....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I obtain dynamic yyyymm dropdown field in jsp?