Aug 2, 2011 7:28 AM
170 Posts

How to set field date/time value using Clientside Js

  • Category: Dojo and Client Side JavaScript
  • Platform: All
  • Release: 8.5.2
  • Role: Developer
  • Tags:
  • Replies: 16
 I have a date/time field that I want to set the value of when the user clicks the field.
 
I can do this with onclick Js code but how can I send the information to the backend document? 
 
because If I have a button that does a full or partial refresh the information inside the field is lost. 
 
 
Aug 2, 2011 9:58 AM
272 Posts
Re: set field value using Clientside Js
Hi Fredrik,
 
if your field has a data binding, your values will not get lost, if you refresh the input element or an element containing the input element.
 
Sven
 
Aug 2, 2011 2:35 PM
170 Posts
Re: set field value using Clientside Js
 But it gets lost. 
 
Perhaps the code that sets the field is wrong? 
 
var f=XSP.getElementById("#{id:TimeField}");

if(f.value==""){
var d=new Date()
var h=d.getHours();
if(new String(h).length==1){
h="0"+new String(h)
}
f.value=h+".00"

 
If I set the value on another field type it works but not date or time fields. 
Aug 2, 2011 3:28 PM
272 Posts
Re: How to set field date/time value using Clientside Js
I am a little bit confused... You want to add "12.00" to a date/time field? Normally you would fill in a field with a time value separated with a colon. I think this is a syntax problem, and Domino is unable to interpret the give value as a date/time.
 
Is client side field validation enabled, or is a handler for error messages defined?
 
Aug 3, 2011 3:20 AM
170 Posts
Re: How to set field date/time value using Clientside Js
 I have validation on this field. I'm confused also.
 
I have looked at what is appening when a field is set using the picker  
 
then the following attribute is set 
aria-valuenow="Thu Jan 01 1970 07:45:00 GMT+0100" 
 
but what I can see using Firefox and firebug the value is not set but the value is showing. 
 
I have tried to set this attribute and the value but no luck. 
the value is still lost when refresh is done. 
Aug 3, 2011 5:14 AM
272 Posts
Re: How to set field date/time value using Clientside Js
Is disableClientSideValidation="true"? If so, please set to "false" and retry.
 
Aug 3, 2011 7:40 AM
170 Posts
Re: How to set field date/time value using Clientside Js
 It was blank but I tried to setting it to false but no change.
 
If I write some time in the field the refesh works and the time is still there. 
But If I set the value the value is lost when the refresh is done. 
Aug 3, 2011 8:27 AM
272 Posts
Re: How to set field date/time value using Clientside Js
Can you please post the code of your field?
Aug 3, 2011 8:40 AM
170 Posts
Re: How to set field date/time value using Clientside Js
This is the source for that field nothing special 
 
 <xp:inputText
  id="TimeField"
  value="#{doc.TimeField}"
  styleClass="dateField"
  disableClientSideValidation="false">
  <xp:dateTimeHelper></xp:dateTimeHelper>

  <xp:this.converter>
  <xp:convertDateTime
  type="time"
  timeStyle="short">
  </xp:convertDateTime>
  </xp:this.converter>
 
  <xp:eventHandler
  event="onclick"
  submit="false">
  <xp:this.script><![CDATA[var f=XSP.getElementById("#{id:TimeField}");

if(f.value==""){
var d=new Date()
var h=d.getHours();
if(new String(h).length==1){
h="0"+new String(h)
}
f.value=h+".00"

} ]]></xp:this.script>
  </xp:eventHandler></xp:inputText>
 
and then I have a button that sets a scoped variable
 
<xp:button id="button3" styleClass="buttons">
  <xp:this.value><![CDATA[#{javascript:var Status=viewScope.Status;
if(Status=="Planerad"){
return "Omplanera"
} else {
return "Planera"
}}]]></xp:this.value>
  <xp:eventHandler
  event="onclick"
  submit="true"
  refreshMode="complete">
  <xp:this.action><![CDATA[#{javascript:viewScope.Status="Planerad"
}]]></xp:this.action>

  <xp:this.script><![CDATA[var status=document.getElementById("status");
if(status){
status.value="Planerad"
}]]></xp:this.script>
   </xp:eventHandler></xp:button> 
Aug 4, 2011 8:15 AM
272 Posts
Re: How to set field date/time value using Clientside Js
Hi again,
 
this is a very interesting problem. Here is my solution:
 
1. Change your "f.value" from dotted to a colon separated value
f.value=h+":00"
 
2. Fill the "aria-valuenow"-attribute with a dummy value like this:
dojo.attr("#{id:TimeField}", "aria-valuenow", "Thu Jan 01 1970 00:00:00 GMT+0000");
 
The value is stored with todays date in the backend document.
 
Sven
 
Aug 5, 2011 4:50 AM
170 Posts
Re: How to set field date/time value using Clientside Js
 I have tried what you are suggesting but If I load the page click inside the field and then directly clicks the button 
the time is lost. but If I move inside the field with the arrow keys. and then click the button then it works. 
 
This is really strange. 
 
I'm using 8.5.2 FP1 on the server 
Aug 5, 2011 4:14 PM
272 Posts
Re: How to set field date/time value using Clientside Js
If you change the value, a onChange-Event is triggered, and it seems that the XSP-Object (or Dojo) does some magic things with the value of the time field...
 
But just a quick question: Why not do a partial refresh on the field and set the field value via SSJS?
Aug 6, 2011 10:07 AM
170 Posts
Re: How to set field date/time value using Clientside Js
 The partial refresh is done later if the user changes a status and It's then 
the value is lost. 
Aug 7, 2011 4:36 PM
272 Posts
Re: How to set field date/time value using Clientside Js
What if you add an SSJS to the onclick event of the field where you set the value (partial refresh of the field)?
On the Client side, you could check if the field is empty, if not "return false" to stop the event.
 
If a user then changes the status, the value will not get lost...
Aug 8, 2011 4:57 AM
170 Posts
Re: How to set field date/time value using Clientside Js
 I'll try that. Or I will skip the idea.
And make the user manually mark the time they want. 
 
Thanks for the help 
 
/Fredrik 
Aug 10, 2011 4:30 AM
14 Posts
Re: How to set field date/time value using Clientside Js
Try to set the value to "T12:00" (separator may vary).
Aug 10, 2011 5:09 AM
272 Posts
Re: How to set field date/time value using Clientside Js
Hi Frantisek,
 
this doesn't work, it is not a valid date/time value.
 
Sven