Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Przenoszenie elementów pomiędzy selectami
Forum PHP.pl > Forum > Po stronie przeglądarki
tiraeth
Witam, mam problem gdyż piszę system downloadu, i jedną z rzeczy jaka musi być w nim zawarta to wybór plików, które mogą być uploadowane w danej kategorii. Typy tych plików muszą być wybierana podczas tworzenia kategorii.
No i teraz chciałbym zrobić, żeby to wyglądało tak:
Kod
Wybór plików:                     Akceptowane typy:
+------------------+              +------------------+
| Audio            |              |                  |
|   .mp3           |              |                  |
|   .wav           |              |                  |
|   .ogg           |    ------    |                  |
|   .wma           |   |  >>  |   |                  |
|                  |    ------    |                  |
| Video            |              |                  |
|   .avi           |    ------    |                  |
|   .dat           |   |  <<  |   |                  |
|   .mpeg          |    ------    |                  |
|   .wmv           |              |                  |
|                  |              |                  |
|    Wszystkie     |              |                  |
|                  |              |                  |
|                  |              |                  |
+------------------+              +------------------+


Chodzi mi o te dwa przyciski >> i << chciałbym aby po nacisnięciu tego pierwszego, zaznaczony element (option) z select'u select1 został dodany jako nowa pozycja do selectu obok - select2, oraz usunięty z select1. Przycisk drugi miałby robić to samo ale w drugą stronę smile.gif

Zastanawiam się jak to zrobić biggrin.gif Proszę o pomoc...

Aha, jeszcze nie wiem jak po nacisnięciu przycisku BUTTON zrobić tak aby zaznaczyło cały select2, aby wybrane typy zostały przesłane poprzez $_POST, co do wyslania formularza z BUTTON to wiem jak to zrobić...
revyag
Kod
function c(from,to){
    var x=document.getElementById(from);
    var y=document.getElementById(to);
    y.options[y.length] = new Option(x.options[x.selectedIndex].text,x.selectedIndex.value);
    x.remove(x.selectedIndex);
}

Funkcja przerzuca zaznaczony option z selecta "from" do selecta "to" i usuwa go z selecta "from".
Aha, option jest dodawany na końcu selecta.
ganimedes
Fajne działa, napewno przyda się smile.gif) Dzięki
tiraeth
To raczej ja powininenem podziękować... THC 4 JS smile.gif

Lekko go ulepszyłem pod względem wizualnym:
Kod
function c(from,to)
{
   var x=document.getElementById(from);
   var y=document.getElementById(to);
   y.options[y.length] = new Option(x.options[x.selectedIndex].text,x.selectedIndex.value);
   x.remove(x.selectedIndex);
}
cerastes
Witam

czy mozna by prosic o wklejenie tutaj calego kodu jak by to mialo wygladac? mecze sie juz nad tym od kilku dni i kompletnie nic mi nie wychodzi :/ z gory dziekuje za zyczliwe podejscie do "nowego" smile.gif
revyag
Cytat(cerastes @ 2005-04-13 23:32:58)
czy mozna by prosic o wklejenie tutaj calego kodu jak by to mialo wygladac?

Chodzi Ci o przykład z selectami ?
  1. <select id="s1">
  2. <option value="s11">s11</option>
  3. <option value="s12">s12</option>
  4. <option value="s13">s13</option>
  5. <option value="s14">s14</option>
  6. <option value="s15">s15</option>
  7. </select><br /><br />
  8. <select id="s2">
  9. <option value="s21">s21</option>
  10. <option value="s22">s22</option>
  11. <option value="s23">s23</option>
  12. <option value="s24">s24</option>
  13. <option value="s25">s25</option>
  14. </select><br /><br />
  15. <input type="button" value=">>" onclick="c('s1','s2')"><br /><br />
  16. <input type="button" value="<<" onclick="c('s2','s1')">
cerastes
dzieki bardzo smile.gif
phpion
Odswiezam.
Czy daloby sie zmodyfikowac skrypt tak, zeby przenosil dane z jednego pola do drugiego we wskazane miejsce? Tj. np z s1 za aktualnie zaznaczony element w s2?
EDIT:
I moze jeszcze jedna modyfikacja: zeby obslugiwal pola multiple czyli pozwalal na przenoszenie kilku wartosci na raz. Nie wiem nawet pod jakim haslem szukac - prosilbym o nakierowanie mnie.

Ssss jednak na nic mi sie to przyda :/
Moze przy okazji: czy daloby sie przeslac do skryptu php wszsytkie dane (obojetnie czy indexy czy wartosci) znajdujace sie w drugim selectcie? Zakaldam ze to bylaby jakas funkcja przypisujaca do zmiennej (np. stringa) wszystkie wartosci.

=============================================

Znalazlem odpowiednie skrypciki, polaczylem je w calosc i PRAWIE dzialaja poprawnie. Mozna przenosic po kilka pozycji na raz, mozna dodawac dane we wskazane miejsce, mozna dane przeslac do php (funkcja wyslij()). Jednak jest pewien maly problem. Otoz przenoszenie danych powoduje przeniesienie wskazanych danych + pustych pol w ilosci rownej liczbie przenoszonych pol. Nie wiem jak sie tego pozbyc. Przy wysylaniu danych przez wyslij() te puste pola nie sa juz pokazywane. Mialby ktos jakis pomysl?
  1. <?xml version="1.0" encoding="iso-8859-2"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.  
  5. <title>Przenoszenie miedzy selectami</title>
  6. <meta name="GENERATOR" content="Quanta Plus" />
  7. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
  8. <style type="text/css">
  9. /* <![CDATA[ */
  10.  
  11. /* ]]> */
  12. </style>
  13. <script language="javascript" type="text/javascript">
  14. /* <![CDATA[ */
  15. var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
  16.  
  17. function wyslij(theSel)
  18. {
  19. var selLength = theSel.length;
  20. var output = '';
  21.  
  22. for (i=0; i<selLength; i++)
  23. {
  24. output += theSel.options[i].text + ', ';
  25. }
  26.  
  27. alert(output);
  28. }
  29.  
  30. function appendOldSchool(theSel, newText, newValue)
  31. {
  32. if (theSel.length == 0) {
  33. var newOpt1 = new Option(newText, newValue);
  34. theSel.options[0] = newOpt1;
  35. theSel.selectedIndex = 0;
  36. } else if (theSel.selectedIndex != -1) {
  37. var selText = new Array();
  38. var selValues = new Array();
  39. var selIsSel = new Array();
  40. var newCount = -1;
  41. var newSelected = -1;
  42. var i;
  43. for(i=0; i<theSel.length; i++)
  44. {
  45. newCount++;
  46. selText[newCount] = theSel.options[i].text;
  47. selValues[newCount] = theSel.options[i].value;
  48. selIsSel[newCount] = theSel.options[i].selected;
  49.  
  50. if (newCount == theSel.selectedIndex) {
  51. newCount++;
  52. selText[newCount] = newText;
  53. selValues[newCount] = newValue;
  54. selIsSel[newCount] = false;
  55. newSelected = newCount - 1;
  56. }
  57. }
  58. for(i=0; i<=newCount; i++)
  59. {
  60. var newOpt = new Option(selText[i], selValues[i]);
  61. theSel.options[i] = newOpt;
  62. theSel.options[i].selected = selIsSel[i];
  63. }
  64. }
  65. }
  66.  
  67. function addOption(theSel, theText, theValue)
  68. {
  69. var newOpt = new Option(theText, theValue);
  70. var selLength = theSel.length;
  71. theSel.options[selLength] = newOpt;
  72. }
  73.  
  74. function deleteOption(theSel, theIndex)
  75. {
  76. var selLength = theSel.length;
  77. if(selLength>0)
  78. {
  79. theSel.options[theIndex] = null;
  80. }
  81. }
  82.  
  83. function moveOptions(theSelFrom, theSelTo)
  84. {
  85.  
  86. var selLength = theSelFrom.length;
  87. var selectedText = new Array();
  88. var selectedValues = new Array();
  89. var selectedCount = 0;
  90.  
  91. var i;
  92.  
  93. // Find the selected Options in reverse order
  94. // and delete them from the 'from' Select.
  95. for(i=selLength-1; i>=0; i--)
  96. {
  97. if(theSelFrom.options[i].selected)
  98. {
  99. selectedText[selectedCount] = theSelFrom.options[i].text;
  100. selectedValues[selectedCount] = theSelFrom.options[i].value;
  101. deleteOption(theSelFrom, i);
  102. selectedCount++;
  103. }
  104. }
  105.  
  106. // Add the selected text/values in reverse order.
  107. // This will add the Options to the 'to' Select
  108. // in the same order as they were in the 'from' Select.
  109. for(i=selectedCount-1; i>=0; i--)
  110. {
  111. appendOldSchool(theSelTo, selectedText[i], selectedValues[i]);
  112. }
  113.  
  114. if(NS4) history.go(0);
  115. }
  116. /* ]]> */
  117. </script>
  118. </head>
  119.  
  120. <form action="selecty.php" method="post">
  121. <table border="0">
  122. <tr>
  123. <td>
  124. <select name="sel1" size="30" multiple="multiple" style="width: 200px;">
  125. <option value="1">Left1</option>
  126. <option value="2">Left2</option>
  127. <option value="3">Left3</option>
  128. <option value="4">Left4</option>
  129. <option value="5">Left5</option>
  130. <option value="6">Right1</option>
  131. <option value="7">Right2</option>
  132. <option value="8">Right3</option>
  133. <option value="9">Right4</option>
  134. <option value="10">Right5</option>
  135. </select>
  136. </td>
  137. <td align="center" valign="middle">
  138. <input type="button" value="--&gt;"
  139. onclick="moveOptions(this.form.sel1, this.form.sel2);" /><br />
  140. <input type="button" value="&lt;--"
  141. onclick="moveOptions(this.form.sel2, this.form.sel1);" />
  142. </td>
  143. <td>
  144. <select name="sel2" size="30" multiple="multiple" style="width: 200px;">
  145. </select>
  146. </td>
  147. </tr>
  148. <input type="button" value="Wyslij" onclick="wyslij(this.form.sel2);" />
  149. </form>
  150.  
  151. </body>
  152. </html>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.