0

I'm trying to output all dates from start to end date to array. Once I get all dates in array I have created a function that loop thorough array and add a className. Here is my code that creates array:

<cfoutput query="qryOne">
    <cfloop from="#PickDateTime#" to="#DropDateTime#" index="i" step="#CreateTimeSpan(1,0,0,0)#">
        dateArray[#currentrow#] = new Array("#UserID#","#dateformat(i,'mmddyyyy')#");
    </cfloop>
</cfoutput>

and here is my JavaScript function:

var dateArray = new Array();

function getDate(){
    for (var i=1; i < dateArray.length; i++){
        result = document.getElementById(dateArray[i][1])
        result.className = 'booked'
    }
}

So problem that I have at this point is that code gives me just End dates. Sometimes I have start and end time that happen on the same date but also sometimes we can have date range. Here is few examples:

Start           End
01/21/2015    01/21/2015
08:00 AM      12:30 PM
01/23/2015    01/24/2015
09:00 AM      03:00 PM
01/31/2015    02/05/2015
11:00 AM      10:00 AM

So my current code gives me just the end dates, I need to get all dates. If anyone see what I'm doing wrong in my code above please let me know. Thanks.

rrk
  • 15,677
  • 4
  • 29
  • 45
espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

1 Answers1

1

As Alex and charlietfl suggested, you array creation logic seems wrong. I assume you are trying to store the date range per user. In this case the, instead of array you should use structure for storing date arrays per user.

Apart from this I would suggest creating ColdFusion structure of date arrays first and then serializing it to JSON using ColdFusion's SerializeJSON() function. Like this:

<cfset dateStruct = structNew() />
<cfoutput query="qryOne">
    <cfset dateStruct[userID] = arrayNew(1) />
    <cfloop from="#PickDateTime#" to="#DropDateTime#" index="i" step="#CreateTimeSpan(1,0,0,0)#">
        <cfset arrayAppend(dateStruct[userID],"#dateformat(i,'mmddyyyy')#")/>
    </cfloop>
</cfoutput>

And then you can save the structure in JSON form in JavaScript variable using function like this:

<script>
     var jsonString =  '<cfoutput>#SerializeJSON(dateStruct)#</cfoutput>';
     var JSONObject = JSON.parse(jsonString);
</script>

Once you have JSON object in JavaScript, you can do whatever you need, using it. If you want to loop over JSON object read this: iterating through json object javascript

Community
  • 1
  • 1
Pankaj
  • 1,731
  • 1
  • 13
  • 15
  • I tried to implement JSONObject in function that I have above and I could not get to work. I used for loop to loop through JSON and then grab the key and assign the class name. How I can grab the value together with the key? – espresso_coffee Jan 22 '16 at 14:03
  • 1
    @user3023588 - Descriptions are ambiguous ;-) Instead of describing the code you used, please just post it. If possible, post a *stand alone example* others can run outside your environment. Generally, the easier it is for others to test the code, the more quickly you will get an answer. – Leigh Jan 22 '16 at 14:47
  • Still struggling to create working example here, I will try to fix that. Thanks for advice. – espresso_coffee Jan 22 '16 at 14:48
  • How I can expand number of elements in my dateStruct, on date that is already there to add more elements from the query? – espresso_coffee Jan 22 '16 at 16:39