Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts

    build csv with var or array?

    I need to create a csv field in a form, for dates such as -

    2302,2402,2502,0103,0203......

    I have a popup calendar where users can select different dates and a javascript sets the bgcolor of that cell to blue, they can also de-select their selections.

    When they have selected all the dates they require, I need the user to click a button which populates the field in the parent with the csv info (as above).

    The id of each date is the info I require, so 3rd of jans id in 0301 etc.

    I want to know the best way to build the info - the options I thought of are -

    A. To add to an array each time a date is selected and search/delete from the array if one is deselected - when the user pushes the final button, the page builds the csv info by looping the array and then sends the info to the form field.

    B. Or, create the csv as a var on the fly, so each selected date is added to the end of the var (with the comma added). Deselected dates could be removed using a reg exp?

    Which of these sound like it would work/work best. Any other suggestions?
    monkey
    Monkey

    My head hurts!

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think we'd have to see your popup calendar and what values it returns before we can say anything about your solutions.

    Also what do you mean by "ids"? Like does each cell have an id in your popup calendar? If you can do that then you're half-way there.

    Sadiq.

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Monkey

    My head hurts!

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Create a "hash array" using the id as the key. You will add the key as you select each date. You will delete the key when you de-select it.
    Code:
    var arr = new Array();
    function hilite(cell,weekend){
      if(document.getElementById(cell).bgColor == "#3399ff" && weekend){
    	document.getElementById(cell).bgColor = endcolor; 
    	arr[cell] = cell;
      }else if(document.getElementById(cell).bgColor == "#3399ff" && !weekend){
    	document.getElementById(cell).bgColor = "#ffffff";
    	delete arr[cell];
      }else{
    	document.getElementById(cell).bgColor = "#3399ff"; 
    	arr[cell] = cell;
      }
    }
    Then you access the items like this:
    Code:
    for (var item in arr){
      alert(arr[item]);
      //or just: alert(item)
    }

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Glenn - excellent!!

    I thought this was going to be tricky, but was the easiest bit, I didint think of using the id for the array key

    What do you mean by a "hash" array?

    Is there any way I can put the array in numeric order? I tried

    arr.sort() but this then returned 1,2,3....

    cheers

    PS while your at it, could you have a look at the cellspacing below the days in feb and aug - any ideas why this is bigger? I have tried all sorts, it seem to be related to months that start on the sunday?
    Last edited by Boxhead; 03-23-2004 at 11:10 AM.
    Monkey

    My head hurts!

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    You can't sort a "hash" array. Why do you have to? If you really need it to be sorted then put the items in the hash array into an index-based array and do the sorting.

    A hash array stores name-value pairs (referred to as the key and item respectively) unlike in normal array which is index-based. Hash arrays are the same as Hashtable in Java, Dictionary in VBScript.

    With regard to the cellspacing, you have two opening <tr> tags in Feb and Aug.

    <TR>
    <TR>

  • #7
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Glenn

    are you using a view source bookmarklet? if so I am getting 2 <tr>s as well, but cant work out why?

    cheers
    Monkey

    My head hurts!

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Yes I am.

    Take a closer look at your code. You also have two closing </tr> tags.
    Code:
    day_title("Sat")
    document.write("</tr><tr>")
    ...
    // fill the remaining weeks
    	while (day <= month_length) {
    		for (var i=1;i<=7 && day<=month_length;i++){
    			//create the date with zero at start if below 10th
    			caldate=day+month_date;
    			if(i==1){
    				document.write("<tr><td align=center ...");
    ...
    	document.write("</tr>")  
    	}
    	document.write("</tr></table>")

  • #9
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I dont get it!

    I've made some changes....but I am still getting pairs of unneeded td and tr tags.

    I cant work out where my script is going wrong

    please help!

    cheers
    Monkey

    My head hurts!

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Now you're getting excess <tr></tr>.

    Just remove the code in red.

    day_title("Sat")
    document.write("</tr><tr>")
    // pad cells before first day of month
    ...
    document.write("</tr>")
    // fill the remaining weeks
    while (day <= month_length) {
    ...
    }

  • #11
    Regular Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    excellent! works fine now....but i'm still confused.

    Where are the tr tags for the first week written in in the code? The days tr ends then its straight into td's?

    cheers
    Monkey

    My head hurts!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •