Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

2 Stron V   1 2 >  
Reply to this topicStart new topic
> [PHP][JavaScript]jquery autocomplete -własne parametry
rafik73
post
Post #1





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

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


Witam
Korzystam z najnowszego pluginu
jquery autocomplete, wszystko chodzi fajnie,
dane pobieram z pliku z zapytaniem MySQL.
Używam standardowych parametrów: label i value
Potrzebuje dodatkowych parametrów, np.: o nazwie id.
Istnieje taka możliwość, czy tylko te dwa
parametry przypisane na stałe?
Czyli...
Mam kolumny id_osoby, name, miasto.
Chcę przesłać odpowiednio za pomocą parametrów :
label: name,miasto
value: name
id: id_osoby


Ten post edytował rafik73 23.10.2015, 07:25:00
Go to the top of the page
+Quote Post
Pyton_000
post
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


A wiesz ile jest takich skryptów do autocomplete?
Go to the top of the page
+Quote Post
rafik73
post
Post #3





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

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


No właśnie sęk w tym, że nie mogłem znaleźć
rozwiązania w necie.
Go to the top of the page
+Quote Post
kapslokk
post
Post #4





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


Myślę, że Pytonowi po prostu chodzi o to, że jak podasz konkretnie którego autocomplete'ra używasz, będzie znacznie łatwiej Ci pomóc wink.gif
Go to the top of the page
+Quote Post
rafik73
post
Post #5





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

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


Sorki
Chodzi o najnowszy jquery ui 1.11.4

Ten post edytował rafik73 23.10.2015, 09:00:51
Go to the top of the page
+Quote Post
Pyton_000
post
Post #6





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


http://stackoverflow.com/questions/7292462...mplete-response
Go to the top of the page
+Quote Post
rafik73
post
Post #7





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

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


  1. <?php
  2.  
  3.  
  4. $q=$_GET["term"];
  5. if (!$q) return;
  6.  
  7. mysql_query("SET NAMES 'utf8'");//polskie znaki
  8. $query = mysql_query("SELECT * FROM baza WHERE miasto LIKE '$q%' ORDER BY miasto") or die(i'Błąd zapytania');
  9.  
  10.  
  11. $json=array();
  12.  
  13. while($z=mysql_fetch_array($query)){
  14. $json[]=array(
  15. "value"=> $z['name],
  16. "label"=>$z['miasto'],
  17. "id"=>$z['id_osoby'] // to chcę dodać
  18.  
  19. );
  20. }
  21.  
  22. echo json_encode($json);
  23. ?>



Ten post edytował rafik73 23.10.2015, 10:03:27
Go to the top of the page
+Quote Post
Pyton_000
post
Post #8





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


no super, Ty powiedziałeś co chcesz, ja dałem rozwiązanie... A gdzie to chcesz dodać?
Go to the top of the page
+Quote Post
rafik73
post
Post #9





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

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


id ma być przesłane do formularza,do hidden

[JAVASCRIPT] pobierz, plaintext
  1. <script type="text/javascript">// <![CDATA[
  2. $(function() {
  3.  
  4. $("#search").autocomplete({
  5. source: "search.php",minLength:1,
  6. focus: function(event, ui) {
  7. // prevent autocomplete from updating the textbox
  8. event.preventDefault();
  9. // manually update the textbox
  10. $(this).val(ui.item.label);
  11. },
  12. select: function(event, ui) {
  13. // prevent autocomplete from updating the textbox
  14. event.preventDefault();
  15. // manually update the textbox and hidden field
  16. $(this).val(ui.item.label);
  17. $("#id_search").val(ui.item.value);
  18. }
  19. });
  20. });
  21.  
  22. // ]]></script>
[JAVASCRIPT] pobierz, plaintext

Go to the top of the page
+Quote Post
Pyton_000
post
Post #10





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


$("#id_search").val(ui.item.value);

zmień value na klucz id i tyle.
Go to the top of the page
+Quote Post
rafik73
post
Post #11





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

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


Też tak myślałem ale tak nie działa,mam wrażenie, że value
i label są na stałe zapisane w skrypcie jquery
Go to the top of the page
+Quote Post
Pyton_000
post
Post #12





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


console.log(ui.item)
Go to the top of the page
+Quote Post
rafik73
post
Post #13





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

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


Sprawdzę po 15,bo teraz z pracy ze smartfona piszę :-)

To była literówka, działa, dzięki wielkie !
Ale to nie koniec problemu....mam problem z wyświetlaniem tych danych w pętli. na stronie wyświetlam zawartość bazy danych
w formularzu do edycji, która jest w pętli while.
Skrypt szukania:
  1. <?php
  2.  
  3.  
  4. $q=$_GET["term"];
  5. if (!$q) return;
  6.  
  7. mysql_query("SET NAMES 'utf8'");//polskie znaki
  8. $query = mysql_query("SELECT name,surname,id,miasto FROM miasta WHERE surname LIKE '$q%' ORDER BY surname ASC") or die('Błąd zapytania');
  9.  
  10.  
  11. $json=array();
  12.  
  13. while($s=mysql_fetch_array($query)){
  14.  
  15. $json[]=array(
  16. "value"=> $s['surname'].' '.$s['name'],
  17. "label"=>$s['surname'].' '.$s['name'].' '.$s['miasto'],
  18. "id"=>$s['id'] ,
  19. "miasto"=>$s['miasto']
  20.  
  21. );
  22.  
  23. }
  24.  
  25. echo json_encode($json);
  26. ?>


odbieram:

[JAVASCRIPT] pobierz, plaintext
  1. <script type="text/javascript">
  2. $(function() {
  3.  
  4. $(".search").autocomplete({
  5.  
  6. source: "search.php",minLength:2,
  7. focus: function(event, ui) {
  8. // prevent autocomplete from updating the textbox
  9. event.preventDefault();
  10. // manually update the textbox
  11. $(this).val(ui.item.label);
  12. },
  13. select: function(event, ui) {
  14. // prevent autocomplete from updating the textbox
  15. event.preventDefault();
  16. // manually update the textbox and hidden field
  17. $(this).val(ui.item.value);
  18. $(".id_search").val(ui.item.id);
  19. $(".id_miasto_search").val(ui.item.miasto);
  20. }
  21. });
  22. });
  23.  
  24. </script>
[JAVASCRIPT] pobierz, plaintext


Wysyłam do formularza w pętli, wyświetla się powiedzmy 5 rekordów.
Problem w tym, że wysyła te same dane do textboxa o klasie 'id_miasto_search' oraz 'id_search'
Nie traktuje pojedyńczego rekordu tylko masowo.

czyli mamy:

  1. while{
  2. textbox1 class="id_search";
  3. textbox2 class="id_miasto_search";
  4. }


Go to the top of the page
+Quote Post
Pyton_000
post
Post #14





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Nie do końca rozumiem. albo to wina pory.
Go to the top of the page
+Quote Post
rafik73
post
Post #15





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

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


  1. // tu zapytanie mysql, $query
  2. while($a=mysql_fetch_array($query)){
  3. echo'<input type="text" class="search" name="nazwisko" value=".'$a ['name']">';
  4. echo'<input type="text" class="id_search" name="id" value=".'$a ['id']">';
  5. echo'<input type="text" class="id_miasto_search" name="miasto" value=".'$a ['id_miasto']">';
  6. }


Kiedy edytuje textbox o klasie search, to wartości id_search oraz id_miasto_search są przekazywane do wszystkich rekordów w pętli a nie tylko do edytowanego.

Ten post edytował rafik73 23.10.2015, 21:56:34
Go to the top of the page
+Quote Post
Pyton_000
post
Post #16





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Ujmij inputy w np. div i potem przypisuj wartości na zasadzie:

$(this).closest('div').find(".id_miasto_search").val(ui.item.miasto);
Go to the top of the page
+Quote Post
rafik73
post
Post #17





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

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


Nie działa, za to dla klasy 'id_search' zadziałało:

[JAVASCRIPT] pobierz, plaintext
  1. $(this).parent().parent().parent().find(".id_search").val(ui.item.id);
[JAVASCRIPT] pobierz, plaintext


zaś dla klasy 'id_miasto_search' nie działa. fakt, że input z tą klasą nie jest obok inputa 'id_search', tylko pod innymi divami, w
innej części formularza, ale nie mogę go namierzyć parentami, dopiero gdy dorzucę jeden parent, czyli:

[JAVASCRIPT] pobierz, plaintext
  1. $(this).parent().parent().parent()..parent()find(".id_miasto_search").val(ui.item.miasto);
[JAVASCRIPT] pobierz, plaintext


to się wyświetla ale niestety we wszystkich rekordach sad.gif

Ten post edytował rafik73 24.10.2015, 10:47:15
Go to the top of the page
+Quote Post
trueblue
post
Post #18





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


W każdym .search przechowuj w atrybucie np. data-id-search oraz data-id-miasto, id pól zależnych (im nadaj unikalne id).
W metodzie select pobierz obydwa atrybuty z elementu .search, dzięki temu nie będziesz musiał przeszukiwać struktury.


--------------------
Go to the top of the page
+Quote Post
rafik73
post
Post #19





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 19.03.2014

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


Można prosić bardziej łopatologicznie? biggrin.gif
Go to the top of the page
+Quote Post
trueblue
post
Post #20





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


Kiedy tworzysz elementy w pętli nadaj im id, np. id="search1" i id="miasto1", i tak po kolei.
W elemencie .search powiązanym z tymi elementami dodaj atrybuty: data-id1="search1" i data-id2="miasto1".
W metodzie select pobierz wartości atrybutów (.attr('data-id1') .attr('data-id2')), pobrane wartości wskażą na powiązane elementy, który chcesz uzupełnić.


--------------------
Go to the top of the page
+Quote Post

2 Stron V   1 2 >
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 Aktualny czas: 19.08.2025 - 19:51