Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Przenoszenie elementów pomiędzy selectami, no właśnie, jak to zrobić ?!
tiraeth
post 6.04.2005, 12:25:30
Post #1





Grupa: Przyjaciele php.pl
Postów: 1 789
Pomógł: 41
Dołączył: 30.10.2003
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


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ć...

Ten post edytował Tiraeth 6.04.2005, 12:33:24
Go to the top of the page
+Quote Post
revyag
post 6.04.2005, 13:29:44
Post #2





Grupa: Przyjaciele php.pl
Postów: 2 258
Pomógł: 16
Dołączył: 21.09.2004
Skąd: Kielce

Ostrzeżenie: (0%)
-----


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.


--------------------
-------------

------
Go to the top of the page
+Quote Post
ganimedes
post 6.04.2005, 14:06:59
Post #3





Grupa: Zarejestrowani
Postów: 66
Pomógł: 0
Dołączył: 1.04.2004

Ostrzeżenie: (0%)
-----


Fajne działa, napewno przyda się smile.gif) Dzięki
Go to the top of the page
+Quote Post
tiraeth
post 6.04.2005, 14:30:18
Post #4





Grupa: Przyjaciele php.pl
Postów: 1 789
Pomógł: 41
Dołączył: 30.10.2003
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


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);
}


Ten post edytował Tiraeth 6.04.2005, 14:43:39
Go to the top of the page
+Quote Post
cerastes
post 13.04.2005, 23:32:58
Post #5





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 13.04.2005
Skąd: Zielona Góra / Wrocław

Ostrzeżenie: (0%)
-----


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
Go to the top of the page
+Quote Post
revyag
post 14.04.2005, 08:38:12
Post #6





Grupa: Przyjaciele php.pl
Postów: 2 258
Pomógł: 16
Dołączył: 21.09.2004
Skąd: Kielce

Ostrzeżenie: (0%)
-----


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')">


--------------------
-------------

------
Go to the top of the page
+Quote Post
cerastes
post 14.04.2005, 22:29:32
Post #7





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 13.04.2005
Skąd: Zielona Góra / Wrocław

Ostrzeżenie: (0%)
-----


dzieki bardzo smile.gif
Go to the top of the page
+Quote Post
phpion
post 1.11.2006, 10:54:19
Post #8





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




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>


Ten post edytował phpion.com 1.11.2006, 11:44:55
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 1.07.2025 - 14:50