Why would we need to do this ?
In my case , I needed to create an input date parameter but limit it to the next 7 days. Usually I would create an SQL datasource and write a query to return the dates this way but it dawned on me I didn't need to "waste" a database connection like this.Here the scripted datasource can come into play.
We can create a dataset that contains only dates ranging from now to 7 days from now using only Javascript contained entirely within the report. Lets get started !
Steps
- Click on Data Source and Add new. Select Scripted Data Source from the options listed.
- We now need to add a data source. Create a new data source , linked to the scripted one we created.
- Add the columns and set the data types needed.
- In the Open script for the Dataset we initialize our count with the below script
setGlobalVariable("day_range",1);
- In the fetch script for the Dataset add the below script to add a date + n days to our dataset.
var current_value = reportContext.getGlobalVariable("day_range"); if(current_value < 7){ current_value++ // Create a New Date + n days var date = new Date(); date.setDate(date.getDate() + current_value); // Set Our Column Values for this Row row["DateIndex"] = current_value ; row["DateOption"] = date ; reportContext.setGlobalVariable("day_range",current_value ); return true; } else{ return false; }
- Save & Preview.
See the example report here
Comments
Post a Comment