ralphi: Formatproblem mit 'Remote autocomplete' Daten

hi leute,

bin wieder mal bei meiner musiksammlung ;-)
nun möchte ich ein suchfeld einfügen (jquerymobile autocomplete ajax) und habe dabei ein problem die Begriffe mit ajax, JS bereitzustellen.

vorlage unter: JQmobile - Remote autocomplete

also html

<ul id="autocomplete" data-role="listview" data-inset="true" data-filter="true" data-filter-placeholder="Find a song..." data-filter-theme="d"></ul>

und JS

$( document ).on( "pageinit", "#foo", function() {  
    $( "#autocomplete" ).on( "listviewbeforefilter", function ( e, data ) {  
        var $ul = $( this ),  
            $input = $( data.input ),  
            value = $input.val(),  
            html = "";  
        $ul.html( "" );  
        if ( value && value.length > 2 ) {  
            $ul.html( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>" );  
            $ul.listview( "refresh" );  
            $.ajax({  
                //url: "http://gd.geobytes.com/AutoCompleteCity",  
	        url: "autocomplete.php",  
                //dataType: "jsonp",  
	        dataType: "json",  
                //crossDomain: true,  
                data: {  
                    q: $input.val()  
                }  
            })  
            .then( function ( response ) {  
                $.each( response, function ( i, val ) {  
                    html += "<li>" + val + "</li>";  
                });  
                $ul.html( html );  
                $ul.listview( "refresh" );  
                $ul.trigger( "updatelayout");  
            });  
        }  
    });  
});	

und source mit php liefert -> array und mit
echo json_encode($json);

(auszug von ca. 10.000 songs einmal interp. vorne andermal songname)

[{"value":"+44 - Baby Come On","label":"+44 - Baby Come On _ 0"},{"value":"Baby Come On - +44","label":"Baby Come On - +44 _ 1"},{"value":"10 CC - Dreadlock holiday","label":"10 CC - Dreadlock holiday _ 2"},{"value":"Dreadlock holiday - 10 CC","label":"Dreadlock holiday - 10 CC _ 3"},{"value":"10 Ft. Ganja Plant - Invincible Butcher","label":"10 Ft. Ganja Plant - Invincible Butcher _ 4"},{"value":"Invincible Butcher - 10 Ft. Ganja Plant","label":"Invincible Butcher - 10 Ft. Ganja Plant _ 5"},{"value":"10 Ft. Ganja Plant - The Challenge","label":"10 Ft. Ganja Plant - The Challenge _ 6"},{"value":"The Challenge - 10 Ft. Ganja Plant","label":"The Challenge - 10 Ft. Ganja Plant _ 7"},{"value":"10,000 Maniacs - These Are Days","label":"10,000 Maniacs - These Are Days _ 8"},{"value":"These Are Days - 10,000 Maniacs","label":"These Are Days - 10,000 Maniacs _ 9"}]

hab auch schon probiert:
                ~~~javascript $.each( response, function ( label, value ) {
                    html += "<li>" + value + "</li>";
                });

		  
	  
als ergebnis bei eingabe ins textfeld kommt etliche male:  
[object Object]  
[object Object]  
usw  
  
die begriffe hab ich als var's - sind also mit php frei formatierbar.  
wie funktioniert's, was kann ich machen?  
gibt's auch die Möglichkeit mit text, csv etc die Begriffe zu übergeben?  
  
grüße aus LA  
ralphi
  1. Hallo,

    hab auch schon probiert:
                    ~~~javascript

    $.each( response, function ( label, value ) {

    html += "<li>" + value + "</li>";
                    });

    Lass dir mal hier statt value label ausgeben, dann verstehst du vielleicht mehr...  
      
    
    > als ergebnis bei eingabe ins textfeld kommt etliche male:  
    > [object Object]  
    > [object Object]  
    > usw  
    
    Ein Objekt wird durch geschweifte Klammern definiert. Nun schau dir noch einmal deine erhaltene Datenstruktur an. Du möchstest aus dem jeweiligen Objekt die Eigenschaften value und label auslesen.  
      
      
    vg ichbinich  
    
    -- 
    Kleiner Tipp:  
    Tofu schmeckt am besten, wenn man es kurz vor dem Servieren durch ein saftiges Steak ersetzt...
    
    1. gelöst ist es nicht - nur umgangen. verstehen tu ichs immer noch nicht ;-(

      mein test JS:

      $.each( response, function ( label, value ) {  
      	html += "<li>" + label + " wert: " + value + "</li>";  
      });
      

      ich hab jetzt mal das JSON, zum Experimentieren, in eine Textdatei gespeichert:
      nach dem ich die [] und feldnamen im json entfernt habe
      also statt:
      [{"value":"Alex Clare - Too close","label":"Alex Clare - Too close"},{"value":"Too Close - Alex Clare","label":"Too Close - Alex Clare"}]

      0 wert: [object Object]
      1 wert: [object Object]

      • ( kommt 2x - label gedreht) weil ich auch nach songnamen suchen lassen möchte)
        folgende Zeile:
        {"1":"Alex Clare - Too close","2":"Too Close - Alex Clare"}

      jetzt zeigt er:
      1 wert: Alex Clare1 - Too close
      2 wert: Alex Clare2 - Too close
      was passt

      schöner und schneller wäre es natürlich mit  json_encode() aber der macht [] klammern

      //echo json_encode($json, JSON_FORCE_OBJECT); fuzt nicht
      //$encodedArray = array_map(utf8_encode, $json); fuzt auch nicht
      echo json_encode($json); fuzt nicht - macht klammern

      für alle die ne schnelle autocomplete lösung für lokale URL-source suchen - ohne nachzudenken ,-): (JQuerymobile)
      1. beispiel kopieren unter demo Jquerymobile
      2. jsonp -> dataType: "json",
      3. //crossDomain: true, raus
      4. Json mit q als GET-var im obigen format mit echo generieren
      5. natürlich url: "deine.php", anpassen