Posted By

jeffreality on 04/24/08


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

korzhik
czehner


Auto-resort numbers in selectable numbered list


 / Published in: JavaScript
 

When you have a list of items to sort (say, three items, each with a drop-down from 1-3), this script will (when one of the item drop-downs is changed) automatically change the others to have appropriate numbers. For example, if your list is numbered: 1, 2, 3...and you change the second item to 3, the list will then be numbered 1, 3, 2.

  1. function changeNumericSequence (objSelectChosen)
  2. {
  3.  
  4. // ------------------------------------------------------------------------------
  5.  
  6. // Created: Jeffrey B. Berthiaume
  7. // Date: 12 December 2000
  8. // Version: 1.0
  9.  
  10. // Description: changeNumericSequence manipulates numbers in a series of dropdown
  11. // select lists. If there are three dropdown select lists (each with the options
  12. // of "1", "2", and "3") in a numerical sequence, this function will change the
  13. // other select lists as appropriate.
  14.  
  15. // Each select list in the html form should have the name "cat_" + a common group
  16. // name for the group of select lists + a unique identifier.
  17.  
  18. // for example:
  19.  
  20. // <select name="cat_1_item_32" onChange="changeNumericSequence (this);">
  21.  
  22. // Parameter:
  23. // objSelectChosen (should be the select object that was just changed)
  24.  
  25. // Usage example:
  26. // <select name="cat_1_item_32" onChange="changeNumericSequence (this);">
  27.  
  28. // ------------------------------------------------------------------------------
  29.  
  30. // find out which group of select lists (category) are being manipulated
  31. strSelectChosenName = objSelectChosen.name;
  32. arrSelectChosenName = strSelectChosenName.split ("_");
  33.  
  34. // strCategoryName contains the name of the category (i.e. the n in "cat_n")
  35. strCategoryName = arrSelectChosenName [1];
  36.  
  37. // find total number of items to manipulate
  38. intNumItems = objSelectChosen.length;
  39.  
  40. // store the changed number (new number chosen for this dropdown)
  41. intChangedItemNumber = objSelectChosen.selectedIndex;
  42.  
  43. arrItemNumbers = new Array (intNumItems);
  44. arrSelectNames = new Array (intNumItems);
  45.  
  46.  
  47. // go through and build array of all numbers in select list
  48.  
  49. // increment through all select lists in the parent form of objSelectChosen
  50. // if their name begins with "cat_" + strCategoryName then they belong to this group of categories
  51.  
  52. intTotalFormElements = objSelectChosen.form.elements.length
  53. intArrTotal = 0;
  54. for (i = 0; i < intTotalFormElements; i++)
  55. {
  56. strElementName = objSelectChosen.form.elements [i].name;
  57. arrSelectName = strElementName.split ("_");
  58. if (strCategoryName == arrSelectName [1])
  59. {
  60.  
  61. if (strElementName == strSelectChosenName)
  62. {
  63. intChangedItemIndex = intArrTotal;
  64. }
  65.  
  66. arrSelectNames [intArrTotal] = strElementName;
  67. arrItemNumbers [intArrTotal] = objSelectChosen.form.elements [i].selectedIndex;
  68. intArrTotal = intArrTotal + 1;
  69. }
  70.  
  71. }
  72.  
  73. // calculate the number that is missing
  74. intMissingNum = -1;
  75. i = 0;
  76. while (intMissingNum == -1)
  77. {
  78. boolNumFound = -1;
  79.  
  80. for (i2 = 0; i2 < intNumItems; i2++)
  81. {
  82.  
  83. if (arrItemNumbers [i2] == i)
  84. {
  85. boolNumFound = 0;
  86. }
  87.  
  88. }
  89.  
  90. if (boolNumFound == -1)
  91. {
  92. intMissingNum = i;
  93. }
  94.  
  95. i++;
  96.  
  97. }
  98.  
  99. // re-order the numbers
  100.  
  101. // the current item was intMissingNum
  102. // but now it's intChangedItemNumber
  103.  
  104.  
  105. if (intMissingNum < intChangedItemNumber)
  106. {
  107.  
  108. // find all numbers between (intMissingNum + 1) and intChangedItemNumber
  109.  
  110. for (i = 0; i < intNumItems; i++)
  111. {
  112.  
  113. num = arrItemNumbers [i];
  114.  
  115. if ((i != intChangedItemIndex) && (num >= (intMissingNum + 1)) && (num <= intChangedItemNumber))
  116. {
  117.  
  118. // decrement their indices by 1
  119. arrItemNumbers [i] = arrItemNumbers [i] - 1;
  120.  
  121. }
  122. }
  123.  
  124.  
  125. }
  126. else
  127. {
  128.  
  129. // find all numbers between (intChangedItemNumber + 1) and intMissingNum
  130.  
  131. for (i = 0; i < intNumItems; i++)
  132. {
  133.  
  134. num = arrItemNumbers [i];
  135.  
  136. if ((i != intChangedItemIndex) && (num >= intChangedItemNumber) && (num < intMissingNum))
  137. {
  138.  
  139. // increment their indices by 1
  140. arrItemNumbers [i] = arrItemNumbers [i] + 1;
  141.  
  142. }
  143. }
  144.  
  145. }
  146.  
  147. // change the selection drop boxes to be the newly ordered numbers
  148.  
  149. for (i = 0; i < intNumItems; i++)
  150. {
  151. eval ("document." + objSelectChosen.form.name + "." + arrSelectNames [i] + ".selectedIndex = " + arrItemNumbers [i] + ";")
  152. }
  153.  
  154.  
  155. }

Report this snippet  

You need to login to post a comment.