Dates in Repeating Tables

M

Martyn Lawson

Hi,

I am using the code:

var dStartDate =
eventObj.Source.selectSingleNode("/ar:absenceRequest/ar:leaves/ar:leave/ar:startDate").text;
var dReturnDate =
eventObj.Source.selectSingleNode("/ar:absenceRequest/ar:leaves/ar:leave/ar:returnDate").text;

to find the values of 2 date pickers in a repeating table and then to
perform some calculations to find the difference. This is working fine until
i insert a subsequent row in the table. The calculations are still performed,
but they use the first row only.

How do i get the date values from the row that is firing the onAfterChange
event?

Thanks in advance,
Martyn...
 
C

Chris K

I've had similiar problems in the past, not with dates but I dont think that
impacts the solution.

Do you have the field that you are putting the result of the calculation in
defined in the same group as the start and end date? If not move it there
and try using an onAfterChange event on one of the date fields (probably the
end date) to trigger the calculation.

Chris
 
M

Martyn Lawson

Hi Chris,

Thanks for the response. Unfortunately, that is what i'm already doing:

var dReturnDate =
eventObj.Source.selectSingleNode("/ar:absenceRequest/ar:leaves/ar:leave/ar:returnDate").text;
if(dStartDate != "" && dReturnDate != "")
{
var dStartDate2 = new
Date(dStartDate.substring(0,4),dStartDate.substring(5,7)-1,dStartDate.substring(8,10));
var dReturnDate2 = new
Date(dReturnDate.substring(0,4),dReturnDate.substring(5,7)-1,dReturnDate.substring(8,10));
//Calculate weekdays
var iTotalDays = ((dReturnDate2 - dStartDate2)/86400000)+1;
var dtDate = new Date(dStartDate2);
var iWeekDays = 0;
while (iTotalDays > 0)
{
var flag = 0;
if (new Date(dtDate).getDay() % 6 != 0)
{
for (i = 0; i <= BankHolidays.length - 1; i++)
{
if (dtDate - BankHolidays >= 0 && dtDate - BankHolidays <= 7200000)
flag = 1;
}
if (flag == 0)
iWeekDays++;
}
var dateInMs = dtDate.getTime();
dateInMs += 86400000;
dtDate.setTime(dateInMs);
iTotalDays = iTotalDays - 1;
}
eventObj.Source.selectSingleNode("/ar:absenceRequest/ar:leaves/ar:leave/ar:hours").text = iWeekDays;
}
else
{
eventObj.Source.selectSingleNode("/ar:absenceRequest/ar:leaves/ar:leave/ar:hours").text = "0.0";
}

The code (from the returnDate: OnAfterChnage event) takes the two dates and
calculates how many weekdays lies between them (excluding bank holidays) and
writes it to the /ar:hours field.

As before, this works fine for the first row in the table, however, any
subsequent rows simply update the first rows hours using the first rows
dates. Do i need to loop through all rows in the table each time?

Any suggestions would be greatly appreciated,
Cheers,
Martyn...
 
M

Martyn Lawson

Hi,

Just in case anyone looks at this and wonders how i got round it - buy a
book!!

After reading the book, i found the way to do it. It comes down to the way
the field text is obtained. I managed to get it working using:

var dStartDate = eventObj.Site.text;
var dReturnDate = eventObj.Site.nextSibling.nextSibling.text;

where Site.text is the text of the field firing the event and the
nextSibling.nextSibling gets the text from the next node in the datasource.
Obviously, previousSibling.previousSibling can also be used. To move through
the nodes use multiple '.nextSibling.nextSibling' in the line. It appears
that two only move one node. If there is quite a distance between nodes,
might be better looping using .nextNode()

Hope someone finds this useful,
Cheers,
Martyn...

Martyn Lawson said:
Hi Chris,

Thanks for the response. Unfortunately, that is what i'm already doing:

var dReturnDate =
eventObj.Source.selectSingleNode("/ar:absenceRequest/ar:leaves/ar:leave/ar:returnDate").text;
if(dStartDate != "" && dReturnDate != "")
{
var dStartDate2 = new
Date(dStartDate.substring(0,4),dStartDate.substring(5,7)-1,dStartDate.substring(8,10));
var dReturnDate2 = new
Date(dReturnDate.substring(0,4),dReturnDate.substring(5,7)-1,dReturnDate.substring(8,10));
//Calculate weekdays
var iTotalDays = ((dReturnDate2 - dStartDate2)/86400000)+1;
var dtDate = new Date(dStartDate2);
var iWeekDays = 0;
while (iTotalDays > 0)
{
var flag = 0;
if (new Date(dtDate).getDay() % 6 != 0)
{
for (i = 0; i <= BankHolidays.length - 1; i++)
{
if (dtDate - BankHolidays >= 0 && dtDate - BankHolidays <= 7200000)
flag = 1;
}
if (flag == 0)
iWeekDays++;
}
var dateInMs = dtDate.getTime();
dateInMs += 86400000;
dtDate.setTime(dateInMs);
iTotalDays = iTotalDays - 1;
}
eventObj.Source.selectSingleNode("/ar:absenceRequest/ar:leaves/ar:leave/ar:hours").text = iWeekDays;
}
else
{
eventObj.Source.selectSingleNode("/ar:absenceRequest/ar:leaves/ar:leave/ar:hours").text = "0.0";
}

The code (from the returnDate: OnAfterChnage event) takes the two dates and
calculates how many weekdays lies between them (excluding bank holidays) and
writes it to the /ar:hours field.

As before, this works fine for the first row in the table, however, any
subsequent rows simply update the first rows hours using the first rows
dates. Do i need to loop through all rows in the table each time?

Any suggestions would be greatly appreciated,
Cheers,
Martyn...



Chris K said:
I've had similiar problems in the past, not with dates but I dont think that
impacts the solution.

Do you have the field that you are putting the result of the calculation in
defined in the same group as the start and end date? If not move it there
and try using an onAfterChange event on one of the date fields (probably the
end date) to trigger the calculation.

Chris
 

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