[Javascript] Calculating large number of different values
Triche Osborne
wdlists at triche-osborne.com
Sat Aug 6 15:47:29 CDT 2005
Fred Newtz wrote:
>
> I started trying to name my textboxes but I keep feeling that I need to make
> my looping structure before I make the names for the text boxes. What
> should come first? The field names/ids or the looping structure? What is a
> good way to plan for the names use and how you are looping through them?
>
Interesting question. It's one of those things that is easier to do than
to explain because it's not a process I go through explicitly and there
isn't just one way to do it.
For me, the naming comes first. I generally consider 3 things: input
data, output requirements and language resources and constraints.
Input Data:
(1) Attendance broken down by day of week per meal per age group
(2) Food group (servings per child) broken down by meal per age group
Output Requirements:
XHTML:
(1) Attendance => same as input
(2) Food Group => input * Attendance
Display Heirarchy:
------------------
Age Group => Meal => Food Group
PHP: ?
Language Resources and Constraints:
XHTML
-----
Resource: NAME attribute
Constraint: If using NAME['arrayIndex'] format, must be accessed
through DOM with getElementById or getElementsByTagName; that
is, no direct access through form array.
Resource: ID attribute
Constraint: Cannot use array format. Invisible to PHP.
Resource: CLASS attribute
Constraint: Cannot use array format. Invisible to PHP.
JavaScript
----------
Resource: instance property
Constraint: Arrays pass by reference. Invisible to PHP.
If PHP has to process edited textbox input in some way, I'm limited to
using the NAME attribute for any data that must be processed by both
languages, and I may need to use a single form so that the submit button
processes everything. If not, then I'm free to use other attributes and
break up the output into a form per age group. Since I don't know
whether PHP has to get this information or not, I'm going to stick with
one form and the NAME attribute.
The first thing I'd try to do is to get a good match between my array
names and textboxes, while at the same time trying to simplify the
arrays as much as possible. The arrays can be looked at in different ways:
PlanPart[day][meal][agegroup]
PlanPart[day][agegroup][meal]
In some cases, one may be more helpful than the other. In this case, for
instance, you're using the first way, but your output hierarchy matches
the second. I'd try to make them match.
Suppose the first entry in your PlanPart array looked something like
this: PlanPart["Monday"]['age0'] = [30, 30, 30, 30, 20, 15]
That means that the first input box in the attendance column of your
form could be named "age00". Since the array name and the day of the
week are external to the form, this name gives you simple access to the
secondary and tertiary levels of the PlanPart array.
In the same way, modifying the food group arrays can do the same thing.
Let's say the milk array looks something like this . . .
var milk = new Array([3, 2, 2, 1, 1], [2, 2, 2, 1, 1], [2, 2, 2, 1, 1])
. . . where the primary level represents the age groups and the
secondary level the meal. You could then name your first row, first
column input box "milk00" (or "milk[0][0]").
In the places where the meal consists of more than one food group, I
think I'd eliminate all but the primary attendance box. After all, if
the child is present for the meal, he is present for all of the food
groups. This eliminates complications with naming the other boxes.
At this point, you can modify the original function to deal with the
new levels OR (and I just thought of this as I was thinking on this),
you could rewrite it to grab all of the INPUT tags, then flip through
the inputs one by one, filling them out as you go. If you choose the
latter, you might want to rearrange the column order to make it easy to
"save" the attendance figure where it need to apply to multiple food
groups within a single meal.
Triche
More information about the Javascript
mailing list