1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| <html>
<head>
<title>Select Adapter</title>
<script type="text/javascript">
makeSelect=function(s) {
if (s.tagName.toLowerCase() != "select") {
throw "ArgumentException : 's' isn't a valid Select";
}
s.allOptions = new Array();
var all=s.getElementsByTagName("option");
for (var i=0; i<all.length; i++) {
s.allOptions[i] = all[i].cloneNode(true);
s.allOptions[i].display=true;
s.allOptions[i].setAttribute("index", i)
all[i].setAttribute("index", i)
}
s.allOptions.select=s;
s.allOptions.add=function(text, value) {
var index=this.length;
var o=document.createElement("option");
o.display=true;
o.value=value;
o.setAttribute("index", index)
o.appendChild(document.createTextNode(text));
this[index]=o;
this.select.refresh();
return index;
}
s.allOptions.remove=function(index) {
this[index]=null; this.select.refresh(); return true;
}
s.refresh=function() {
/* Conserver la selection IE+FF */
/* Bug Connu : IE ne prend pas en charge la multi selection */
var all=this.getElementsByTagName("option");
for (var i=0; i<this.allOptions.length; i++) {
this.allOptions[i].removeAttribute("selected");
this.allOptions[i].selected=false;
}
for (var i=0; i<all.length; i++) {
var index=all[i].getAttribute("index");
var selected=all[i].selected;
if (""+selected+""=="true") {
this.allOptions[index].setAttribute("selected","selected");
}
}
/* Refresh, IE+FF */
this.innerHTML="";
var current=0; var selectedIndex=-1;
for (var i=0; i<this.allOptions.length; i++) {
var o = this.allOptions[i];
if (o && o.display) {
var newO=o.cloneNode(true);
if (document.all) {
if(current && o.selected) {
selectedIndex=current; current=false;
}
current++;
}
this.appendChild(newO);
}
}
if (document.all && selectedIndex != -1) {
this.selectedIndex=selectedIndex;
}
}
s.hide=function(index,noRefresh) {
var o = this.allOptions[index];
if (o) {
o.display=false;
if (!(noRefresh)) {
this.refresh();
}
return true;
} else {
return false;
}
}
s.show=function(index,noRefresh) {
var o = this.allOptions[index];
if (o) {
o.display=true;
if (!(noRefresh)) {
this.refresh();
}
return true;
} else {
return false;
}
}
s.hideAll=function() {
for (var i=0; i<this.allOptions.length; i++) {
this.hide(i, true);
}
this.refresh()
}
s.showAll=function() {
for (var i=0; i<this.allOptions.length; i++) {
this.show(i, true);
}
this.refresh()
}
return s;
}
</script>
</head>
<body>
<select>
<option value="0">0</option>
<option value="1">1</option>
</select>
<script>
var s = makeSelect(document.getElementsByTagName("select")[0]);
</script>
<input type="button" value="Ajouter une option" onclick="s.allOptions.add(s.allOptions.length,s.allOptions.length)" />
</body>
</html> |
Partager