Two Jscript Problems: Can't Find Solution

M

mg

I'm an InfoPath newbie and the following syntax problems might be easy for
some of you to fix. I've searched in Help, online resources, and even
purchased "Beginning InfoPath 2003" and I still can't find a solution.

Created a jscript function to manipulate date fields (to call from an
OnAfterChange event). Very simple at this point. Problems:

1. This statement doesn't seem to correctly reference the weekDay field:
var weekDay1 = XDocument.DOM.selectSingleNode("//my:day1/my:weekDay");
The cell bound to weekDay1 remains blank even with something simple like
weekDay1 = "test". The field is inside a Group called "day1" that seems to
be properly bound to the cell. If I try weekDay1.text = "test" I get a
"null or not an object error". Is the problem here: ("//my:day1/my:weekDay")
?


2. I get a "type mismatch" error when I try to set one field to the value of
another--even though I tried setting both to date or both to text:
var weekStart = XDocument.DOM.selectSingleNode("//my:weekStartDate");
var date3 = XDocument.DOM.selectSingleNode("//my:date3");
date3.text = weekStart


I'm getting frustrated. Any help will be appreciated.

mg
 
O

olivier

I'm not so good for jscript but I can say something that others will correct
if I'm wrong :

if you want to set a value, the syntax is
weekDay1 = XDocument.DOM.selectSingleNode("//my:day1/my:weekDay");
weekDay1.setTypedValue="test"; (I suppose that you defined a string for your
weejday field)

selectSingleNode just create Weekday as a node, after querying the schema.
It doesn't modify it.

The problem is identical in your second example where you are try to set a
string with a node.

Am I right ?
 
O

olivier

Sorry, it's nodeTypedValue, setTypedValue is a function that nyou can also
use coming with the Infopath SDK

weekDay1 = XDocument.DOM.selectSingleNode("//my:day1/my:weekDay");
weekDay1.nodeTypedValue="test";
 
D

danslemur

I'm a bit of a newbie myself, but I think I might see the problem.

Regarding #1, I would try doing
var weekDay1 =
XDocument.DOM.selectSingleNode("/my:myFields/my:day1/my:weekDay");

By default, Infopath groups all fields underneath a master group called
"myFields", so you must include that when using selectSingleNode.

With regards to #2, the same advice applies about including the master
group in the initial node selection and variable declaration. If that
doesn't fix it, I would say that setting the text property of one node
to the value of another node would be the type mismatch. Assuming
weekStart and date3 are properly initialized, you would want to do
something like

date3.text = weekStart.text;


Nick Bennett


I'm an InfoPath newbie and the following syntax problems might be easy for
some of you to fix. I've searched in Help, online resources, and even
 
M

mg

Nick and Oliver,

Thanks very much for your suggestions. I learned some new things from you.
Also, just seeing some replies when I got home gave me new determination to
solve these issues! Both problems are resolved so now I can take a few more
baby steps. <g>

My "populateDateColumn" function will now populate the weekDay field in
seven groups using a date picker field ("weekStartDate") as the initial
source. (I found the parseDate function somewhere on the Web, so thanks to
that person also.)

So far I just increment the day (it generates invalid days beyond the end of
a month). With a little more work, the function will properly calculate
month and year and eventually calculate leap years as well. I'm sure someone
has already done this, but this project is helping me to learn. (Even so,
please feel free to share jscript or vbscript examples IF you can find any!)

mg

p.s.
Both of these node references worked equally well:
"...selectSingleNode("//my:day1/my:weekDay")" or
"...selectSingleNode("/my:timesheet/my:day1/my:weekDay")"

== Code sample ==============================================

function populateDateColumn()
{
var wsNode = XDocument.DOM.selectSingleNode("//my:weekStartDate");
var d1Node = XDocument.DOM.selectSingleNode("//my:day1/my:weekDay");
var d2Node = XDocument.DOM.selectSingleNode("//my:day2/my:weekDay");
var d3Node = XDocument.DOM.selectSingleNode("//my:day3/my:weekDay");
var d4Node = XDocument.DOM.selectSingleNode("//my:day4/my:weekDay");
var d5Node = XDocument.DOM.selectSingleNode("//my:day5/my:weekDay");
var d6Node = XDocument.DOM.selectSingleNode("//my:day6/my:weekDay");
var d7Node = XDocument.DOM.selectSingleNode("//my:day7/my:weekDay");

var ws = parseDate(wsNode.text);

d1Node.text = (ws.getMonth()) + "/" + (ws.getDate()) + "/" +
ws.getFullYear();
d2Node.text = (ws.getMonth()) + "/" + (ws.getDate() + 1) + "/" +
ws.getFullYear();
d3Node.text = (ws.getMonth()) + "/" + (ws.getDate() + 2) + "/" +
ws.getFullYear();
d4Node.text = (ws.getMonth()) + "/" + (ws.getDate() + 3) + "/" +
ws.getFullYear();
d5Node.text = (ws.getMonth()) + "/" + (ws.getDate() + 4) + "/" +
ws.getFullYear();
d6Node.text = (ws.getMonth()) + "/" + (ws.getDate() + 5) + "/" +
ws.getFullYear();
d7Node.text = (ws.getMonth()) + "/" + (ws.getDate() + 6) + "/" +
ws.getFullYear();
}

function parseDate(odate)
{ // Dates are stored in ISO8601 format "yyyy-mm-dd".
if (odate.match(/(\d\d\d\d)-(\d\d)-(\d\d)/))
return new Date(RegExp.$1, RegExp.$2, RegExp.$3);

return null;
}
 
M

Mike Sharp

mg was asking for some javascript examples...

This is how I'd probably handle it, in an HTML test harness so folks without
InfoPath can see it work:

<html>
<head>
<title>Test</title>
<script language="javascript">

Date.prototype.toShortDateString = function()
{
var YYYY,MM,M,DD,D;
YYYY = this.getFullYear() + "";
MM = (M=this.getMonth()+1)<10?('0'+M):M;
DD = (D=this.getDate())<10?('0'+D):D;
return MM + "/" + DD + "/" + YYYY ;
}

function parseISODate(sDate)
{
if (sDate.match(/(\d\d\d\d)-(\d\d)-(\d\d)/))
{
return Date.parse(RegExp.$2 + "/" + RegExp.$3 + "/" + RegExp.$1);
}
else
{
return Date.parse(sDate)
}
}

function AddDays(oldDate, daysToAdd)
{
var t1, t2, newDate;
var MinMilli = 1000 * 60;
var HrMilli = MinMilli * 60;
var DyMilli = HrMilli * 24;
t1 = parseISODate(oldDate);
t2 = t1 + Math.round(daysToAdd * DyMilli)
newDate = new Date(t2)
return(newDate.toShortDateString());
}

function init()
{
var dateString1 = "2005-01-25T12:31:48.123Z";
// var dateString1 = "2005-01-25"
// var dateString1 = "01-25-2005";
// var dateString1 = "2005/01/25";

var sHtml = AddDays(dateString1, 10)
target.innerText = sHtml
}
window.onload = init;
</script>
<body>
<h2 id="target"></h2>
</body>
</html>


There is an intrinsic Date object with a static parse method. This,
unfortunately, doesn't accept ISO format dates. However, this handy line of
code:

Object.prototype.parseISODate = function(sDate)

extends the instrinsic Date object with a method that handles both standard
and ISO format dates. Actually, you'd want to make that function a bit more
rigorous, so that it would handle full ISO format dates and return just what
it finds. So if it found

The other odd bit near the top:

Date.prototype.toShortDateString = function()

is one way of extending the date object to serialize into a string format
that it doesn't support. I have a variety of these little methods I use.
For example, I have one that produces an ISO 8601 format that will validate
in a W3C XML Schema. This one works on a Date object instance. The other
one is a prototype of the Object object, which the Date object "inherits".

Actually, if we're going to do a regex, we should probably make it bit more
robust, and extend these so they can handle ISO short and long dates. The
regex below will ignore dates like 2004-35-88 (though obviously it doesn't
check for leap years, and whether April has 31 days...That checking is left
to the Date object which will validate the date string anyway.

Here's the same example extended a bit more (no guarantees on robustness--I
only did a little testing ;^)

<html>
<head>
<title>Test</title>
<script language="javascript">

Date.prototype.toShortDateString = function()
{
var YYYY,MM,M,DD,D;
YYYY = this.getFullYear() + "";
MM = (M=this.getMonth()+1)<10?('0'+M):M;
DD = (D=this.getDate())<10?('0'+D):D;
return MM + "/" + DD + "/" + YYYY ;
}

Date.prototype.toDateTimeString = function()
{
var YYYY,MM,M,DD,D,hh,h,mm,m,ss,s,i,ii,iii;
YYYY = this.getFullYear() + "";
MM = (M=this.getMonth()+1)<10?('0'+M):M;
DD = (D=this.getDate())<10?('0'+D):D;
h=this.getHours();
hh = h<10?('0'+h):h;
mm=(m=this.getMinutes())<10?('0'+m):m;
ss=(s=this.getSeconds())<10?('0'+s):s;
iii=(ii=((i=this.getMilliseconds())<10?('0'+i):i))<100?('0'+ii):i;

return YYYY + "-" + MM + "-" + DD + " " + hh + ":" + mm + ":" + ss + "."
+ iii
}


Object.prototype.parseISODate = function(sDate)
{
if
(sDate.match(/(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T(0[0-9]|1[0-9
]|2[0-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9]).(\d{3})(Z)/))
{
var t = Date.parse(RegExp.$2 + "/" + RegExp.$3 + "/" + RegExp.$1 + "
" + RegExp.$4 + ":" + RegExp.$5 + ":" + RegExp.$6);
if (RegExp.$7 != "")
t += parseInt(RegExp.$7);
return t
}
else
{
if (sDate.match(/(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])/))
return Date.parse(RegExp.$2 + "/" + RegExp.$3 + "/" +
RegExp.$1);
else
return Date.parse(sDate)
}
}

function AddDays(oldDate, daysToAdd)
{
var t1, t2, newDate, tempDate;
tempDate = new Date();
var MinMilli = 1000 * 60;
var HrMilli = MinMilli * 60;
var DyMilli = HrMilli * 24;
t1 = Date.parseISODate(oldDate);
t2 = t1 + Math.round(daysToAdd * DyMilli)
newDate = new Date(t2)
// return(newDate.toShortDateString());
return(newDate.toDateTimeString());
}

function init()
{
var dateString1 = "2005-01-25T12:31:48.123Z";
// var dateString1 = "2005-01-25"
// var dateString1 = "01-25-2005";
// var dateString1 = "2005/01/25";

var sHtml = AddDays(dateString1, 7)
target.innerText = sHtml
}
window.onload = init;
</script>
<body>
<h2 id="target"></h2>
</body>
</html>




Regards,
Mike Sharp
 
M

Mike Sharp

Well, I had a reply, but it seems to have been lost. Apologies if this
turns out to be a double post.

mg was asking for some javascript examples...

This is how I'd probably handle it, in an HTML test harness so folks without
InfoPath can see it work:

<html>
<head>
<title>Test</title>
<script language="javascript">

Date.prototype.toShortDateString = function()
{
var YYYY,MM,M,DD,D;
YYYY = this.getFullYear() + "";
MM = (M=this.getMonth()+1)<10?('0'+M):M;
DD = (D=this.getDate())<10?('0'+D):D;
return MM + "/" + DD + "/" + YYYY ;
}

function parseISODate(sDate)
{
if (sDate.match(/(\d\d\d\d)-(\d\d)-(\d\d)/))
{
return Date.parse(RegExp.$2 + "/" + RegExp.$3 + "/" + RegExp.$1);
}
else
{
return Date.parse(sDate)
}
}

function AddDays(oldDate, daysToAdd)
{
var t1, t2, newDate;
var MinMilli = 1000 * 60;
var HrMilli = MinMilli * 60;
var DyMilli = HrMilli * 24;
t1 = parseISODate(oldDate);
t2 = t1 + Math.round(daysToAdd * DyMilli)
newDate = new Date(t2)
return(newDate.toShortDateString());
}

function init()
{
var dateString1 = "2005-01-25T12:31:48.123Z";
// var dateString1 = "2005-01-25"
// var dateString1 = "01-25-2005";
// var dateString1 = "2005/01/25";

var sHtml = AddDays(dateString1, 10)
target.innerText = sHtml
}
window.onload = init;
</script>
<body>
<h2 id="target"></h2>
</body>
</html>


There is an intrinsic Date object with a static parse method. This,
unfortunately, doesn't accept ISO format dates. However, this handy line of
code:

Object.prototype.parseISODate = function(sDate)

extends the instrinsic Date object with a method that handles both standard
and ISO format dates. Actually, you'd want to make that function a bit more
rigorous, so that it would handle full ISO format dates and return just what
it finds. So if it found

The other odd bit near the top:

Date.prototype.toShortDateString = function()

is one way of extending the date object to serialize into a string format
that it doesn't support. I have a variety of these little methods I use.
For example, I have one that produces an ISO 8601 format that will validate
in a W3C XML Schema. This one works on a Date object instance. The other
one is a prototype of the Object object, which the Date object "inherits".

Actually, if we're going to do a regex, we should probably make it bit more
robust, and extend these so they can handle ISO short and long dates. The
regex below will also ignore dates like 2004-35-88 (though obviously it
doesn't
check for leap years, and whether April has 31 days...That checking is left
to the Date object which will validate the date string anyway.

Here's the same example extended a bit more (no guarantees on robustness--I
only did a little testing ;^)

<html>
<head>
<title>Test</title>
<script language="javascript">

Date.prototype.toShortDateString = function()
{
var YYYY,MM,M,DD,D;
YYYY = this.getFullYear() + "";
MM = (M=this.getMonth()+1)<10?('0'+M):M;
DD = (D=this.getDate())<10?('0'+D):D;
return MM + "/" + DD + "/" + YYYY ;
}

Date.prototype.toDateTimeString = function()
{
var YYYY,MM,M,DD,D,hh,h,mm,m,ss,s,i,ii,iii;
YYYY = this.getFullYear() + "";
MM = (M=this.getMonth()+1)<10?('0'+M):M;
DD = (D=this.getDate())<10?('0'+D):D;
h=this.getHours();
hh = h<10?('0'+h):h;
mm=(m=this.getMinutes())<10?('0'+m):m;
ss=(s=this.getSeconds())<10?('0'+s):s;
iii=(ii=((i=this.getMilliseconds())<10?('0'+i):i))<100?('0'+ii):i;

return YYYY + "-" + MM + "-" + DD + " " + hh + ":" + mm + ":" + ss + "."
+ iii
}


Object.prototype.parseISODate = function(sDate)
{
if
(sDate.match(/(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T(0[0-9]|1[0-9
]|2[0-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9]).(\d{3})(Z)/))
{
var t = Date.parse(RegExp.$2 + "/" + RegExp.$3 + "/" + RegExp.$1 + "
" + RegExp.$4 + ":" + RegExp.$5 + ":" + RegExp.$6);
if (RegExp.$7 != "")
t += parseInt(RegExp.$7);
return t
}
else
{
if (sDate.match(/(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])/))
return Date.parse(RegExp.$2 + "/" + RegExp.$3 + "/" +
RegExp.$1);
else
return Date.parse(sDate)
}
}

function AddDays(oldDate, daysToAdd)
{
var t1, t2, newDate, tempDate;
tempDate = new Date();
var MinMilli = 1000 * 60;
var HrMilli = MinMilli * 60;
var DyMilli = HrMilli * 24;
t1 = Date.parseISODate(oldDate);
t2 = t1 + Math.round(daysToAdd * DyMilli)
newDate = new Date(t2)
// return(newDate.toShortDateString());
return(newDate.toDateTimeString());
}

function init()
{
var dateString1 = "2005-01-25T12:31:48.123Z";
// var dateString1 = "2005-01-25"
// var dateString1 = "01-25-2005";
// var dateString1 = "2005/01/25";

var sHtml = AddDays(dateString1, 7)
target.innerText = sHtml
}
window.onload = init;
</script>
<body>
<h2 id="target"></h2>
</body>
</html>




Regards,
Mike Sharp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top