【问题标题】:Show search hits显示搜索结果
【发布时间】:2014-10-28 13:35:37
【问题描述】:

我正在尝试学习 JavaScript,所以我知道这不是网上商店的最佳解决方案,但它仅用于学习。 我正在做一个搜索功能,你搜索一个类别,如果你有匹配,结果就会显示出来。我被困在最后一部分。我将如何编写代码以显示结果?

这是我的代码:(不要介意 showItem 函数,还没有开始那个)

$(document).ready(function() {
var gallery = [
        {
            "img" : "img/gallery/gameboy2.png",
            "title" : "GameBoy Color [yellow]",
            "about" : "A-ball",
            "price" : 99,
            "category" : ["Gameboy", "color", "Console", "game"]

        },
        {
            "img" : "img/gallery/phone.png",
            "title" : "Hamburger Phone",
            "about" : "What is a smartphone?",
            "price" : 129,
            "category" : ["phone","hamburger"]
        },
        {
            "img" : "img/gallery/gameboy.png",
            "title" : "Nintendo GameBoy",
            "about" : "Things doesnt get better with time. This is the shit.",
            "price" : 499,
            "category" : ["Game","Console", "Gameboy"]
        },
        {
            "img" : "img/gallery/game2.png",
            "title" : "SEGA",
            "about" : "Things doesnt get better with time. This is the shit.",
            "price" : 699,
            "category" : ["Game","Console", "SEGA"]
        },
                    {
            "img" : "img/gallery/gameboy2.png",
            "title" : "GameBoy Color [yellow]",
            "about" : "A-ball",
            "price" : 99,
            "category" : ["Gameboy", "color", "Console", "game"]

        },
        {
            "img" : "img/gallery/phone.png",
            "title" : "Hamburger Phone",
            "about" : "What is a smartphone?",
            "price" : 129,
            "category" : ["phone","hamburger"]
        },
        {
            "img" : "img/gallery/gameboy.png",
            "title" : "Nintendo GameBoy",
            "about" : "Things doesnt get better with time. This is the shit.",
            "price" : 499,
            "category" : ["Game","Console", "Gameboy"]
        },
        {
            "img" : "img/gallery/game2.png",
            "title" : "SEGA",
            "about" : "Things doesnt get better with time. This is the shit.",
            "price" : 699,
            "category" : ["Game","Console", "SEGA"]
        }

 ];

 var search = document.getElementById("submit_search");
 search.addEventListener("click", searchItem);

 showItemsList();


 /*
     Create a li element and append to already existing ul
     Show an image of the product, and below the image show product title and price
 */

 function showItemsList() {

var ul = document.getElementById("product_list");

for(i =0; i < gallery.length; i++) {

    // get the current product
    var currentProduct = gallery[i];

    // create element li 
    var li = document.createElement('li');                           

    // create product img
    var productImg = document.createElement('img');
    productImg.src = currentProduct.img;
    productImg.className = "thumbnail";
    li.appendChild(productImg); 

    // create caption
    li.appendChild(document.createTextNode(currentProduct.title + " " + currentProduct.price + "kr"));                                         
    ul.appendChild(li);
}

}

/*
    If someone click on a product, show a larger picture with a caption telling about the product
    If you click the picture again, it minimize back to a thumbnail

*/
function showItem() {

}

/*
    A searchfield where you can choose between already existing categories. 
    Show only those items that been search for
*/
function searchItem() {
    var ul = document.getElementById("product_list");
    var search = document.getElementById("search").value;
    for(var x = 0; x < gallery.length; x++){
       //Get the current product
       var currentProduct = gallery[x];

       //get the current product categories
       var currentProductCategories = currentProduct.category;

       //Loop through each category
       for(var z = 0; z < currentProductCategories.length; z++){

        //Check if the category is the one we are looking for
        if(currentProductCategories[z] == search){

        }
    }
}
}

});

【问题讨论】:

    标签: javascript arrays search


    【解决方案1】:

    让您的showItemsList 方法接受您的列表作为参数:

    function showItemsList(items) {
    
    var ul = document.getElementById("product_list");
    ul.innerHTML = "";
    for(i =0; i < items.length; i++) {
    
        // get the current product
        var currentProduct = items[i];
    
        // create element li 
        var li = document.createElement('li');                           
    
        // create product img
        var productImg = document.createElement('img');
        productImg.src = currentProduct.img;
        productImg.className = "thumbnail";
        li.appendChild(productImg); 
    
        // create caption
        li.appendChild(document.createTextNode(currentProduct.title + " " + currentProduct.price + "kr"));                                         
        ul.appendChild(li);
    }
    
    }
    

    现在你的searchItem 方法可以使用它了:

    function searchItem() {
        var matches = [];
        var ul = document.getElementById("product_list");
        var search = document.getElementById("search").value;
        for(var x = 0; x < gallery.length; x++){
           //Get the current product
           var currentProduct = gallery[x];
    
           //get the current product categories
           var currentProductCategories = currentProduct.category;
    
           //Loop through each category
           for(var z = 0; z < currentProductCategories.length; z++){
    
            //Check if the category is the one we are looking for
            if(currentProductCategories[z] == search){
               matches.push(currentProduct);
            }
           }
        }
        showItemsList(matches);
    }
    

    【讨论】:

    • 我试过这个并且搜索功能有效,但是当我加载页面时,画廊没有显示以......
    • 是的,当然你必须在开头用showItemsList(gallery); 替换showItemsList(); 来传递你的数组...
    • 我也这样做了,它说 Uncaught TypeError: Cannot read property 'length' of undefined 谈论这段代码for(i =0; i &lt; items.length; i++) {
    • 确保在调用showItemsList(gallery);之前设置了gallery变量
    • 它设置在代码的开头,在调用 showItemsList 之前。我应该把你所有的 list 改成 gallery 吗?因为这就是我所做的......