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

Best Practices for Variable Initialization in Stencil.js

Having just started working with Stencil, I find myself curious about the best practice for initializing variables. In my assessment, there seem to be three potential approaches: 1) @State() private page: Boolean = true; 2) constructor() { this.p ...

Event fails to emit when used in conjunction with router-link

I created a basic Vue application: const Home = { template: '<div>Home</div>' } const Bar = { methods: { bar () { alert('bar') this.$emit('test') } }, template: ` <div> ...

Integrate the elements from the <template> section into the designated <slot> area

I am trying to retrieve template content, insert it into a custom element with shadow DOM, and style the span elements inside the template using the ::slotted selector. However, it seems like this functionality is not working as I expected. <!doctype h ...

Creating dynamic pagination in React using a variable length loop

I'm currently implementing pagination using react ultimate pagination. When a page number is clicked, I update a variable to display the necessary content. The challenge arises from having a loop with a variable number of elements, making it impossibl ...

Supplying information to my ejs template while redirecting

I am currently working on a feature that involves sending data from the login page to the home page when the user is redirected. This data will then be used in the home EJS file. Below is the code snippet I have implemented: module.exports = functio ...

Unblocking the context menu: How entering JS directly into the address bar compares to using a bookmark

Exploring the concept of blocking the context menu using JavaScript. Here's how you can block such a menu: document.addEventListener('contextmenu', event => event.preventDefault()); I recently came across an article that mentioned this ...

React: An error has occurred - Properties cannot be read from an undefined value

THIS PROBLEM HAS BEEN RESOLVED. To see the solutions, scroll down or click here I've been working on a React project where I need to fetch JSON data from my server and render it using two functions. However, I'm encountering an issue where the v ...

I have developed a custom jQuery carousel that includes functionality to start and stop the carousel based on specific conditions

I have set up a jQuery carousel that moves to the left when a checkbox is checked, but I need it to stop moving when the checkbox is unchecked. Can someone help me achieve this? $("#checkBox").click(function(){ if($(this).prop("checked") == true){ ...

The power of Rails unleashed through Ajax Remote Javascript

Trying to locate Employees who are engaged in multiple Job roles, I have set up three select boxes that dynamically adjust their options. The problem arises when my CoffeeScript file only triggers once. After the initial selection and rendering of the part ...

Steps for adding hover color to a div with linear gradient border

My current challenge involves adding a hover color to a specific div, but I'm facing obstacles due to the gradient background that was implemented to achieve border-radius functionality. This task is being carried out within a React Component using c ...

Bizarre Behavior of String Comparison in Typescript When Using String.toLowerCase

As someone who is naturally curious (and has no background in JS), I have decided to take the plunge into Typescript. However, I seem to have hit a roadblock. I am trying to compare two strings but want to make it easier by first converting them to lowerca ...

Is it considered fundamentally inappropriate to call $scope.$digest within $scope.$on?

I recently inherited some AngularJS code, and my knowledge of both the codebase and Angular itself is limited. Within the code I inherited, there are instances where $scope.$digest is being called inside a $scope.$on method within a controller. Here's ...

Is there a way to disable text selection in AngularJS when double-clicking using ng-dblclick?

My element has ng-dblclick='doSomthing()' functionality that is functioning correctly, however, it also selects the text in the element which is not visually appealing. Is there a way to avoid this issue? ...

I'm feeling completely lost trying to understand cors in conjunction with fetch, particularly when an options request is

I am using whatwg fetch in the code snippet below: const headers = new Headers(); //uncommenting this causes the preflight options request to be sent //headers.append('x-something', 'foo'); const response = await fetch(&apos ...

Substitute the functions within a Node.js module with simulated functions

I am currently working on a Node.js project that serves as an API wrapper. To ensure the reliability of my code, I am writing unit tests using nodeunit and need to incorporate mock functions into the module. For instance, I require a function that mimics s ...

Display an error message when the button is clicked and the input field is left empty in a Vue 3 script setup

Hello, I am currently exploring Vue 3 and embarking on a new Vue 3 project venture. However, I seem to be encountering a challenge when it comes to displaying an error message if the button is clicked while the input field remains empty in my Vue 3 script ...

Incorporating source files from an Express server into an HTML document

Recently, I delved into the world of Node.js with Express and Socket.io to create a web application, specifically a game. In my project, I have a designated /public folder where I aim to serve the necessary files for the client-side operations. Typically, ...

Prevent the ability to add options dynamically to a drop-down select list

I have implemented a dynamic data retrieval feature from my database using jQuery's .getJSON function and appended the data to an HTML select drop-down list. The JavaScript code for this functionality is as follows: $.getJSON('url', f ...

Tips for retrieving javascript-generated HTML content

Currently, I'm attempting to retrieve article headlines from the NY Times website. Upon inspection, it appears that the HTML is being generated by Javascript since it's only visible when using the 'inspect element' feature in Firefox. ...

When submitting the club form, my goal is to automatically generate a club admin within the user list in activeadmin

My dashboard.rb setup looks like this: ActiveAdmin.register_page "Dashboard" do menu priority: 1, label: proc{ I18n.t("active_admin.dashboard") } content title: proc{ I18n.t("active_admin.dashboard") } do # form render 'form' # Thi ...