有条件地按键显示结果

时间:2017-03-29 20:50:46

标签: javascript jquery json

鉴于以下样本数据,我需要按类型显示结果:

{
    "count": 50,
    "data": [{
        "id": "title",
        "image": "https://url-to-image.jpg",
        "permalink": "/folder/bob/",
        "text": "lots of text",
        "title": "Bob",
        "type": "User"
    }, {
        "id": "title2",
        "image": "https://url-to-image2.jpg",
        "permalink": "/folder/joe/",
        "text": "lots of text here",
        "title": "Joe",
        "type": "Article"
    }, {
        "id": "title3",
        "image": "https://url-to-image3.jpg",
        "permalink": "/attorneys/jim/",
        "text": "lots of text here",
        "title": "Jim",
        "type": "Blog"
    }]
}

我正在尝试运行一个条件,如果类型是某个类型,则显示这些帖子:

$("#searchterm").keyup(function(e) {
   var q = $("#searchterm").val();
   //Only begin search with at least 3 characters.
   if(q.length > 3){
      ajax_search();
   }
   //Send search query
   function ajax_search(){
   $.getJSON("http://url-to-search?q=" + q, {
            dataType:'jsonp'
     },
     //Get results and make 'em look good
     function(data) {
       console.log(data);
       $(".results").empty();
      //  $(".results").append("Results for <b> " + q + "</b>");
         $.each(data.data, function(i, data) {
                     if(data.type === 'Article'){
             $(".articles").append("<li><a href=localhost:4000" + data.permalink + ">" + data.title + "</a></li>");
                     } else if(data.type === 'Blog'){
                         $(".blog").append("<li><a href=localhost:4000" + data.permalink + ">" + data.title + "</a></li>");
                     }
         });
      });
    }
});

但是,结果是空的,尽管没有条件语句会有结果。

最后,结果应如下所示:

文章

博客

吉姆

2 个答案:

答案 0 :(得分:0)

我猜你想要这样的东西:

cv::Mat colorFrame(cv::Size(width, height),CV_8UC3,pointerToMemoryOfCamera);
var result = {
    "count": 50,
    "data": [{
        "id": "title",
        "image": "https://url-to-image.jpg",
        "permalink": "/folder/bob/",
        "text": "lots of text",
        "title": "Bob",
        "type": "User"
    }, {
        "id": "title2",
        "image": "https://url-to-image2.jpg",
        "permalink": "/folder/joe/",
        "text": "lots of text here",
        "title": "Joe",
        "type": "Article"
    }, {
        "id": "title3",
        "image": "https://url-to-image3.jpg",
        "permalink": "/attorneys/jim/",
        "text": "lots of text here",
        "title": "Jim",
        "type": "Blog"
    }]
};

var filteredView = function(data,type,divclass){
  var filteredResults = $.grep(data, function(d){ return d.type == type; });
  var container = $("<div class='"+divclass+"'></div>").appendTo("#results");
  for(i in filteredResults){
     container.append("<div><h3>" + filteredResults[i].title + "</h3><p>" + filteredResults[i].text + "</p></div>");
  }
};

filteredView(result["data"],"Article","article");
filteredView(result["data"],"Blog","blog");
filteredView(result["data"],"User","user");

答案 1 :(得分:0)

事实证明,问题是我设置的溢出能够滚动浏览结果被设置为错误的类。