seanybravo
IS-IT--Management
Please could someone help. I am completely stuck I have some javascript code that runs great on a PC but when I test it on a Mac it does not work.
The code automatically generates dropdown boxes depending on the amount of categories and creates a parent child relationship. Multiple categories are split up using | (pipes). I want to use this code to work with our existing databases which split categories in this way.
I have not had much experience with javascript and I was hoping someone could shed some light on what maybe causing the problem.
Regards
Sean
Here is the code:
<HTML>
<HEAD>
<TITLE></TITLE> <SCRIPT TYPE="text/javascript">
<!--
//Define the record object
function objCategory(level,sort,parent,category){
this.level=level;
this.sort=sort;
this.parent=parent;
this.category=category;
}
function objList(text,value){
this.text=text;
this.value=value;
}
function padLevel(number) { return (number < 10) ? "0" + number : number }
function removePadLevel(number) { return (number < 10) ? parsInt(number) : number }
function padSort(number){
if(number < 10) {
number = "00" + number;
}else{
if(number < 100){
number = "0" + number;
}
}
return number;
}
function removePadSort(number){
if (number != ""){
return "["+parseInt(number)+"]";
}else{
return "";
}
}
//Sort the table data. Returns -1 = Acending 1 = Decending
function compare(a,b){return ( a.level + a.sort + a.parent + a.category < b.level + b.sort + b.parent + b.category) ? -1 : 1 }
//Build the Category lists
function buildCatList(formName,listLevel,listName,listValue){
listValue = listValue.split("[")[0]
removeOption(formName,listLevel);
if(vncatCount >= padLevel(listName)){
var vsList = new Array();
for(var i=0;i<vnRecordLength;i++){
if(vsRecord.level==padLevel(listName)){break};
}
var vnListLength = null;
for(i;i<vnRecordLength;i++){
if(vsRecord.level!=padLevel(listName)){break};
if(vsRecord.parent == listValue){
if(vnListLength == null){
vsList[0] = new objList(vsRecord.category,vsRecord.category + removePadSort(vsRecord.sort));
vnListLength = 0;
}
if(vsRecord.category!=vsList[vnListLength].text){
vnListLength++;
vsList[vnListLength] = new objList(vsRecord.category,vsRecord.category + removePadSort(vsRecord.sort));
}
}
}
//Add categories to list box
var vnListLength = vsList.length
for(var i=0; i<vnListLength; i++){
addOption(formName,listLevel,vsList.text,vsList.value);
}
}
/*
//Display the list For testing purposes only
document.write("<br>" + "Result for " + padlevel(listName) +" "+ listValue + "<br>")
var vnListLength = vsList.length
for (var i=0; i<vnListLength; i++){
document.write(vsList+ "<br>")
}
*/
}
function addOption(formName,listLevel,text,value){
var theList = document.forms[formName].elements[listLevel];
var newOption = new Option(text,value);
theList.options[theList.length]= newOption;
}
function removeOption(formName,listLevel){
for(var i=parseInt(vncatCount)-1;i>=listLevel;i--){
var theList = document.forms[formName].elements;
for(var j=theList.options.length;j>0;j--){
theList.options.remove(j);
}
//This refreshes the lists better
var newOption = new Option("Select Option","Select Option");
theList.options[0]= newOption;
}
//Build the querry string
theList = document.forms[formName].elements[0];
vsQueryString = theList.options.value;
for(i=1;i<parseInt(vncatCount);i++){
theList = document.forms[formName].elements;
if(theList.options.value != "Select Option"){
vsQueryString += "|"
vsQueryString += theList.options.value;
}
}
}
var vsTableData = new Array();
var vsRecord = new Array();
var vsString;
var vsParent;
var vsCategory;
var vsSort;
var vsQueryString
vsTableData[vsTableData.length] = "Agriculture, Forestry & Fishing[1]|Growing of crops; market gardening; horticulture"
vsTableData[vsTableData.length] = "Agriculture, Forestry & Fishing[1]|Mixed farming|Cows"
vsTableData[vsTableData.length] = "Agriculture, Forestry & Fishing[1]|Farming of animals|Pigs|Sheep"
vsTableData[vsTableData.length] = "Agriculture, Forestry & Fishing[1]|Mixed farming|Pigs"
vsTableData[vsTableData.length] = "Sean[0]|Found me"
vsTableData[vsTableData.length] = "Sean[0]|Found me|Found me again"
vsTableData[vsTableData.length] = "And[999]|Test 10"
//
var vnRecordLength = 0;
var vnTableDataLength = vsTableData.length;
for (var i=0;i<vnTableDataLength;i++){
vsString = vsTableData.split("|")
vsParent = "";
var vnStringLength = vsString.length;
for (var j=0;j<vnStringLength;j++) {
vsCategory = vsString[j].split("[");
if (j==0){
if (vsCategory[1] != null){
vsCategory[1] = padSort(vsCategory[1].slice(0,vsCategory[1].length - 1));
}else{
vsCategory[1] = padSort(0);
}
}else{
vsCategory[1] = "";
}
vsRecord[vnRecordLength] = new objCategory(padLevel(j+1),vsCategory[1],vsParent,vsCategory[0]);
vsParent = vsCategory[0];
vnRecordLength++;
}
}
vsRecord.sort(compare);
/*
document.write("Complete table" + "<br>")
for (var i=0;i<vnRecordLength;i++) {
document.write(vsRecord.level + "," + vsRecord.sort + "," + vsRecord.parent + "," + vsRecord.category + removePadSort(vsRecord.sort) + "<br>");
}
*/
//Draw the drop list(s) and the form.
var vncatCount = vsRecord[vsRecord.length-1].level;
document.write("<FORM NAME=\"category\">");
for (var i=1 ;i<=vncatCount;i++){
var j=i+1;
document.write("<P><SELECT NAME=\"L"+ j +"\" SIZE=\"1\" ONCHANGE=\"buildCatList(this.form.name," + i + ",name.slice(1),value)\">");
document.write("<OPTION VALUE=\"Select Option\">Select Option</OPTION>");
document.write("</SELECT></P>");
}
document.write("</FORM>");
document.write("<FORM NAME=\"submitcategory\">");
document.write("<P><INPUT TYPE=\"SUBMIT\" NAME=\"SubmitQuery\" ONCLICK=\"alert(vsQueryString)\" ONKEYPRESS=\"alert(vsQueryString)\"></P>");
document.write("</FORM>");
buildCatList("category",0,1,"");
//-->
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
The code automatically generates dropdown boxes depending on the amount of categories and creates a parent child relationship. Multiple categories are split up using | (pipes). I want to use this code to work with our existing databases which split categories in this way.
I have not had much experience with javascript and I was hoping someone could shed some light on what maybe causing the problem.
Regards
Sean
Here is the code:
<HTML>
<HEAD>
<TITLE></TITLE> <SCRIPT TYPE="text/javascript">
<!--
//Define the record object
function objCategory(level,sort,parent,category){
this.level=level;
this.sort=sort;
this.parent=parent;
this.category=category;
}
function objList(text,value){
this.text=text;
this.value=value;
}
function padLevel(number) { return (number < 10) ? "0" + number : number }
function removePadLevel(number) { return (number < 10) ? parsInt(number) : number }
function padSort(number){
if(number < 10) {
number = "00" + number;
}else{
if(number < 100){
number = "0" + number;
}
}
return number;
}
function removePadSort(number){
if (number != ""){
return "["+parseInt(number)+"]";
}else{
return "";
}
}
//Sort the table data. Returns -1 = Acending 1 = Decending
function compare(a,b){return ( a.level + a.sort + a.parent + a.category < b.level + b.sort + b.parent + b.category) ? -1 : 1 }
//Build the Category lists
function buildCatList(formName,listLevel,listName,listValue){
listValue = listValue.split("[")[0]
removeOption(formName,listLevel);
if(vncatCount >= padLevel(listName)){
var vsList = new Array();
for(var i=0;i<vnRecordLength;i++){
if(vsRecord.level==padLevel(listName)){break};
}
var vnListLength = null;
for(i;i<vnRecordLength;i++){
if(vsRecord.level!=padLevel(listName)){break};
if(vsRecord.parent == listValue){
if(vnListLength == null){
vsList[0] = new objList(vsRecord.category,vsRecord.category + removePadSort(vsRecord.sort));
vnListLength = 0;
}
if(vsRecord.category!=vsList[vnListLength].text){
vnListLength++;
vsList[vnListLength] = new objList(vsRecord.category,vsRecord.category + removePadSort(vsRecord.sort));
}
}
}
//Add categories to list box
var vnListLength = vsList.length
for(var i=0; i<vnListLength; i++){
addOption(formName,listLevel,vsList.text,vsList.value);
}
}
/*
//Display the list For testing purposes only
document.write("<br>" + "Result for " + padlevel(listName) +" "+ listValue + "<br>")
var vnListLength = vsList.length
for (var i=0; i<vnListLength; i++){
document.write(vsList+ "<br>")
}
*/
}
function addOption(formName,listLevel,text,value){
var theList = document.forms[formName].elements[listLevel];
var newOption = new Option(text,value);
theList.options[theList.length]= newOption;
}
function removeOption(formName,listLevel){
for(var i=parseInt(vncatCount)-1;i>=listLevel;i--){
var theList = document.forms[formName].elements;
for(var j=theList.options.length;j>0;j--){
theList.options.remove(j);
}
//This refreshes the lists better
var newOption = new Option("Select Option","Select Option");
theList.options[0]= newOption;
}
//Build the querry string
theList = document.forms[formName].elements[0];
vsQueryString = theList.options.value;
for(i=1;i<parseInt(vncatCount);i++){
theList = document.forms[formName].elements;
if(theList.options.value != "Select Option"){
vsQueryString += "|"
vsQueryString += theList.options.value;
}
}
}
var vsTableData = new Array();
var vsRecord = new Array();
var vsString;
var vsParent;
var vsCategory;
var vsSort;
var vsQueryString
vsTableData[vsTableData.length] = "Agriculture, Forestry & Fishing[1]|Growing of crops; market gardening; horticulture"
vsTableData[vsTableData.length] = "Agriculture, Forestry & Fishing[1]|Mixed farming|Cows"
vsTableData[vsTableData.length] = "Agriculture, Forestry & Fishing[1]|Farming of animals|Pigs|Sheep"
vsTableData[vsTableData.length] = "Agriculture, Forestry & Fishing[1]|Mixed farming|Pigs"
vsTableData[vsTableData.length] = "Sean[0]|Found me"
vsTableData[vsTableData.length] = "Sean[0]|Found me|Found me again"
vsTableData[vsTableData.length] = "And[999]|Test 10"
//
var vnRecordLength = 0;
var vnTableDataLength = vsTableData.length;
for (var i=0;i<vnTableDataLength;i++){
vsString = vsTableData.split("|")
vsParent = "";
var vnStringLength = vsString.length;
for (var j=0;j<vnStringLength;j++) {
vsCategory = vsString[j].split("[");
if (j==0){
if (vsCategory[1] != null){
vsCategory[1] = padSort(vsCategory[1].slice(0,vsCategory[1].length - 1));
}else{
vsCategory[1] = padSort(0);
}
}else{
vsCategory[1] = "";
}
vsRecord[vnRecordLength] = new objCategory(padLevel(j+1),vsCategory[1],vsParent,vsCategory[0]);
vsParent = vsCategory[0];
vnRecordLength++;
}
}
vsRecord.sort(compare);
/*
document.write("Complete table" + "<br>")
for (var i=0;i<vnRecordLength;i++) {
document.write(vsRecord.level + "," + vsRecord.sort + "," + vsRecord.parent + "," + vsRecord.category + removePadSort(vsRecord.sort) + "<br>");
}
*/
//Draw the drop list(s) and the form.
var vncatCount = vsRecord[vsRecord.length-1].level;
document.write("<FORM NAME=\"category\">");
for (var i=1 ;i<=vncatCount;i++){
var j=i+1;
document.write("<P><SELECT NAME=\"L"+ j +"\" SIZE=\"1\" ONCHANGE=\"buildCatList(this.form.name," + i + ",name.slice(1),value)\">");
document.write("<OPTION VALUE=\"Select Option\">Select Option</OPTION>");
document.write("</SELECT></P>");
}
document.write("</FORM>");
document.write("<FORM NAME=\"submitcategory\">");
document.write("<P><INPUT TYPE=\"SUBMIT\" NAME=\"SubmitQuery\" ONCLICK=\"alert(vsQueryString)\" ONKEYPRESS=\"alert(vsQueryString)\"></P>");
document.write("</FORM>");
buildCatList("category",0,1,"");
//-->
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>