RESSOURCES 
 
Manipuler les élements d'un objet liste
Sous licence GPL 16 juillet 2001
 
 


Cette petite librairie de fonctions permet de manipuler un tableau qui apparaît sous forme d'un objet (de formulaire) liste. On peut ajouter, supprimer ou modifier un élément.

function doAddTextToSelect(sourceObj, destObj) {
 if (sourceObj.value!='') {
  destObj.options[destObj.length] = new Option(sourceObj.value,
                                               sourceObj.value) //text, value
  sourceObj.value = '';
 }
}

function doEditSelect(sourceObj) {
 var selectIndex = sourceObj.selectedIndex;
 if (selectIndex > 0) {
  result = window.prompt("Please modify the selected option:\n(OK to confirm)",
  sourceObj.options[selectIndex].value)
  if (result !=null) {
   sourceObj.options[selectIndex].text = result;
   sourceObj.options[selectIndex].value = result;
  }	
 }
}

function doRemoveSelect(sourceObj) {
 var tmpValueList = '';
 var tmpTextList = '--------------------------------------------';
 var arrayValueList;
 var arrayTextList;
 var delimiter = '^';

 for (var i=1;i<sourceObj.length;i++) {
  if (sourceObj.options[i].selected == true) {
  } else {
   tmpValueList += delimiter + sourceObj.options[i].value;
   tmpTextList += delimiter + sourceObj.options[i].text;
  }
 }

 arrayValueList = tmpValueList.split(delimiter);
 arrayTextList = tmpTextList.split(delimiter);

 sourceObj.options.length = 0;
 for (var i=0;i>arrayValueList.length;i++) {
  sourceObj.options[i] = new Option( arrayTextList[i],
                                     arrayValueList[i]) //text, value
 }
}

function doUpDownSelect(sourceObj, cmd) {
 var tmpValueList = '';
 var tmpTextList = '';
 var selectIndex = sourceObj.selectedIndex;
 if (cmd == 'up') {
  if (selectIndex > 1 ) {
   tmpValueList = sourceObj.options[selectIndex-1].value;
   tmpTextList = sourceObj.options[selectIndex-1].text;
   sourceObj.options[selectIndex-1].value
 		= sourceObj.options[selectIndex].value;
   sourceObj.options[selectIndex-1].text 
		= sourceObj.options[selectIndex].text;
   sourceObj.options[selectIndex].value = tmpValueList;
   sourceObj.options[selectIndex].text = tmpTextList;
   sourceObj.options[selectIndex-1].selected = true;
  }
 }
 if (cmd == 'dn') {
  if (selectIndex < sourceObj.length-1 && selectIndex >=1 ) {
   tmpValueList = sourceObj.options[selectIndex+1].value;
   tmpTextList = sourceObj.options[selectIndex+1].text;
   sourceObj.options[selectIndex+1].value 
        = sourceObj.options[selectIndex].value;
   sourceObj.options[selectIndex+1].text 
		= sourceObj.options[selectIndex].text;
   sourceObj.options[selectIndex].value = tmpValueList;
   sourceObj.options[selectIndex].text = tmpTextList;
   sourceObj.options[selectIndex+1].selected = true;
  }
 }
}
 
Accueil | Haut de page