Using VueJS to showcase user input in a dynamic list and a pop-up modal

I am attempting to achieve the following:

  1. Use a v-for loop to display form input (name, position, company) as an unordered list, showing only the name input and a button for each person
  2. When a button is clicked, a modal will appear displaying all the data entered for that person (name, company, position) - possibly using an array of objects?

You can view my code sandbox here: https://codesandbox.io/s/testing-2s8xvi?file=/src/components/EmployeeTable.vue

If you prefer vanilla JavaScript, this example demonstrates how to access data as an array of objects and use .myParam to retrieve the object in the array for a specific person: enter code herehttps://codepen.io/walrus2/pen/wvmoQOP

I would greatly appreciate any help or suggestions, thank you in advance!

Answer №1

If I understand correctly, you are looking to convert that vanilla JS code into Vue code. If so, here is the translation:

Note: The demo below does not include any validations. Please add them as needed.

new Vue({
  el: '#app',
  data: {
    formObj: {
        name: null,
      position: null,
      company: null
    },
    usersList: [],
    isShowDetails: false,
    modalContent: null
  },
  methods: {
    createList() {
      const obj = structuredClone(this.formObj);
        this.usersList.push(obj);
    },
    showDetails(id) {
        this.isShowDetails = true;
      this.modalContent = this.usersList.find((obj, index) => index === id);
    },
    closeModal() {
        this.isShowDetails = false;
    }
  }
})
/* Modal (Background) */
.modal {
  display: block; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black with opacity */
}

/* Modal Content */
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

/* Close Button */
.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <label>Name:</label><br/>
  <input type="text" id="name" name="name" v-model="formObj.name"/><br/><br/>

  <label>Position:</label><br/>
  <input type="text" id="position" name="position" v-model="formObj.position"/><br/><br/>

  <label>Company:</label><br />
  <input type="text" id="company" name="company" v-model="formObj.company"/><br/><br/>

  <button @click="createList()">Submit</button>

  <ul>
    <li v-for="(user, index) in usersList" :key="index">
      {{ user.name }}
      <button @click="showDetails(index)">Show Details</button>
    </li>
  </ul>

  <!-- Modal -->
  <div id="myModal" class="modal" v-if="isShowDetails">

    <!-- Modal content -->
    <div class="modal-content">
      <span class="close" @click="closeModal">&times;</span>
      <p>Name: <span id="modalText1">{{ modalContent.name }}</span></p>
      <p>Position: <span id="modalText2">{{ modalContent.position }}</span></p>
      <p>Company: <span id="modalText3">{{ modalContent.company }}</span></p>
    </div>

  </div>

</div>

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

Restricting array elements through union types in TypeScript

Imagine a scenario where we have an event type defined as follows: interface Event { type: 'a' | 'b' | 'c'; value: string; } interface App { elements: Event[]; } Now, consider the following code snippet: const app: App ...

Is the controller of a nested view in Angular UI router automatically considered a child of the parent view's controller?

My UI-Router setup includes a main view for products and two nested states below it. I want each nested view to have its own controller while also inheriting some basic information from the parent controller (productsCtrl). However, when attempting to acce ...

Iterating over objects within a nested array using ng-repeat

My back-end is sending me an array of string arrays (Java - CXF-RS). The length of each array within the string arrays ranges from 1 to n. In order to display this data on my front-end, I am utilizing ng-repeat. Everything is functioning correctly with o ...

Display the chosen option in the console by using onChange() function; this is analogous to how onSelect()

I'm having trouble getting the value of a select element to log in the console. I managed to do this with an onSelect() method, but the onChange() method isn't returning anything. Here's what I tried with the onChange() method: <Form.Gr ...

What is the reason for the function to return 'undefined' when the variable already holds the accurate result?

I have created a function that aims to calculate the digital root of a given number. Despite my efforts, I am encountering an issue where this function consistently returns undefined, even though the variable does hold the correct result. Can you help me ...

I'm looking for a simple solution to implement SPA authentication in a Vue.js + Laravel application without relying on JWT. Can anyone

Currently, I am working on developing an App using Laravel and Vue.js. I have already created several views and now I want to convert it into a Single Page Application (SPA). During my search for authentication methods, I came across JWT authentication. Ho ...

Overabundance of Recursive Calls in Node.js Project Dependencies

After a tiring day at work, I noticed an alert for Windows SkyDrive showing that files couldn't be uploaded due to the path being too long. The lengthy directory structure made me chuckle at the technological limitation. However, it got me thinking: ...

Limit the width and height of MUI Popper with a maximum setting

After experimenting with the popper API from MUI, I discovered that it extends beyond my main div. Does anyone have suggestions on how to prevent this overflow? I am looking to increase the height of the popper. Please refer to the code snippet below: con ...

What steps can be taken to obtain the fully computed HTML rather than the source HTML?

Is there a way to access the final computed HTML of a webpage that heavily relies on javascript to generate its content, rather than just the source HTML? For example, if a page contains script functions that dynamically generate HTML, viewing the page sou ...

The successful Ajax POST request does not result in an update to the JSON file

I am facing an issue with adding data to a *.JSON file. Despite receiving a "success" message from Ajax, the file remains unchanged. I have also noticed that when the JSON file is empty, nothing happens at all. Can someone help me find a solution? I'm ...

Rotate through different image sources using jQuery in a circular pattern

I'm working on a project where I have 3 img tags in my HTML file. My goal is to change the src of all 3 images with a button click, using an array that stores 9 different image src links. When the page initially loads, it should display the first set ...

When invoking a function within another function, it is essential to ensure that both values are returned within the function

When calling a function within another function function1(){ return this.a } function2(){ return this.b } To output in string format, you can call function1 inside function2 function2(){ var resultOfFunction1 = this.function1(); return resultOfFunction1 ...

The link containing special characters like % cannot access the api

I am facing an issue with retrieving a signUrl from S3. When I make the call with special characters like %, my code does not parse it correctly and I receive a 404 not found error. Here is the ajax request I am using: My API setup: app.get('/websi ...

Angular implementing a carousel feature using the ngFor directive

Currently, I am working on implementing a carousel feature. The images in the carousel are sourced from a string array and I want to be able to cycle through them when clicking on the left or right arrows. How can I achieve this functionality using only ...

Spring Boot fails to recognize path variable data sent from Angular

When working with Angular 7, I encountered a situation where I needed to pass a value from the service class of Angular. Here is how I achieved it: executeHelloWorldBeanServiceWithPathVariable(name){ console.log("name coming from here"+name); retu ...

A step-by-step guide on upgrading or transferring from Vite 2 to Vite 3 using NPM while harnessing the power of Vue

While it may seem like a straightforward question, I found myself unable to locate the specific process or commands for upgrading from Vite 2 to Vite 3 using npm. Despite reading the announcement and the migration guide (along with other resources), I ha ...

The translation of popups using ngx-translate in Javascript is not functioning properly

When I click on my request, the content "Are you sure?" is not changing to the required language. This issue is located in list.component.html <button type="button" (click)="myrequest(item.Id)">Request View</button> The fu ...

Troubleshooting a TypeScript Problem with React Context

In my AppContext.tsx file, I have defined the following import React, { useState, createContext } from "react"; import { Iitem } from "../utils/interfaces"; interface AppContext { showModal: boolean; setShowModal: React.Dispatch< ...

Troubleshooting PHP/MySQL integration with Google Maps - issues persist

I have come across several other posts on this theme but unfortunately, none of them have been able to help me. I am using the article https://developers.google.com/maps/articles/phpsqlajax_v3, however, the code provided is not working for me. I have a fil ...

Grouping elements of an array of objects in JavaScript

I've been struggling to categorize elements with similar values in the array for quite some time, but I seem to be stuck Array: list = [ {id: "0", created_at: "foo1", value: "35"}, {id: "1", created_at: "foo1", value: "26"}, {id: "2", cr ...