开发者

formatting json object returned from jQuery autocomplete

开发者 https://www.devze.com 2023-01-29 12:23 出处:网络
I am using this jQuery autocomplete plugin. I want my return data to be in json format. searchPopup:function(){

I am using this jQuery autocomplete plugin.

I want my return data to be in json format.

searchPopup:function(){
     $("input.searchField").autocomplete("myjson1.json", {
      dataType: 'json',
      parse: function(data) {
        $.each(data.productList, function(k, v) {
        jQuery(".searchPanel").append("<div class="+k+"><h2>"+k+"</h2><ul class="+k+"></ul</div>");
         $.each(v, function(k1, v1) {
          $.each(v1, function(k2, v2) { 
           jQuery("ul."+k).append("<li>"+v2+"</li>");
          });
         });
       });
      },
      max: 50
     }); 

   }

When I am parsing this data, it is giving an error

c.split is not a function

How can this be fixed?

{ 
"productList" : { 
    "Byproducts" : [ 
    {
     "brand":"Bosch",
     "productname":"Jigsaw开发者_Go百科 blade",
     "price":50
    },
    {
     "brand":"Bosch1",
     "productname":"Jigsaw blade",
     "price":51
    },
    {
     "brand":"Bosch2",
     "productname":"Jigsaw blade",
     "price":52
    },
    {
     "brand":"Bosch3",
     "productname":"Jigsaw blade",
     "price":53
    }
     ],
    "Spareparts" : [ 
    {
     "brand":"BoschS1",
     "productname":"Jigsaw blade",
     "price":50
    }
   ],
    "Bybrand" : [ 
    {
     "brand":"BoschBY"
    }      
   ],
    "Bycategory" : [{
     "brand":"BoschBYc",
     "productname":"Jigsaw blade",
     "price":50
    } ]
 }
}


I favor a different way something like (using the jQueryUI plugin):

  $('input.searchField').autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "POST",
                url: "myjson1.json",
                contentType: "application/json; charset=utf-8",
                dataType: "json"
                success: function (data) {
                    response(
                    //this saves from having to make a new type to return. It's a                          standard jQuery function.
                    $.map(data.Byproducts, function (item) {
                        return {
                            label: item.productname + ' ' + item.price,
                            value: item.brand
                        }
                    }))
                }
            })
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消