Hey all,
I am running into an issue. My situation is that I wish to copy the
contents of one listbox to an array, sort it by innerText value, and
write the sorted options into a new listbox.
Currently I am able to do this by assigning the innerText of the old
listbox to the innerText of the new listbox. I find this tedious as i
have some other attributes that I eventually would need to copy over to
the new listbox.
INSTEAD, I would like to assign the <option> object of the old listbox
to the new listbox object by object, but when i do this I get an object
assignment error.
Below is a stripped down demo that you can run to see whats going on. A
few things to note:
o The function CHashTable is a class providing psudeo hastable
functionality that I use as a map to reference the <option>'s from the
old listbox.
o I am not concerned about having this code be cross-browser compliant,
the client is only working in an IE enviorment (due to the applications
reliance on active X controls).
Below is the code, to run it just click on the button. I commented out
the debugger keywords. Also, the most important function to node is
writeAsElement( ) considering that this is where I am having issues.
Cheers,
Peter
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="stylesheet " type="text/css" href="style.css ">
<title></title>
<meta name="GENERATOR " content="Micros oft Visual Studio .NET 7.1">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</head>
<body>
<script type="text/javascript">
/////////////////////////////////////////
// Function class for providing psudo-hashtable functionality
function CHashTable(){
var m_arryHashTable = new Array();
// function that allows the user to add a specific key
// and value to the hash table
this.addToLooku p = function(p_strN ewLookupKey, p_lngValue){
m_arryHashTable[p_strNewLookupK ey] = p_lngValue;
}
// function to retun if a specific hash function exists
this.checkLooku p = function(p_strL ookupKey){
if(m_arryHashTa ble[p_strLookupKey] === undefined)
return false;
else
return true;
}
this.hashSize = function(){retu rn m_arryHashTable .length; }
// function to return a value the value at a specific hash key value
this.getLookupV alue = function (p_strLookupKey ){
return m_arryHashTable[p_strLookupKey];
}
}
function allyo(){
//debugger;
var oSwap = new CSortList();
oSwap.sendFormR eference();
oSwap.writeAsEl ement();
}
function CSortList(){
var m_docHtmlOption s;
var m_hash = new CHashTable();
var m_array = new Array();
var m_num = new Number();
this.swapOldOpt ionsWithNew2 = function(p_hash , p_sort){
var docHtmlOptionsT emp = document.create Element("option s");
for (var i = 0; i < p_sort.length; i++)
docHtmlOptionsT emp.option[i] = p_hash.getLooku pValue(p_sort[i])
return docHtmlOptionsT emp
}
this.writeAsEle ment = function(){
//debugger;
var docHtmlSelect = document.create Element("select ");
/////////////////////////////////////////
/// What currently works
docHtmlSelect[0] = document.create Element("OPTION ");
docHtmlSelect[1] = document.create Element("OPTION ");
docHtmlSelect[0].innerText = "wawawewow" ;
docHtmlSelect[1].innerText = "asdfewow";
docHtmlSelect[0].innerText =
m_hash.getLooku pValue(m_array[0]).innerText;
docHtmlSelect[1].innerText =
m_hash.getLooku pValue(m_array[1]).innerText;
/////////////////////////////////////////
/////////////////////////////////////////
/////////////////////////////////////////
/// What i would like to work
/*
docHtmlSelect[0] = document.create Element("OPTION "); // may not need
this
docHtmlSelect[1] = document.create Element("OPTION "); // may not need
this
docHtmlSelect[0] = m_hash.getLooku pValue(m_array[0]);
docHtmlSelect[1] = m_hash.getLooku pValue(m_array[1]);
*/
docHtmlSelect.i d = "hardSelect ";
//docHtmlSelect.s tyle.display = "none";
document.body.a ppendChild(docH tmlSelect);
}
this.sendFormRe ference = function()
{
//debugger;
var selectToSort = document.getEle mentById("formI D")
var selectLength = selectToSort.le ngth;
var selectOptions = selectToSort.op tions;
// store options into lookup table, reference via text element
var oHashOptions = new CHashTable();
for (var i = 0; i< selectLength; i++)
oHashOptions.ad dToLookup( selectOptions[i].text, selectOptions[i])
// push the text element of each option into an array
var arryOptionsToSo rt = new Array();
for (var i = 0; i< selectLength; i++)
arryOptionsToSo rt.push(selectO ptions[i].text)
// sort array
arryOptionsToSo rt.sort();
var docHtmlOptionsT emp = new document.create Element("option s");
for (var i = 0; i < arryOptionsToSo rt.length; i++)
docHtmlOptionsT emp[i] =
oHashOptions.ge tLookupValue(ar ryOptionsToSort[i])
m_hash = oHashOptions;
m_array = arryOptionsToSo rt;
m_docHtmlOption s = docHtmlOptionsT emp;
m_num = 3;
}
}
</script>
<p><br>
<!--<button>hey there</button>-->
<select size="5" name="selectIdN ame" id="formID">
<option value="value1"> cvalue1</option>
<option value="value3"> kvalue3</option>
<option value="value2"> avalue2</option>
<option value="value3"> bvalue3</option>
</select>
<button onclick="allyo( );" ID="Button1">cl ickme</button>
</body>
</html>
I am running into an issue. My situation is that I wish to copy the
contents of one listbox to an array, sort it by innerText value, and
write the sorted options into a new listbox.
Currently I am able to do this by assigning the innerText of the old
listbox to the innerText of the new listbox. I find this tedious as i
have some other attributes that I eventually would need to copy over to
the new listbox.
INSTEAD, I would like to assign the <option> object of the old listbox
to the new listbox object by object, but when i do this I get an object
assignment error.
Below is a stripped down demo that you can run to see whats going on. A
few things to note:
o The function CHashTable is a class providing psudeo hastable
functionality that I use as a map to reference the <option>'s from the
old listbox.
o I am not concerned about having this code be cross-browser compliant,
the client is only working in an IE enviorment (due to the applications
reliance on active X controls).
Below is the code, to run it just click on the button. I commented out
the debugger keywords. Also, the most important function to node is
writeAsElement( ) considering that this is where I am having issues.
Cheers,
Peter
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="stylesheet " type="text/css" href="style.css ">
<title></title>
<meta name="GENERATOR " content="Micros oft Visual Studio .NET 7.1">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</head>
<body>
<script type="text/javascript">
/////////////////////////////////////////
// Function class for providing psudo-hashtable functionality
function CHashTable(){
var m_arryHashTable = new Array();
// function that allows the user to add a specific key
// and value to the hash table
this.addToLooku p = function(p_strN ewLookupKey, p_lngValue){
m_arryHashTable[p_strNewLookupK ey] = p_lngValue;
}
// function to retun if a specific hash function exists
this.checkLooku p = function(p_strL ookupKey){
if(m_arryHashTa ble[p_strLookupKey] === undefined)
return false;
else
return true;
}
this.hashSize = function(){retu rn m_arryHashTable .length; }
// function to return a value the value at a specific hash key value
this.getLookupV alue = function (p_strLookupKey ){
return m_arryHashTable[p_strLookupKey];
}
}
function allyo(){
//debugger;
var oSwap = new CSortList();
oSwap.sendFormR eference();
oSwap.writeAsEl ement();
}
function CSortList(){
var m_docHtmlOption s;
var m_hash = new CHashTable();
var m_array = new Array();
var m_num = new Number();
this.swapOldOpt ionsWithNew2 = function(p_hash , p_sort){
var docHtmlOptionsT emp = document.create Element("option s");
for (var i = 0; i < p_sort.length; i++)
docHtmlOptionsT emp.option[i] = p_hash.getLooku pValue(p_sort[i])
return docHtmlOptionsT emp
}
this.writeAsEle ment = function(){
//debugger;
var docHtmlSelect = document.create Element("select ");
/////////////////////////////////////////
/// What currently works
docHtmlSelect[0] = document.create Element("OPTION ");
docHtmlSelect[1] = document.create Element("OPTION ");
docHtmlSelect[0].innerText = "wawawewow" ;
docHtmlSelect[1].innerText = "asdfewow";
docHtmlSelect[0].innerText =
m_hash.getLooku pValue(m_array[0]).innerText;
docHtmlSelect[1].innerText =
m_hash.getLooku pValue(m_array[1]).innerText;
/////////////////////////////////////////
/////////////////////////////////////////
/////////////////////////////////////////
/// What i would like to work
/*
docHtmlSelect[0] = document.create Element("OPTION "); // may not need
this
docHtmlSelect[1] = document.create Element("OPTION "); // may not need
this
docHtmlSelect[0] = m_hash.getLooku pValue(m_array[0]);
docHtmlSelect[1] = m_hash.getLooku pValue(m_array[1]);
*/
docHtmlSelect.i d = "hardSelect ";
//docHtmlSelect.s tyle.display = "none";
document.body.a ppendChild(docH tmlSelect);
}
this.sendFormRe ference = function()
{
//debugger;
var selectToSort = document.getEle mentById("formI D")
var selectLength = selectToSort.le ngth;
var selectOptions = selectToSort.op tions;
// store options into lookup table, reference via text element
var oHashOptions = new CHashTable();
for (var i = 0; i< selectLength; i++)
oHashOptions.ad dToLookup( selectOptions[i].text, selectOptions[i])
// push the text element of each option into an array
var arryOptionsToSo rt = new Array();
for (var i = 0; i< selectLength; i++)
arryOptionsToSo rt.push(selectO ptions[i].text)
// sort array
arryOptionsToSo rt.sort();
var docHtmlOptionsT emp = new document.create Element("option s");
for (var i = 0; i < arryOptionsToSo rt.length; i++)
docHtmlOptionsT emp[i] =
oHashOptions.ge tLookupValue(ar ryOptionsToSort[i])
m_hash = oHashOptions;
m_array = arryOptionsToSo rt;
m_docHtmlOption s = docHtmlOptionsT emp;
m_num = 3;
}
}
</script>
<p><br>
<!--<button>hey there</button>-->
<select size="5" name="selectIdN ame" id="formID">
<option value="value1"> cvalue1</option>
<option value="value3"> kvalue3</option>
<option value="value2"> avalue2</option>
<option value="value3"> bvalue3</option>
</select>
<button onclick="allyo( );" ID="Button1">cl ickme</button>
</body>
</html>
Comment