Solved

# Calculate difference in fields when user inputs

Posted on 2008-09-29
167 Views
Hi
I have 6 fields as follows on a ASP.net datagridview as follows.
The fields are for entering the number of days in two periods, say called  X & Y or to calculate the difference in the two period - the user needs both options.
1) Number of Days X
2) Number of Days Y
3) Start Date X
4) End Date X
5) Start Date Y
6) End Date Y

The user can enter "Number of X" & "Number of Y"

or choose a start and end date and have fields 1) & 2) calculated automatically.

Fields 3-6 give ajax popup calendars.

IF the user chooses a start and end date in fields 3) & 4), field 1) should be calculated as field 4)-field 3)
IF the user chooses a start and end date in fields 5) & 6), field 2) should be calculated as field 6)-field 5)

I'm stuck at how to trigger the calcs of field 1) & field 2)  when user makes change to fields 3-6.

The number of rows can change when user clicks on an "Add" button. This is an inmemory dataset, it is not linked to a database.

I presume there is some javascript I could add or something and it needs to be added as each row is added?  Or should it be done server side?

Thanks in advance for code to solve this problem.
0
Question by:rwallacej
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3

LVL 29

Expert Comment

ID: 22596139
This can be done on the client using event sinks.

Add "onchange=calc_1();" to field 4 and "onchange=calc_2();" to field 6

function \$(id) { return document.getElementById(id); } // shorthand method

function calc_1() {
var from_date = \$('START_DATE_X');
var thru_date = \$('END_DATE_X');
if (!from_date) { alert('Invalid "from" date'); return false; }
if (!thru_date) { alert('Invalid "thru" date'); return false; }
var num_days = thru_date - from_date; // this may need to be altered, depending upon where the value is
\$('Number_of_days_X').value = num_days;
return num_days;
}

function calc_2() {
var from_date = \$('START_DATE_Y');
var thru_date = \$('END_DATE_Y');
if (!from_date) { alert('Invalid "from" date'); return false; }
if (!thru_date) { alert('Invalid "thru" date'); return false; }
var num_days = thru_date - from_date; // this may need to be altered, depending upon where the value is
\$('Number_of_days_X').value = num_days;
return num_days;
}

You can further optimize the functions, combing them into one, if you wish.

Please note the functions are untested, and bugs may exist.
0

LVL 29

Expert Comment

ID: 22596149
Of course, the statement in "calc_2" shown as:

\$('Number_of_days_X').value

should be:

\$('Number_of_days_Y').value
0

Author Comment

ID: 22596176

Thanks for feedback

var from_date = \$('START_DATE_X');
(and subsequent like this)
works?

How does javascript know which row of the datagridview I am on to be able to get the correct text field?

I understand the return document.getElementById(id);  but I'd think that different rows on the gridview will have different IDs for the "Number of Days X" field

Thanks
0

LVL 29

Expert Comment

ID: 22596647
I do not know where the selected day value is with the calendar object; that is what you have to find out.

And are you saying that fields 1 and 2 are repeated in a grid? If that is the case, then yes, you will have to pull out the calendar object values for each row, and stuff the values for fields 1 and 2 in a similar manner.

0

Author Comment

ID: 22596668

all fields are repeated in grid - this is the problem with getting values by their id
0

LVL 29

Accepted Solution

ID: 22596974
You will have to trap the "onchange" event for the grid, and determine - by interrogating the event object - what element in the grid triggered the event. If it is eiter of the thru dates, then you can proceed, otherwise not.

You should be able to use the column of the cell to determine what triggered the event, since giving every cell a unique id attribute seems like overkill.

Again, these are untested, and bugs may exist. The logic seems correct; I apologize for my lack of .NET knowledge.

``````function resolve_event(ev) {
var evnt = { evt: null, tgt: null };
if (ev === undefined) { ev = window.event; }
evnt.evt = ev || window.event;
evnt.tgt = evnt.evt.target || evnt.evt.srcElement;
if (evnt.tgt.nodeType == 3) { // defeat Safari bug
evnt.tgt = evnt.tgt.parentNode;
}
return evnt;
}

function grid_change(e) {
//
// eobj	JSON object containing the event "target" and the parent event
// col	column of grid for end date "X" or "Y"
// row	row of grid for end date "X" or "Y"
// g	grid object
// c	column for # days (1 or 2)
//
eobj = resolve_event(e);
var col = Number(eobj.tgt.column); // syntax may be wrong for "column"
if (col == 4 || col == 6) {
var c = (col / 2) - 1;
var g = \$('YOUR_GRID_ID');
r = eobj.tgt.row; // syntax may be wrong for "row"
g.row[row].col[c].value = g.row[row].col[col].value - g.row[row].col[col - 1].value;
}
``````
0

Author Closing Comment

ID: 31501129
thank-you very much; this has helped me in getting solution
regards,
rwallacej
0

LVL 29

Expert Comment

ID: 22597840
No worries - glad to help.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…