简体   繁体   中英

How to display json encoded data via jquery?

I already got a jquery autocomplete textbox which is displaying data correctly, but when I add $group in MongoDB query to do select distinct, I recieve the required json array as response but in the autocomplete text view nothing is shown,

Below is my request result and the json array response from the processing page

My first query (Auto complete is populating perfectly)

$result = $collection->aggregate(array(
array(
'$match' => array(

  'Quote_Details.Quote_Catagory' => new MongoRegex("/^$q/i"),
    'Name_Status' => "P",
    'Quote_Details.Quote_Status' => "p"

 )
),
 array(
'$unwind' => '$Quote_Details'
 ),
        array(
'$match' => array(

  'Quote_Details.Quote_Catagory' => new MongoRegex("/^$q/i"),
    'Name_Status' => "P",
    'Quote_Details.Quote_Status' => "p"


  )
   ),


       array(
    '$group' => array(
        '_id' => array(
            'value1' =>'$Quote_Details.Quote_Catagory'

        )
        ) 

    ),   





  array(
  '$project' => array(

 'value' => '$Quote_Details.Quote_Catagory'
   )      
   ), 



  ));

json result

[{"_id":{"$id":"538443bf05a7d1226d000000"},"value":"Inspirational"},{"_id":{"$id":"538443bf05a7d1226d000000"},"value":"Inspirational"},{"_id":{"$id":"538443bf05a7d1226d000000"},"value":"Imagination"}]

New query(Autocomplete is populating but nothing is displaying)

$result = $collection->aggregate(array(
 array(
'$match' => array(

  'Quote_Details.Quote_Catagory' => new MongoRegex("/^$q/i"),
    'Name_Status' => "P",
    'Quote_Details.Quote_Status' => "p"

)
),
 array(
'$unwind' => '$Quote_Details'
 ),
        array(
   '$match' => array(

  'Quote_Details.Quote_Catagory' => new MongoRegex("/^$q/i"),
    'Name_Status' => "P",
    'Quote_Details.Quote_Status' => "p"


   )
 ),




 array(
    '$group' => array(
        '_id' => array(
            'value1' =>'$Quote_Details.Quote_Catagory'

        )
        )
       ), 

      ));

result json

[{"_id":{"value1":"Imagination"}},{"_id":{"value1":"Inspirational"}}]

My Jquery

$(document).ready(function(){

                $("#catagr_id").autocomplete({
                    source:'insertionauto.php',
                    minLength:1,
                    select: function (event, ui) 
                    {
                         //label = ui.item.label;
                        var value1 = ui.item.value;
                        $("#authnames").val(value);
                        console.log("hi");

                        //alert(label);
                        //alert(value);

                        $("#quote_id").autocomplete({
                    source:'autocatagquote.php?postcode='+value1,
                    minLength:1,
                    select: function (event, ui) 
                    {
                        var label1 = ui.item.label;
                        var value1= ui.item.value;

                        console.log("hi");

                        //alert(label);
                        //alert(value);

                    }


                });

                    }


                });



              });

if my guess is correct I am getting a multi array as a second result and result is not displaying in autocomplete, but why ? Please help, there must be something about grouping

So really about working with the "jqueryui" auto-complete method that expects a plain array of strings as a data-source. But let's clean up your aggregation statement first as there are some un-necessary things in there that are not helping:

$result = $collection->aggregate(array(
    array(
       '$match' => array(
           'Quote_Details.Quote_Catagory' => new MongoRegex("/^$q/i"),
           'Name_Status' => "P",
           'Quote_Details.Quote_Status' => "p"
       )
    ),
    array(
        '$unwind' => '$Quote_Details'
    ),
    array(
        '$match' => array(
            'Quote_Details.Quote_Catagory' => new MongoRegex("/^$q/i"),
            'Name_Status' => "P",
            'Quote_Details.Quote_Status' => "p"
        )
    ),
    array(
        '$group' => array(
            '_id' => '$Quote_Details.Quote_Catagory'
        )
    ),
));

So there is no reason to nest the _id value in the end "group" result. And you actually want to strip down the objects as plain strings for your returned result.

Then you are just looping through the php array:

  $myresult = array();

  for ( $i = 0; $i < sizeof( $result ); $i++ ) {
      $myresult[$i] = $result["result"][$i]["_id"];
  }

  json_encode( $myresult );

So then you just have an array of strings as required by the plugin method:

["Imagination","Inspirational"]

The point is that the plugin method included in the jqueryui API expects the results just to be an array of strings. You are returning objects with unexpected keys unless you manipulate it first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM