Display different images based on user selection in vue.js

I am new to working with vue.js and I'm facing a challenge. My goal is to display images of NBA players based on the selected criteria using vue.js. For instance, if the Dunk contest champion is chosen, only images of Kobe and Jordan should be displayed. To determine which player's image to show, the data in an object with key-value pairs known as properties needs to be checked, containing specific conditions for each NBA player.

I have initialized a variable called checkstate set to false. The objective is to switch it to true once the condition is satisfied. I have implemented an onChange method for this purpose. While I am able to display the images and populate the selection element with data from the key-value objects, I am struggling to make the selection element and the images work together.

Any suggestions or solutions?

<head>
    <style>
     a.incognito{
    display: none;
        }
        </style>
</head>
<body>
        <h2>NBA players</h2>
        <p>Explore the top NBA players</p>

        <div id="app">
            <select  @change="onChange()" v-model="selected">
                <option value="none">Select Filter</option>
                <option v-for="property in properties" :value="property.filter"> {{property.filter}}</option>
            </select>

            <div class="portfolio">
                <a v-for="player in players" :href="player.href" 
                    :class="{incognito: checkstate}" :playername="player.name">
                    <img :src="player.src" alt="" >
                </a>
            </div>
        </div>
  
    <script src="https://unpkg.com/vue@3"></script>


    <script>
        let app = Vue.createApp({
            data() {
                return {
                    players: [{ name: "Mike", href: "https://en.wikipedia.org/wiki/Michael_Jordan", src: "/Test/Michael_Jordan_in_2014.jpg" },
                    { name: "Kobe", href: "https://en.wikipedia.org/wiki/Kobe_Bryant", src: "/Test/Kobe_Bryant_2014.jpg" },
                    { name: "Lebron", href: "https://en.wikipedia.org/wiki/LeBron_James", src: "/Test/LeBron_James_-_51959723161_(cropped).jpg" },
                    { name: "Kd",  href: "https://en.wikipedia.org/wiki/Kevin_Durant", src: "/Test/Kevin_Durant_(Wizards_v._Warriors,_1-24-2019)_(cropped).jpg" },
                    ],

                    properties: [{filter: "Guard position",Mike:true,Kobe:true,Kd:false,LeBron:false},
                    { filter: "Foward position",Mike:false,Kobe:false,Kd:true,LeBron:true},
                    { filter: "Multiple Championships",Mike:true,Kobe:true,Kd:true,LeBron:true},
                    { filter: "Multiple MVP's",Mike:true,Kobe:false,Kd:false,LeBron:true},
                    { filter: "Over 6'8",Mike:false,Kobe:false,Kd:true,LeBron:true},
                    { filter: "Dunk contest champion",Mike:true,Kobe:true,Kd:false,LeBron:false}],
                    
                    selected: 'none',                  
                    checkstate:false,
                }
            }, methods: {
                onChange() {
                    const attr = this.properties.find((item) => item.filter === this.selected)

                        checkstate=this.properties[this.playername] 
                },
            },
                props:['playername']
            , 
        })
        app.mount('#app')
        
    </script>

</body>

Answer №1

<!DOCTYPE html>
 <html lang="en">
 
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Awesome Website</title>
     <style>
         a.incognito {
             display: none;
         }
     </style>
 </head>
 
 <body>
     <h2>Top NBA Players</h2>
     <p>Discover the incredible talent in the NBA</p>
 
     <div id="app">
         <select v-model="selected" >
             <option value="none">Select filter</option>
             <option v-for="property in properties" :value="property"> {{property}}</option>
         </select>
         
         <div >   
             <a  v-for="player in players"  :href="player.href" :class="{incognito: !player[selected]}" >
                 <img :src="player.src" alt="">
             </a>
             
         </div>
     </div>
 
     <script src="https://unpkg.com/vue@3"></script>
 
 
     <script>
         let app = Vue.createApp({
             data() {
                 return {
                     players: [{ name: "Magic Johnson", ID: "0", href: "https://en.wikipedia.org/wiki/Magic_Johnson", src: "/Images/Magic_Johnson.jpg", GuardPosition: true, ForwardPosition: false, MultipleChampionships: true, MultipleMVP: true, Over6foot8: false, DunkContestChampion: true },
                     { name: "Larry Bird", ID: "1", href: "https://en.wikipedia.org/wiki/Larry_Bird", src: "/Images/Larry_Bird.jpg", GuardPosition: true, ForwardPosition: false, MultipleChampionships: true, MultipleMVP: false, Over6foot8: false, DunkContestChampion: true },
                     { name: "Tim Duncan", ID: "2", href: "https://en.wikipedia.org/wiki/Tim_Duncan", src: "/Images/Tim_Duncan.jpg", GuardPosition: false, ForwardPosition: true, MultipleChampionships: true, MultipleMVP: true, Over6foot8: true, DunkContestChampion: false },
                     { name: "Shaquille O'Neal", ID: "3", href: "https://en.wikipedia.org/wiki/Shaquille_O%27Neal", src: "/Images/Shaquille_ONeal.jpg", GuardPosition: false, ForwardPosition: true, MultipleChampionships: true, MultipleMVP: false, Over6foot8: true, DunkContestChampion: false },
                     ],
 
                   properties: ["GuardPosition", "ForwardPosition", "MultipleChampionships", "MultipleMVP", "Over6foot8", "DunkContestChampion"],
 
                     selected: 'none',
                     checkstate: false,
                 }
             }
         })
         app.mount('#app')
 
     </script>
 
 </body>
 
 </html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a dynamic dropdown list with PHP and AJAX using JQuery

I was attempting to create a dynamic dependent select list using AJAX, but I am facing issues with getting the second list to populate. Below is the code I have been working with. The gethint.php file seems to be functioning properly. I'm not sure whe ...

Safari not centering element with justify-self in CSS grid

My challenge involves adjusting the alignment of a div (mobile menu) that is currently centered using css grid and the justify-self property in chrome. However, when viewed in Safari, it appears on the left side of the screen behind the Instagram icon: ht ...

MERN stack does not have a defined onClick function

I am developing a website for college registration, and I encountered an issue while trying to create a feature that allows students to select a major and view all the associated courses. I made a list of majors with buttons next to them, but whenever I at ...

Maximizing the potential of PJAX with Express and EJS: A guide

Hey there, question coming from a newbie in the world of coding. I've been struggling to make pjax work for quite some time now without any success. The closest I've come is seeing some activity in the terminal, but when I click on the link, it ...

Tips for utilizing .env variables within npm scripts

I have set the following variable in my .env file: SHOPIFY_STORE_URL=mystore.myshopify.com Now, I am looking to utilize this variable in an npm script like so: "scripts": { "shopify-login": "shopify login --store=SHOPIFY_STO ...

Attempting to recreate the dynamic banner featured in the video

Looking to replicate a setup similar to what was demonstrated in the video. I have two div blocks - one with a random image and the other with a video, and I want them to be as flexible and identical as possible to the video layout. How should I go about a ...

How can one quickly display a confirmation dialog box within react-admin?

Looking for a way to implement a confirmation dialog in my react-admin project, similar to JavaScript's alert function but more visually appealing. I want the user to only proceed with certain database operations if they click OK on the dialog. I cou ...

The Hyperledger Sawtooth JavaScript SDK has encountered invalid submitted batches

I am currently working on integrating a hyperledger sawtooth transaction using the javascript SDK. I am following the tutorial provided here: . /* *Create the transaction header */ const createTransactionHeader = function createTransactionHeader(payloadBy ...

Identifying modifications to the content of a text area

Is there a way to determine if the value in my textareaId has changed due to JavaScript? For example: $("#buttonID").on("click, function(){ $("#textareaID").val("lorem ipsum"); }); $("#textareaID").change(function(){ //not working }); $ ...

Ensure the beginning and ending times are accurate for newly added input fields using jQuery

I've included a JSFIDDLE link here for reference. The validation for start and end time kicks in when the 'Next' button is clicked, located on the same page. I'm looking to validate the start and end times for dynamically generated fiel ...

Navigating through paginated data can be made easier by using Backbone.PageableCollection in conjunction with a personalized

I have developed an application that interacts with a database known as LeanCloud. Currently, I have configured a page to display all the users stored in the database. However, LeanCloud has a limitation where only 100 pieces of data can be retrieved per r ...

"Unlocking the potential of JSON: A guide to retrieving and displaying three specific fields in

My PHP service is returning the following data: [[1,"16846"],[2,"16858"],[3,"16923"],[4,"16891"]] Within my HTML, I have ajax set up to fetch this information; $.ajax({ type: 'POST', url: 'getDadosGrafico.php', ...

I am having trouble understanding why dropdown menus with the same content have varying widths

My issue is illustrated in the screenshots below: first-dropdown: second-dropdown: The second dropdown appears slightly wider on the right side. Despite Google Chrome's claim that the widths are the same. HTML code: <div class="row menu"> ...

I am looking to generate an array containing sub arrays so that I can easily iterate through the JSON data

I'm relatively new to creating subarrays and PHP, so please bear with me. I have some code that generates a JSON array, which is shown below. foreach ($result as $row) { $points = $row['points']; $price = ...

The tooltip menu options in material ui are displaying in a different location in the window rather than below the button icon. How can this issue be resolved?

I'm having trouble implementing a tooltip on an option menu icon and it's not working as expected. Here is my code: export default function FadeMenu() { const [ anchorEl, setAnchorEl ] = React.useState(null) const bus = Bus() const o ...

Issue: Connection Problem in React, Express, Axios

I've encountered an issue while attempting to host a website on an AWS EC2 instance using React, Express, and Axios. The specific problem I'm facing is the inability to make axios calls to the Express back-end that is running on the same instanc ...

What is the best method for testing routes implemented with the application router in NextJS? My go-to testing tool for this is vitest

Is it possible to test routes with vitest on Next.js version 14.1.0? I have been unable to find any information on this topic. I am looking for suggestions on how to implement these tests in my project, similar to the way I did with Jest and React Router ...

Update the content of an HTML element without having direct access to the HTML code

I am currently in the process of creating a website using a website builder, and I am interested in customizing the default message that is displayed when a required field in the website form is left empty. The form in question is an integral part of the w ...

Learn how to incorporate an image into your Codepen project using Dropbox and Javascript, along with a step-by-step guide on having the picture's name announced when shown on the screen

Hey there, I'm in need of some assistance. I have a Codepen page where I am working on displaying 4 random shapes (Square, Triangle, Circle, and Cross) one at a time, with the computer speaking the name of each shape when clicked. I want to pull these ...

showing text style that is only specified on the server, not by the client

I am in the process of creating a new website that offers SMS services through clickatell. This website includes various font families that may not be defined on the client's computer. For instance, if the site is accessed from a different computer, t ...