根据单击的按钮,在JavaScript中显示不同的卡片

icnyk63a  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(126)

我正在创建一个电子商务网站。在这个电子商务网站,我有一个产品页面。这个产品页面包含多种类型的产品。
我创建了一些独立的按钮,用户可以点击这些按钮来显示不同的产品。例如,当用户点击标题为"Xbox"的按钮时,只有平台为"Xbox"的产品才会显示出来。标题为"Xbox"的按钮的颜色也会变成红色,其他按钮的颜色也会变成白色。
我写了下面的代码:

<div id="buttons">
            <button class="button-value" onclick="filterProduct('all')">All</button>
            <button class="button-value" onclick="filterProduct('xbox')">Xbox</button>
            <button class="button-value" onclick="filterProduct('playstation-5')">PlayStation 5</button>
        </div>
        <div id="products"></div>
#buttons {
    text-align: center;
}

.button-value {
    border: 2px solid #ff0000;
    padding: 1em 2.2em;
    border-radius: 3em;
    background-color: transparent;
    color: #ff0000;
    cursor: pointer;
}

.active {
    background-color: #ff3333;
    color: #ffffff;
}
let products = {
    data: [
        {
            productName: "Alan Wake Remasterd",
            developer: "Epic Games",
            genre: "",
            pegi: "16",
            platforms: ["Xbox Series X"],
            price: "24.99",
            image: "images/images/xbox/alan-wake-remastered-xbox.jpg",
        },
        {
            productName: "Battlefield 2042",
            developer: "Electronic Arts",
            genre: "",
            pegi: "16",
            platforms: ["Xbox Series X", "PlayStation 5"],
            price: "24.99",
            image: "images/images/xbox/battlefield-2042-xbox.jpg",
        },
    ],
};

for (let i of products.data) {
    // Create Card
    let card = document.createElement("div");
    // Card should have a category and should stay hidden initialy
    card.classList.add("card", i.category, "hide");
    // image div
    let imgContainer = document.createElement("div");
    imgContainer.classList.add("image-container");
    // img tag
    let image = document.createElement("img");
    image.setAttribute("src", i.image);
    imgContainer.appendChild(image);
    card.appendChild(imgContainer);
    // Container
    let container = document.createElement("div");
    container.classList.add("container");
    // Product Name
    let name = document.createElement("h5");
    name.classList.add("product-name");
    name.innerText = i.productName.toUpperCase();
    container.appendChild(name);
    // Product Price
    let price = document.createElement("h6");
    price.innerText = "$" + i.price;
    container.appendChild(price);

    card.appendChild(container);
    document.getElementById("products").appendChild(card);
}

// Parameter passed from button (Parameter same as category)
function filterProduct(value) {
    // Button Class Code
    let buttons = document.querySelectorAll(".button-value");
    buttons.forEach((button) => {
        // Check if value equals innerText
        if (value.toUpperCase() == button.innerText.toUpperCase()) {
            button.classList.add("active");
        } else {
            button.classList.remove("active");
        }
    });

    // Select all elements
    let elements = document.querySelectorAll(".card");
    // Loop through the elements
    elements.forEach((element) => {
    // Display all cards on all button click
    if (value == "all") {
        element.classList.remove("hide");
    } else {
        // Check if element contains category class
        if (element.classList.contains(value)) {
            // Display elements based on category
            element.classList.remove("hide");
        } else {
            // Hide other elements
            element.classList.add("hide");
        }
    }
});
}


function filterProduct(value) {
    // Button Class Code
    let buttons = document.querySelectorAll(".button-value");
    buttons.forEach((button) => {
        // Check if value equals innerText
        if (value.toUpperCase() == button.innerText.toUpperCase()) {
            button.classList.add("active");
        } else {
            button.classList.remove("active");
        }
    });

此代码生成以下输出:Output of my HTML, CSS and JavaScript code
但是,当我点击标题为"Xbox"的按钮时,按钮的颜色会改变,但没有显示任何卡片。当我点击标题为"PlayStation 5"的按钮时,按钮的颜色不会改变,也没有显示卡片。
我希望颜色的"playstation 5"按钮也显示,并为所有的卡需要显示,显示。

jm2pwxwz

jm2pwxwz1#

//I didn't want to create a CSS/HTML file..
(()=>{
    let style = document.createElement("style");
    style.textContent = `
    #buttons {
        text-align: center;
    }
    
    .button-value {
        border: 2px solid #ff0000;
        padding: 1em 2.2em;
        border-radius: 3em;
        background-color: transparent;
        color: #ff0000;
        cursor: pointer;
    }
    
    .active {
        background-color: #ff3333;
        color: #ffffff;
    }
    
    .hide {
        display: none;
    }
    `;
    document.head.appendChild(style);

    let cc = document.createElement("div");
    cc.id = "products";
    document.body.appendChild(cc);

    let btns = document.createElement("div");
    btns.id = "buttons";
    document.body.appendChild(btns);
})();

let products = {
    data: [
        {
            productName: "Alan Wake Remasterd",
            developer: "Epic Games",
            genre: "",
            pegi: "16",
            platforms: ["Xbox Series X"],
            price: "24.99",
            image: "images/images/xbox/alan-wake-remastered-xbox.jpg",
        },
        {
            productName: "Battlefield 2042",
            developer: "Electronic Arts",
            genre: "",
            pegi: "16",
            platforms: ["Xbox Series X", "PlayStation 5"],
            price: "24.99",
            image: "images/images/xbox/battlefield-2042-xbox.jpg",
        },
    ],
};

//no spaces allowed in class names, filter them out with _
function platformToClassName (platform) {
    return platform.replaceAll(" ", "_");
}

//get the products container
let cardContainer = document.getElementById("products");

//get the filter buttons container
let filterButtonsContainer = document.getElementById("buttons");

//create a filter button given its textContent
function createFilterButton (platform) {
    let platformButton = document.createElement("button");
    platformButton.textContent = platform;
    platformButton.classList.add("button-value");

    platformButton.addEventListener("click", (evt)=>{
        filterProduct(platformButton.textContent);
    });
    filterButtonsContainer.appendChild(platformButton);
}

//create a card in UI
function createProductCard (product) {
    // Create Card
    let card = document.createElement("div");
    // Card should have a category and should stay hidden initialy
    card.classList.add("card", "hide");

    for (let platform of product.platforms) {
        let platformClassName = platformToClassName(platform);
        card.classList.add(platformClassName);
    }
    
    cardContainer.appendChild(card);

    // image div
    let imgContainer = document.createElement("div");
    imgContainer.classList.add("image-container");
    card.appendChild(imgContainer);

    // img tag
    let image = document.createElement("img");
    image.setAttribute("src", product.image);
    imgContainer.appendChild(image);
    
    // Container
    let container = document.createElement("div");
    container.classList.add("container");
    card.appendChild(container);
    
    // Product Name
    let name = document.createElement("h5");
    name.classList.add("product-name");
    name.innerText = product.productName.toUpperCase();
    container.appendChild(name);
    
    // Product Price
    let price = document.createElement("h6");
    price.innerText = "$" + product.price;
    container.appendChild(price);
}

//create a display of products given their object data
function populateProducts (products) {
    let platforms = new Set(); //collection of platforms given the data from products

    for (let i of products.data) {
        //collect the platforms from the product
        for (let platform of i.platforms) {
            platforms.add(platform);
        }
        console.log(i);
        createProductCard(i);
    }

    //create buttons
    createFilterButton("all");

    for (let platform of platforms) {
        createFilterButton(platform);
    }
}

//actually do the operation
populateProducts(products);

// Parameter passed from button (Parameter same as category)
function filterProduct(value) {

    // Select buttons
    let buttons = document.querySelectorAll(".button-value");
    // Loop through them
    for (let button of buttons) {
        
        // Check if value equals innerText
        let VALUE = value.toUpperCase();
        let BUTTON_TEXT = button.textContent.toUpperCase();
        console.log(VALUE, BUTTON_TEXT);

        if (VALUE == BUTTON_TEXT) {
            button.classList.add("active");
        } else {
            button.classList.remove("active");
        }
    }

    //class names can't have spaces, convert it
    value = platformToClassName(value);

    // Select card elements
    let elements = document.querySelectorAll(".card");
    // Loop through them
    for (let element of elements) {
        
        // update which cards are shown based on what filtered value is
        if (value == "all" || element.classList.contains(value)) {
            element.classList.remove("hide");
        } else {
            element.classList.add("hide");
        }

    }
}

这是粘贴到控制台,如果你想尝试一下。

相关问题