Using VueJS to access dynamic component data within a method executed through v-bind

I am currently facing a unique challenge and finding it difficult to come up with a solution.

My project involves creating a file manager-like feature in Vue, where I want specific folders or files to display custom thumbnails. For example, showing the Creative Cloud logo if the 'Creative Cloud' folder is detected. Each file is represented by a component in my app.

The code for the file-grid Vue file may seem messy as I've been experimenting with different solutions:

<template>
  <div id="localMain">
    <div id="filesGrid">
      <File :fileName="file"
      :imageAddress="findImage($event)" 
      id="file" 
      v-for="file in files" 
      :key="file.id"></File>
    </div>
  </div>
</template>

<script>
import File from './LocalMain/File';

export default {
  data() {
    return {
      creativeCloud: 'static/logos/creative-cloud.svg',
      blankThumb: 'static/code.svg',
      files: [
        'Creative Cloud',
        'Documents',
        ...
      ],
    };
  },
  components: {
    File,
  },
  methods: {
    findImage: function findImage(e) {
      /* Determine the appropriate thumbnail based on the file/folder name */
      const name = e.target.dataset.fileName;
      let image = this.blankThumb;
      if (name === 'Creative Cloud') {
        image = this.creativeCloud;
      } else {
        image = this.blankThumb;
      }
      return image;
    },
  },
};
</script>

<style scoped>
/* styling */
</style>

This is how the file component is structured:

<template>
  <div id="file">
    <img :src="imageAddress" alt="Logo" id="fileImg" />
    <h3 v-if="display">{{ fileName }}</h3>
  </div>
</template>

<script>
export default {
  data() {
    return {
      display: false,
    };
  },
  props: {
    fileName: String,
    imageAddress: String,
  },
};
</script>

<style scoped>
/* styling */
</style>

Although my question might be unclear, I hope you can help me navigate through this confusion.

Answer №1

It seems like a simple solution would be to use v-bind on the method with the file name as the argument.

For example:

In the parent template:

<File :fileName="file"
  :imageAddress="findImage(file)" 
  id="file" 
  v-for="file in files" 
  :key="file.id"></File>

In the parent Javascript:

findImage: function findImage(name) {

  var image = this.blankThumb;
  if (name === 'Creative Cloud') {
    image = this.creativeCloud;
  }
  return image;
},  

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

Stuck with the same icon even after a successful AJAX call

I am currently working on implementing a 'add to my list' function in my web app. The goal is to change the color of an icon when a user clicks on it, after sending the necessary data to the server. Despite successfully sending the data to the s ...

Unitary Individuals and the Connection Automation

It is advised to refrain from using the deprecated com.sun.star.frame.Desktop type and opt for the com.sun.star.frame.theDesktop singleton instead. Various programming languages provide access to singletons. For instance, in Java, this thread mentions the ...

Issue on Heroku with Discord.js displaying a "Service Unavailable" message

Encountered a strange error while trying to launch my discord bot on heroku. Previously, I implemented a command handler for the bot to organize commands in separate files but faced multiple code errors. With the help of a member from this community, all e ...

What is the best way to display the JSON data?

<!DOCTYPE HTML> <html> <head> <title></title> <link href="/bundles/hoaxpartner/css/style.css" type="text/css" rel="stylesheet" /> </head> <body> <div id="header">Backbone</div> &l ...

Is there a way to deactivate keyboard input on an HTML number input field? How about in a React or Material-UI environment?

I am working with an <input> tag that has the attribute type="number", and I want to disable keyboard input so that users are required to adjust the value using the spinner (up and down arrows). This will allow me to consume the input value on each c ...

Angular-datatables has a scroller function that allows for easy navigation within the content of

I've been scouring the web for hours, but I can't seem to find a solution. Can someone please assist me in resolving this issue? Issue: I integrated angular-datatables within an md-tab using Angular material. Everything was running smoothly unti ...

Individualize participants and thwart repetition

I need help separating 3 radio players using HTML code and ensuring they remain distinct entities. What code should I use between them to achieve this? For example, all 3 radio players end up looking like the last one at the bottom if not separated correc ...

Troubleshooting MySQL Database Insertion Errors caused by Dynamic Forms

<body> <?php $con = mysqli_connect('localhost','root','','cash'); $query = "SELECT DISTINCT category FROM cash"; $result = mysqli_query($con,$query); $dropDownList = &apo ...

Modify the state of an individual item within an array

I have a list of items that I need to show a loader for and hide it once a certain action is completed. For instance, below is my array of items: [ { "id": "69f8f183-b057-4db5-8c87-3020168307c5", "loading": null } ...

Eliminate repeated elements within a JSON dataset to create a consolidated array

Looking to extract unique data from the JSON object below in order to create a result json with a list of questions and their corresponding choices. Any assistance would be greatly appreciated. Thanks in advance..!! var data = [ { "category": "s ...

Executing a PHP script to initiate a ping transmission

I have a project to complete for my university involving the development of a simple application. However, I lack experience in this area and am unsure how to proceed. The objective is straightforward: I want to send ping requests to 50 IP addresses at t ...

Make sure two elements are siblings in JavaScript / jQuery

Looking at the HTML structure below: <div class="wrap"> <div id="a"></div> <div id="b"></div> </div> A statement is presented as false: ($('#a').parent() == $('#b').parent()); //=> false ...

What causes a function to be returned as null when it is appended or assigned as textContent?

There's something I've been trying to figure out and I'm curious to get an answer on why it behaves this way. const print_html = (param) => { let container = document.querySelector('Container'); console.log(test_function(pa ...

The element type provided is not valid: it should be a string for built-in components or a class/function for composite components. However, an object was received instead. - React Native

After conducting extensive research, I have been unable to find a solution as to why this issue persists. Can anyone shed some light on what the error might be referring to? Error: Element type is invalid: expected a string (for built-in components) or a c ...

Update the JSON data based on the specifications outlined in my project

class Transformation { constructor() { this.colHeaders = { error_description: "Description", error_status: "Status", error_code: "Error Code" }; } getColHeader() { return this.colHeaders; } } var jsonData = { error ...

I am encountering an issue where body-parser is not functioning properly with typescript. Whenever I make a request, the request.body is returning as undefined

Below is the code snippet for my Express application using TypeScript version 3.7.4: import bodyParser from "body-parser"; import config from "config"; import cookieParser from "cookie-parser"; import express from "express"; import mongoose from "mongoose ...

Styling buttons with different colors using React onClick event.getSelectedItem() method

I am having an issue with adding a border when the class is set to "transportation active" in my React code. When I click on one of the icons, all of them become active instead of just the clicked one. This behavior is not what I want, as I only want the ...

I can't figure out why my JavaScript recursion function isn't correctly counting the number of lines, words, and characters in a text

Seeking assistance with my JavaScript recursion program that is not functioning as expected. The goal is to extract words from a text file and output the count of words, lines, and characters. I am uncertain where my mistake lies, so any guidance on code m ...

Can Angular 4 experience race conditions?

Here is a snippet of my Angular 4 Service code: @Injectable() export class MyService { private myArray: string[] = []; constructor() { } private calculate(result): void { myArray.length = 0; // Perform calculations and add results to myAr ...

Asynchronous setTimeout for server-side operations

I am currently facing an issue with my web server. Whenever a request is made, the server initiates a phone call, waits for 3 seconds, and then checks if the call is still ongoing. I have utilized setTimeout to achieve this functionality, but it seems to b ...