× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Mike

findIndexByKeyValue is just the same as the (not very efficient) example in my last email because you have to loop through the elements to find the one you want. OK for small JSONs but not for anything bigger.

The more efficient way is to store your HourTypeCodes array as a property of the <option> element while you build the first drop down box. You then don't have to go to the trouble of saving the selected value in an input field. Like this:

// Get the request types for the drop down
$(".RequestTypeDdl").live("focus", function() {
var item1 = $("<option>(Select)</option>");
var field = $(this);
var savedParent = 0;
field.html('');
field.append(item1);
$.each(jsonRequestType, function(i, item) {
var optHtml="";
if ((savedParent != item.ParentId) && (savedParent != 0))
optHtml += "</optgroup>";
if (item.ChildRequired)
optHtml += "<optgroup label='" + item.Title + "'>";
else {
optHtml += "<option value='" + item.Id + "'";
if ($(field).prev("input").val() == item.Id)
optHtml += " selected";
optHtml += ">" + item.Title + "</option>";
};
var opt=$(optHtml);
opt[0]._hourtypecodes=item.HourTypeCodes;
field.append(opt)
});
});


Then it is very simple

$.each(this.options[this.selectedIndex]._hourtypecodes, function(i, item) {



-----Original Message-----
From: web400-bounces@xxxxxxxxxxxx [mailto:web400-bounces@xxxxxxxxxxxx] On Behalf Of Mike Wills
Sent: 15 December 2010 21:17
To: Web Enabling the AS400 / iSeries
Subject: Re: [WEB400] Navigating JSON sub-arrays dynamically

I figured it out finally! I found this:
http://inderpreetsingh.com/2010/10/14/javascriptjson-find-index-in-an-array-of-objects/

<http://inderpreetsingh.com/2010/10/14/javascriptjson-find-index-in-an-array-of-objects/>The
problem with Kevin's solution is that the IDs will not be sequential or they
can change.

From there these are the two lines that work:

var i = findIndexByKeyValue(jsonRequestType, "Id", key);
$.each(jsonRequestType[i].HourTypeCodes, function(i, item) {

--
Mike Wills
http://mikewills.me


On Wed, Dec 15, 2010 at 2:53 PM, Kevin Turner
<kevin.turner@xxxxxxxxxxxxxxx>wrote:

Mike

As I mentioned before, since there is no way to directly reference object
that contains the HourTypeCodes you have to make the assumption that the
value form your first drop down box is always going to be relevant to its
array index.

In other words, you would have to replace this:

$.each(jsonRequestType.Id[key].HourTypeCodes, function(i, item) {

With this

$.each(jsonRequestType[parseInt(key)-1].HourTypeCodes, function(i, item) {



Rgds
Kevin

-----Original Message-----
From: web400-bounces@xxxxxxxxxxxx [mailto:web400-bounces@xxxxxxxxxxxx] On
Behalf Of Mike Wills
Sent: 15 December 2010 20:20
To: Web Enabling the AS400 / iSeries
Subject: Re: [WEB400] Navigating JSON sub-arrays dynamically

People have been talking about jQuery/javascript frameworks on here, I
figured I would ask. This does connect to the iSeries being used as a DB so
it is sort of related. Here is the relavant parts:

$(document).ready(function() {
var jsonRequestType;
var jsonRequestTypeRecieved = false;

$.getJSON('/WebService/GetRequestInformation', function(json) {
jsonRequestType = json;
jsonRequestTypeRecieved = true;
});

// Get the request types for the drop down
$(".RequestTypeDdl").live("focus", function() {
var items = "<option>(Select)</option>";
var field = $(this);
var savedParent = 0;
$.each(jsonRequestType, function(i, item) {
if ((savedParent != item.ParentId) && (savedParent != 0))
items += "</optgroup>";
if (item.ChildRequired)
items += "<optgroup label='" + item.Title + "'>";
else {
items += "<option value='" + item.Id + "'";
if ($(field).prev("input").val() == item.Id)
items += " selected";
items += ">" + item.Title + "</option>";
};
});
$(this).html(items);
});

// Stored the selected value so it isn't lost
$(".RequestTypeDdl").live("change", function() {
// Stored the selected value so it isn't lost
$(this).prev("input").val($(this).val());

// Build the second DDL
var key = $(this).prev("input").val();
var tsddl =
$(this).parent("td").next("td").children(".TimesheetCodeDdl");
var items = "<option>(Select)</option>";
var savedParent = 0;
$.each(jsonRequestType.Id[key].HourTypeCodes, function(i, item) {
if ((savedParent != item.ParentId) && (savedParent != 0))
items += "</optgroup>";
if (item.ChildRequired)
items += "<optgroup label='" + item.Title + "'>";
else {
items += "<option value='" + item.Id + "'";
if ($(tsddl).prev("input").val() == item.Id)
items += " selected";
items += ">" + item.Title + "</option>";
};
});
$(tsddl).html(items);
});
});

Here is a valid section of my JSON:

[{
"Id": 1,
"Title": "Vacation",
"HourTypeCodes": [
{
"Id": "05",
"Title": "VAC POLICE/FIRE"
},
{
"Id": "04",
"Title": "VACATION"
},
{
"Id": "62",
"Title": "VACATION HOURS PURCHASED"
},
{
"Id": "60",
"Title": "VACATION SELL BACK"
}
]
},
{
"Id": 2,
"Title": "Holiday",
"HourTypeCodes": [
{
"Id": "08",
"Title": "HOLIDAY"
}
]
},
{
"Id": 3,
"Title": "Floating Holiday",
"HourTypeCodes": [
{
"Id": "09",
"Title": "FLOATING HOLIDAY"
}
]
}]

--
Mike Wills
http://mikewills.me


On Wed, Dec 15, 2010 at 2:01 PM, Kevin Turner
<kevin.turner@xxxxxxxxxxxxxxx>wrote:

Strange forum for a JSON/jQuery question - but anyhow, the JSON you have
posted is syntactically incorrect. Is that just a typo?

Can you post all the significant code please, rather than a snippet, and
explain a bit more fully what you are trying to do.

Thanks

-----Original Message-----
From: web400-bounces@xxxxxxxxxxxx [mailto:web400-bounces@xxxxxxxxxxxx]
On
Behalf Of Mike Wills
Sent: 15 December 2010 19:30
To: Web Enabling the AS400 / iSeries
Subject: [WEB400] Navigating JSON sub-arrays dynamically

I am using JSON and jQuery to dynamically populate a parent/child
dropdown
lists. I have it working to fill the parent drop down and have the child
drop down filling with the same parent data. How do I loop through the
sub
array of the selected value from the parent?

My "each" line (key is the value from my parent DDL:

var key = $(this).prev("input").val();
$.each(jsondata.Id[key].HourTypeCodes, function(i, item) {

My JSON

[{"Id":1,"Title":"Vacation","HourTypeCodes":[{"Id":"05","Title":"VAC

POLICE/FIRE",},{"Id":"04","Title":"VACATION"},{"Id":"62","Title":"VACATION
HOURS PURCHASED"},{"Id":"60","Title":"VACATION SELL BACK"}]},


{"Id":2,"Title":"Holiday""HourTypeCodes":[{"Id":"08","Title":"HOLIDAY"}]},
{"Id":3,"Title":"Floating
Holiday","HourTypeCodes":[{"Id":"09","Title":"FLOATING HOLIDAY"]}]

What am I doing wrong?

--
Mike Wills
http://mikewills.me
--
This is the Web Enabling the AS400 / iSeries (WEB400) mailing list
To post a message email: WEB400@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/web400
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/web400.


NOTICE: The information in this electronic mail transmission is intended
by
CoralTree Systems Ltd for the use of the named individuals or entity to
which it is directed and may contain information that is privileged or
otherwise confidential. If you have received this electronic mail
transmission in error, please delete it from your system without copying
or
forwarding it, and notify the sender of the error by reply email or by
telephone, so that the sender's address records can be corrected.





--------------------------------------------------------------------------------


CoralTree Systems Limited
25 Barnes Wallis Road
Segensworth East, Fareham
PO15 5TT

Company Registration Number 5021022.
Registered Office:
12-14 Carlton Place
Southampton, UK
SO15 2EA
VAT Registration Number 834 1020 74.
--
This is the Web Enabling the AS400 / iSeries (WEB400) mailing list
To post a message email: WEB400@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/web400
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/web400.


--
This is the Web Enabling the AS400 / iSeries (WEB400) mailing list
To post a message email: WEB400@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/web400
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/web400.


NOTICE: The information in this electronic mail transmission is intended by
CoralTree Systems Ltd for the use of the named individuals or entity to
which it is directed and may contain information that is privileged or
otherwise confidential. If you have received this electronic mail
transmission in error, please delete it from your system without copying or
forwarding it, and notify the sender of the error by reply email or by
telephone, so that the sender's address records can be corrected.




--------------------------------------------------------------------------------


CoralTree Systems Limited
25 Barnes Wallis Road
Segensworth East, Fareham
PO15 5TT

Company Registration Number 5021022.
Registered Office:
12-14 Carlton Place
Southampton, UK
SO15 2EA
VAT Registration Number 834 1020 74.
--
This is the Web Enabling the AS400 / iSeries (WEB400) mailing list
To post a message email: WEB400@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/web400
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/web400.


--
This is the Web Enabling the AS400 / iSeries (WEB400) mailing list
To post a message email: WEB400@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/web400
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/web400.


NOTICE: The information in this electronic mail transmission is intended by CoralTree Systems Ltd for the use of the named individuals or entity to which it is directed and may contain information that is privileged or otherwise confidential. If you have received this electronic mail transmission in error, please delete it from your system without copying or forwarding it, and notify the sender of the error by reply email or by telephone, so that the sender's address records can be corrected.



--------------------------------------------------------------------------------


CoralTree Systems Limited
25 Barnes Wallis Road
Segensworth East, Fareham
PO15 5TT

Company Registration Number 5021022.
Registered Office:
12-14 Carlton Place
Southampton, UK
SO15 2EA
VAT Registration Number 834 1020 74.

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.