Welcome, Guest
Username: Password: Remember me

TOPIC: Prefill date picker from token

Prefill date picker from token 3 years 4 months ago #62166

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
I have 2 questions that asks for a date, using a date picker, and I have 2 relevant dates in my set of tokens that I would like to use for pre-filling these date pickers.

The date-time format in the tokens are: yyyy-mm-dd hh:mm:ss

I currently use a js in these questions to prevent that they choose a date earlier than today:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		var qID = QQ;
 
		$('#question'+qID+' .popupdate').datepicker( 'option', 'minDate', new Date() );
 
	}); 
</script>
"QQ" is replaced with the ID of the date question.

How can I instead pre-fill the date picker with the date information from the tokens?
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.

Prefill date picker from token 3 years 4 months ago #62188

  • DenisChenu
  • DenisChenu's Avatar
  • OFFLINE
  • Moderator Lime
  • Posts: 6435
  • Thank you received: 840
  • Karma: 249
Hello,

I think you can use jqueryui.com/demos/datepicker/#option-defaultDate

Something like this:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		var qID = QQ;
 
		$('#question'+qID+' .popupdate').datepicker( 'option', 'minDate', new Date() );
		$('#question'+qID+' .popupdate').datepicker( 'option', 'defaultDate', '{TOKEN:ATTRIBUTE_1}' );
	}); 
</script>
The hard think is to find the good dateFormat for attribute_1 ;).

Denis

Edit : move on the good sub-forum
Last Edit: 3 years 4 months ago by DenisChenu. Reason: See Edit
The administrator has disabled public write access.

Re: Prefill date picker from token 3 years 4 months ago #62329

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
Mmmmm, thanks for the input. I tried different variations of the date format, but I could not make it work..... Any idea why?
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.

Re: Prefill date picker from token 3 years 4 months ago #62333

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4270
  • Thank you received: 804
  • Karma: 368
I think you need to parse the date out of the token attribute value. Assumming that your token attribute formats are still yyyy-mm-dd hh:mm:ss, try the following. The script calls a function to set a default date for a date question with question ID 1111 to the value found in attribute_1 and question ID 2222 to the value found in attribute_2.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Get the token attribut values
		var attribute_1 = '{TOKEN:ATTRIBUTE_1}';
		var attribute_2 = '{TOKEN:ATTRIBUTE_2}';
 
		// Call the function with question IDs and token variables
		setDefaultdate(1111, attribute_1);
		setDefaultdate(2222, attribute_2);
 
		function setDefaultdate(qID, tokenValue) {
 
			// Break the token attribute dowmn into useable values
			var tmp = tokenValue.split(' ');
			var tmp2 = tmp[0].split('-');
			var tokenYear = tmp2[0];
			var tokenMonth = tmp2[1];
			var tokenday = tmp2[2];
 
			// Set a default date for the datepicker
			$('#question'+qID+' .popupdate').datepicker( 'option', 'defaultDate',  new Date(tokenYear, tokenMonth-1, tokenday) );
 
		}
	});
 
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 3 years 4 months ago by tpartner.
The administrator has disabled public write access.

Re: Prefill date picker from token 3 years 4 months ago #62344

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
Hmmmm, I still don't get anything to change when I open the date picker. The date formats are the same, and since your script had 2 tokens, I commented out one them, since it is going into one question, right?:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Get the token attribut values
		var attribute_1 = '{TOKEN:ATTRIBUTE_28}';
//		var attribute_2 = '{TOKEN:ATTRIBUTE_2}';
 
		// Call the function with question IDs and token variables
		setDefaultdate(873, attribute_1);
//		setDefaultdate(2222, attribute_2);
 
		function setDefaultdate(qID, tokenValue) {
 
			// Break the token attribute dowmn into useable values
			var tmp = tokenValue.split(' ');
			var tmp2 = tmp[0].split('-');
			var tokenYear = tmp2[0];
			var tokenMonth = tmp2[1];
			var tokenday = tmp2[2];
 
			// Set a default date for the datepicker
			$('#question'+qID+' .popupdate').datepicker( 'option', 'defaultDate',  new Date(tokenYear, tokenMonth-1, tokenday) );
 
		}
	});
 
</script>
I also tried to keep both, and put it in the question group, because I have another question with a different date, but that didn't make any difference...
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.

Re: Prefill date picker from token 3 years 4 months ago #62380

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4270
  • Thank you received: 804
  • Karma: 368
Do you have any JavaScript errors?

Try putting in a few alerts to see how the script is progressing:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Get the token attribut values
		var attribute_1 = '{TOKEN:ATTRIBUTE_28}';
 
		alert (attribute_1);
 
		// Call the function with question IDs and token variables
		setDefaultdate(873, attribute_1);
 
		function setDefaultdate(qID, tokenValue) {
 
			// Break the token attribute dowmn into useable values
			var tmp = tokenValue.split(' ');
			var tmp2 = tmp[0].split('-');
			var tokenYear = tmp2[0];
			var tokenMonth = tmp2[1];
			var tokenday = tmp2[2];
 
			alert (tokenYear'+-+'tokenMonth'+-+'tokenday);
 
			// Set a default date for the datepicker
			$('#question'+qID+' .popupdate').datepicker( 'option', 'defaultDate',  new Date(tokenYear, tokenMonth-1, tokenday) );
 
		}
	});
 
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.136 seconds
Donation Image