Utilizing an object or object key as a parameter for a component in VueJs

My Personnel table is connected to an array of objects from VueJs. The last column in the table has an edit button for each record.

I want to display a modal popup when the edit button is clicked, where the textboxes are linked to the personnel properties I wish to modify. Upon clicking the save button on the modal popup, it should update both the table and the data source.

I'm currently struggling with passing the object or even just the key to the component so I can load the data or link the edited object to my textboxes.

JS

var app = new Vue({
  el: '#my-app',
  data: {
    personnels: [
      {
        id: 1,
        firstName: 'Billy',
        lastName: 'Bob',
        email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="315353715a54465d1f525e5c">[email protected]</a>'
      },
      // Additional personnel objects here
    ]
  },
  methods: {
    showPersonnelEditor: function () {
      // How can I pass data to the personnelEditor component?
    }
  }
});

Vue.component('personnel-editor', {
  prop: ['personnel']
});

HTML

<div id="my-app">
    <table classs="table" width="100%">
        <tr>
            <th>
                Id
            </th>
            <th>
                First Name
            </th>
            <th>
                Last Name
            </th>
            <th>
                Email
            </th>
            <th>
                Actions
            </th>
        </tr>
        <tr v-for="personnel in personnels">
            <td>
                {{ personnel.id }}
            </td>
            <td>
                {{ personnel.firstName }}
            </td>
            <td>
                {{ personnel.lastName }}
            </td>
            <td>
                {{ personnel.email }}
            </td>
            <td>
                <button v-on:click="showPersonnelEditor">Edit</button>
            </td>
        </tr>
    </table>


    <personnel-editor inline-template><div class="modal fade" >
        <div class="modal-dialog">
            <div class="modal-header">
                header
            </div>
            <div class="modal-content">
                // Form fields for editing personnel information
            </div>
            <div class="modal-footer">
                oh mah foot
            </div>
        </div>
    </div>
</div></personnel-editor>

Answer №1

If you want to edit personnel information, you can follow these steps:

<button v-on:click="showPersonnelEditor(personnel)">Edit</button>

In the showPersonnelEditor method, make sure to declare selectedPersonnel in the data:

showPersonnelEditor: function (personnel) {
  this.selectedPersonnel = personnel; 
}

Next, in your HTML template, use the personnel editor component:

<personnel-editor inline-template :personnel=selectedPersonnel><div class="modal fade" >

I hope this helps.

To bind the result of the modal:

You can emit an event when the modal is closed, like this:

whenModalClosedMethod() {
    this.$emit('personnel-edited', personnel);
}

In your template, listen for the emitted event and call a method to update the personnel:

<personnel-editor inline-template :personnel=selectedPersonnel v-on:personnel-edited="updatePersonnel">

Add a method in your root component to update the personnel information:

updatePersonnel: function(personnel) {
   // Update the correct entry in the personnels array
}

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

Is it possible to create a synchronous behavior for an asynchronous JavaScript method using async-waterfall?

In my JavaScript application, I have three function calls. The second one is asynchronous, causing some issues as it completes after the third one. Here's how it looks: function runner() { function1(); function2(); //This is the asynchronous ...

Tips for adding React components to an array with the help of backticks

Currently, I am attempting to populate an array with icons by extracting the name from data and concatenating "<" and "/>" around it in order to convert it into an Mui Icon. Despite renaming the imported icons to match the names in the data, when I ...

Using AngularJS, call the $http function in response to another HTTP request

I recently started working with angular JS and I encountered a problem while trying to implement $http within the response of another $http call. My problem is that when I make a $http call within the response of another $http call, the data is not displa ...

Create partial form copies dynamically based on user selections within a nested form

Attempting to design a form that dynamically generates copies of a nested form based on user input. Three models are involved: Visualizations, Rows, and Panes. The goal is to enable users to select from a dropdown menu, create a row, and choose 1, 2, or 3 ...

Performing a Javascript ajax post of a canvas snapshot in "image/jpeg" format to an asp.net web form

Here is the JavaScript AJAX code snippet: var dataURL = canvas.toDataURL("image/jpeg"); var xhr = new XMLHttpRequest(); xhr.open("POST", "myPage.aspx", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechan ...

Updating Button Text Upon Click

I am looking to create a webpage filled with riddles. After each riddle, there will be an answer button that, when clicked, reveals the answer. Clicking the button again should hide the answer. I attempted to implement this functionality using Javascript, ...

Resizing the Canvas in Three.js with GLSL

I'm currently facing challenges with maintaining the proportions when resizing the window to smaller sizes on mobile devices as shown in this CodePen. The lines and interaction become difficult to see on mobile screens. Is there a solution to address ...

Is it possible to protect assets aside from JavaScript in Nodewebkit?

After coming across a post about securing the source code in a node-webkit desktop application on Stack Overflow, I began contemplating ways to safeguard my font files. Would using a snapshot approach, similar to the one mentioned in the post, be a viable ...

Material-UI: The call stack has exceeded the maximum range, causing an Uncaught RangeError

I am currently utilizing the Dialog and Select components offered by Material-UI in conjunction with React. Here is a quick example: import React from 'react'; import { Dialog, MenuItem, Select } from '@material-ui/core'; class Examp ...

getStaticProps function in Next.js fails to execute

My [slug].js file includes two Next.js helper functions, getStaticPaths and getStaticProps. These functions are exported and create the path posts/[slug]. I have also added a post file named hello.json. However, when I try to access localhost:3000/posts/he ...

Using CSS properties as false values within React applications

Can you provide guidance on using conditional styles with conditional operators? What would be the ideal code example? Is it possible to use non-CSS values? margin: isOpen ? '10px' : undefined margin: isOpen ? '10px' : 'initial&a ...

Endlessly refreshing occurs when attempting to load a separate CSS stylesheet using document.write

I am attempting to implement two separate stylesheets on my WordPress blog - one for web access and another for our iOS app. Currently, we are adding ?app=true to the URL when accessing content through the app in order to distinguish between the two. Using ...

Can the URL in angular.js be modified without having to invoke the controller

My current issue involves the following scenario: I am using the url http://localhost:8080/#/dashboard to display features in my dashboard. For instance, when the url changes to http://localhost:8080/#/dashboard/20, I need to load cities where the country ...

How can I enhance the functionality of AJAX in Symfony 1.4 when using jQuery, specifically in cases where a select box's options depend on the selection of another select box?

In my Symfony 1.4 application, I have three interconnected tables outlined below. Table 1: Conflictos1: connection: doctrine tableName: conflictos_1 columns: id: type: integer(4) fixed: false unsigned: false primary: tru ...

Gather a linear array of deeply nested information

Below is a dictionary: var objectSchemasList = { 1: [ { name: 'list_field1_1', uuid: 'uuid1', fieldObjectSchemaId: 2 }, { name: 'list_field1_2', uuid: 'uuid2', f ...

The Vue.createApp function seems to be malfunctioning, whereas using the new Vue() method is functioning correctly

When running my code, I encountered the following error message: tesyya.js:16 Uncaught TypeError: Vue.createApp is not a function. My code snippet looks like this: const app = Vue.createApp({ data() { return { count: 4 } } }) const vm ...

Inter-class communication using TypeScript callbacks

Struggling with Typescript, I have encountered an issue while trying to send a callback from an angular controller to an angular service. Despite setting a break point at the beginning of the callback function using Chrome Dev Tools, it never gets triggere ...

Guide on deploying a Next.js React project with IIS on a Windows Server

Our goal is to deploy the application on an IIS server while ensuring it supports dynamic routing. After attempting to install IIS node, we are unsure of our next steps. Do we need to load a specific file like server.js in order to run the application ...

What is the best way to remove or reset a map from a material skybox in three.js?

In my ThreeJS scene, I am trying to implement different modes for viewing all models in the skybox, with or without textures. I have two functions called 'getSkyTexture' and 'getSkyColor' which are supposed to work separately. However, ...

Ways to transmit information from a modal to a function

Trying to figure out how to update data in an ng-grid after submitting a modal form with multiple text input controls. Should the ajax create function be called within the $scope.open controller section or resolve? $scope.open = function (size) { var mo ...