Alternatively, this is more generic but less efficient
var items = "<option>(Select)</option>";
var savedParent = 0;
$.each(jsonRequestType,function(i,rcd){
if (rcd.Id==key) {
$.each(rcd.HourTypeCodes, function(i, item) {
.........
});
$(tsddl).html(items);
return false; // Quits jQuery loop
}
})
Or you could store the HourTypeCodes against the <option> element when you build the first <select>. This would be more efficient and less code. So you could do 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 you could do this:
var items = "<option>(Select)</option>";
var savedParent = 0;
$.each(this.options[this.selectedIndex]._hourtypecodes, function(i, item) {
......
});
$(tsddl).html(items);
-----Original Message-----
From: Kevin Turner
Sent: 15 December 2010 20:54
To: 'Web Enabling the AS400 / iSeries'
Subject: RE: [WEB400] Navigating JSON sub-arrays dynamically
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.
As an Amazon Associate we earn from qualifying purchases.