Inherited some legacy code and am attempting to do a little caching with it. The query is doing some drop down filtering with an AJAX call to get some XML data. The plan is to intercept the AJAX calls if the element has been requested before by checking the xfilter array for the objCurrent item or allow the AJAX request if it has not and then store the returned XML to the xfilter array.
I am having some issues getting the data to get loaded into the array and back out again once its in been in the array. Can someone point me in the correct direction?
objCurrent is the element ID.
objParent is the parent select item
tmpValue is the value of the selected item in the parent
Bastien
I wish my computer would do what I want it to do,
instead of what I tell it to do...
I am having some issues getting the data to get loaded into the array and back out again once its in been in the array. Can someone point me in the correct direction?
objCurrent is the element ID.
objParent is the parent select item
tmpValue is the value of the selected item in the parent
Code:
var xfilter = new Array();
// Filter a dropdown based on the tmpValue of the Parent
function RequestFilter(objCurrent, objParent, tmpValue){
var url, theField, theListItem;
var objCurrentRealName = '';
// this will store the current selections, prior to the control being redrawn
var arrCurrentSelections = new Array();
// don't attempt on empty field name
if (objCurrent != '') theField = document.getElementById(objCurrent);
// if the field does not exist, then exit.
if (theField == null) return 0;
// save selected values
for (theListItem=0; theListItem < theField.options.length; theListItem ++) {
if (theField.options[theListItem].selected == true) {
arrCurrentSelections[theField.options[theListItem].value] = theField.options[theListItem].text;
};
};
// Remove old list
while (theField.options.length > 0) theField.options[0] = null;
//get the real field name without the silly number
pattern = /(\d)+$/;
objCurrentRealName = objCurrent.replace(pattern,"");
if(xfilter[objCurrentRealName]){
//load the text string into an xml object
theXML = xfilter[objCurrentRealName];
}else{
// do the ajax query
// prepare the XML call
url = "../../includes/apiscontrols2_filter.asp?"
url = url + "xml_field_id=" + objCurrent + "&"
url = url + "xml_filter_id=" + objParent + "&"
url = url + "xml_filter_using=" + tmpValue + "&" + Math.random();
// Set xml_request as the XMLHttpRequest object based on type of browser
// prompt('aa', url);
if(window.XMLHttpRequest) {
xml_request = new XMLHttpRequest();
} else {
if (window.ActiveXObject) xml_request = new ActiveXObject('Microsoft.XMLHTTP');
};
if (xml_request == null) {
return 0;
};
// make the call for filtering synchronous
xml_request.open("POST", url, false);
xml_request.send(null); // send null to end send
// if the response status is 200, it worked, otherwise alert with error.
if(xml_request.status == 200) {
var theXML, theList, sortlist;
// fetch the xml response
theXML = xml_request.responseXML.selectSingleNode('/CONTROL');
//alert(objCurrentRealName+'||'+xfilter[objCurrentRealName]);
if (!xfilter[objCurrentRealName]){ //=="undefined"){
xfilter[objCurrentRealName] = theXML;
//alert("saving xfilter[" + objCurrentRealName+"]");
}
} else {
alert(xml_request.status + ' : ' + xml_request.statusText);
}//end if(xml_request.status == 200)
}//end if(parent.common_area.filter[objCurrent.name]){
if (theXML) {
sortlist = theXML.getAttribute("SORT_LIST");
// remove old values and remember selected ones
theList = theXML.selectNodes("//OPTION");
for (theListItem=0; theListItem < theList.length; theListItem ++) {
// add the option from the response
theField.options[theField.options.length] = new Option(theList[theListItem].getAttribute('VALUE'), theList[theListItem].getAttribute('ID'), false, false)
// if the option was previously selected, then re-select it.
if (arrCurrentSelections[theList[theListItem].getAttribute('ID')]) {
theField.options[theField.options.length-1].selected = true
arrCurrentSelections[theList[theListItem].getAttribute('ID')] = null;
}
};
// add in any remaining previous selections with the leading asterisk
for (theListItem in arrCurrentSelections) {
if (arrCurrentSelections[theListItem]){
if (arrCurrentSelections[theListItem].indexOf("* ") == -1) arrCurrentSelections[theListItem] = "* " + arrCurrentSelections[theListItem]
theField.options[theField.options.length] = new Option(arrCurrentSelections[theListItem], theListItem, true, true)
};
}
// if the result identified the dropdown to be sorted, call the sort_dropdown routine.
if (sortlist.toLowerCase() == 'true') {
Sort_DropDown(theField.id);
}
}
}
Bastien
I wish my computer would do what I want it to do,
instead of what I tell it to do...