Jump to content

trying to load default selected item by top level sid variable which is retrieved from xml...


Greywacke

Recommended Posts

in an ongoing attempt to resolve this on my own while i await generous assistance from the other javascripting forum users - the ajax_attributes.js portions get updated continuously, the xml is perfect. selecting the xml and attributes is perfect. handling it in javascript is not... *facepalms*

the script has sofar been updated as follows...

// top level "global" variables set at top of scriptvar sid = 0;var servicearr = new Array();// plannned future top level variables for reading and storing the attribute "ancestrees"var pss = "0";var ddcnt = 0;var keys = new Array(new Array(),new Array()); // parse servicesvar services = xmldoc.getElementsByTagName("service");var arr10 = new Array();servicearr = new Array();for (var x = 0; x < services.length; x++) {	if (services[x].attributes.getNamedItem("sid") && services[x].attributes.getNamedItem("desc") && services[x].attributes.getNamedItem("selected") && indexOf.call(servicearr,services[x].attributes.getNamedItem("sid").nodeValue)==-1) {		var arr = new Array();		var aa = 0;		var arr1 = new Array();		arr[0] = services[x].attributes.getNamedItem("sid").nodeValue;		arr[1] = services[x].attributes.getNamedItem("desc").nodeValue;		arr[2] = services[x].attributes.getNamedItem("selected").nodeValue;		var i = 1;		for (var c = 0; c < services[x].childNodes.length; c++) {			if (services[x].childNodes[c].nodeName == "attribute" && services[x].childNodes[c].attributes.getNamedItem("id").nodeValue != undefined) {				var arr2 = new Array();							arr2[0] = services[x].childNodes[c].attributes.getNamedItem("id").nodeValue;				arr2[1] = services[x].childNodes[c].attributes.getNamedItem("desc").nodeValue;				arr2[2] = services[x].childNodes[c].attributes.getNamedItem("val").nodeValue;				arr2[3] = services[x].childNodes[c].attributes.getNamedItem("ord").nodeValue;				arr2[4] = (services[x].childNodes[c].attributes.getNamedItem("ptr").nodeValue!="")?services[x].childNodes[c].attributes.getNamedItem("ptr").nodeValue:"";				arr1[aa] = arr2.join(";;;");				aa++;			}		}		arr[3] = arr1.join(";.;");	// service attributes		//salert(arr);		if (arr.length) {			addrecord("menu_services", arr);			servicearr.push(arr[0]);			svc = true;		}	}} 

as for the xml - it is still without errors, and is still generated as follows.

<root><service sid="10" desc="Accounting Bookkeeping" selected="">...</service><service sid="1" desc="Bakkie Canopy" selected="selected">...</service><service sid="3" desc="Bakkie Canopy Accessories" selected=""></service><service sid="2" desc="Bakkie Linings & Rubberizing" selected=""></service><service sid="15" desc="Broadband Internet" selected="">...</service><service sid="5" desc="Business Phone Systems" selected="">...</service><service sid="9" desc="Company Registrations" selected="">...</service><service sid="11" desc="Conference Venues" selected="">...</service><service sid="8" desc="Debt Collection Agencies" selected="">...</service><service sid="17" desc="Office Coffee" selected="">...</service><service sid="7" desc="Office Colour Printers" selected="">...</service><service sid="12" desc="Office Furniture" selected="">...</service><service sid="13" desc="Office Movers" selected="">...</service><service sid="14" desc="Office Network Cabling" selected="">...</service><service sid="6" desc="Office Printers & Copiers" selected="">...</service><service sid="16" desc="Office Water" selected="">...</service><service sid="4" desc="Placeholder" selected=""></service><sql/></root>  

obviously, this abbreviated excerpt comes from google chrome's view of the file, which has the following header info:

<?xml version="1.0" encoding="utf-8"?> 

if nobody wants to help then i guess it's also fine - i am getting there as fast as possible under the current circuimstances sleep.png

 

can anyone see why the services dropdown, does not want to select the single service which has the selected="selected" attribute?

 

sincerely - Pierre "Greywacke" du Toit.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

the data does populate in the select box, for some reason it just does not want to set the selectedIndex or selected = true attribute of the options as i add them in the script...

function addrecord(id, arr) {	//salert(id);	//salert(arr);	var opt0 = document.createElement("option");	opt0.text = arr[1];								// value 1 text service name	opt0.value = arr[0] + "|;|" +					// value 0 service id				 arr[2] + "|;|" + 					// save selection flag ("selected" or "")				 arr[3];							// save the value 3 attribute keys/values/order array string	if (arr[2]=="selected") opt0.selected = true;	var sel = document.getElementById(id);	try {		sel.add(opt0, null); 						// standards compliant; doesn't work in IE	}	catch(ex) {		sel.add(opt0); 								// IE only	}	//if (arr[2]=="selected") sel.selectedIndex = sel.options.length - 1;} 

neither the option will get set to selected, nor setting the selected option by changing the selectedIndex property of the dropdown select works - i am at my wits ends right now...

- Pierre "Greywacke" du Toit

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

at the point of calling the addrecord function, the properties are all in an array... arr[2] is the index which receives this "selected" value if set, otherwise it's blank...

Link to comment
Share on other sites

ok success at last! :D

i've managed to get the scvript working by modifying it as follows:

// top level variablesvar service = -1;var ajaxloading = 0;var sid = 0;// javascript xml reading sectionvar services = xmldoc.getElementsByTagName("service");var arr10 = new Array();servicearr = new Array();for (var x = 0; x < services.length; x++) {	if (services[x].attributes.getNamedItem("sid") && services[x].attributes.getNamedItem("desc") && services[x].attributes.getNamedItem("selected") && indexOf.call(servicearr,services[x].attributes.getNamedItem("sid").nodeValue)==-1) {		var arr = new Array();		var aa = 0;		var arr1 = new Array();		arr[0] = services[x].attributes.getNamedItem("sid").nodeValue;		arr[1] = services[x].attributes.getNamedItem("desc").nodeValue;		arr[2] = services[x].attributes.getNamedItem("selected").nodeValue;		var i = 1;		for (var c = 0; c < services[x].childNodes.length; c++) {			if (services[x].childNodes[c].nodeName == "attribute" && services[x].childNodes[c].attributes.getNamedItem("id").nodeValue != undefined) {				var arr2 = new Array();				arr2[0] = services[x].childNodes[c].attributes.getNamedItem("id").nodeValue;				arr2[1] = services[x].childNodes[c].attributes.getNamedItem("desc").nodeValue;				arr2[2] = services[x].childNodes[c].attributes.getNamedItem("val").nodeValue;				arr2[3] = services[x].childNodes[c].attributes.getNamedItem("ord").nodeValue;				arr2[4] = (services[x].childNodes[c].attributes.getNamedItem("ptr").nodeValue!="")?services[x].childNodes[c].attributes.getNamedItem("ptr").nodeValue:"";				arr1[aa] = arr2.join(";;;");				aa++;			}		}		arr[3] = arr1.join(";.;");	// service attributes		//salert(arr);		if (arr.length) {			addrecord("menu_services", arr);			if (arr[2]!="") document.getElementById("menu_services").selectedIndex = service;			servicearr.push(arr[0]);			svc = true;		}	}}// populating functionfunction addrecord(id, arr) {	//salert(id);	//salert(arr);	var opt0 = document.createElement("option");	opt0.text = arr[1];								// value 1 text service name	opt0.value = arr[0] + "|;|" +					// value 0 service id				 arr[2] + "|;|" + 					// save selection flag ("selected" or "")				 arr[3];							// save the value 3 attribute keys/values/order array string	var sel = document.getElementById(id);			// get the select element	try {		sel.add(opt0, null); 						// standards compliant; doesn't work in IE	}	catch(ex) {		sel.add(opt0); 								// IE only	}	if (arr[2]!="") {		sid = parseInt(arr[0]);		service = sel.options.length - 1;	}}

as can be seen, the function sets the service top level variable to the index to be marked as selected. once the dropdown is populated by the options in the xml reader, it sets the selectedIndex if the current options arr[2] value was noticed to != "" ;)

thanks for the help in refining the script justsomeguy! :D

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...