VueJS: How to display a value in the dynamic modal input with v-model or v-bind: value

1

There is a listing where I return all users active on the system and there is a button for Edit User , when I click on this option, I open a modal, capture the user id and data of that user in question, and I load the inputs with the fields.

However, it does not work as expected. I'll introduce my code:

new Vue({
el: '#user-table',
data: {
    users: '',
    firstName: '',
    lastName: '',
    cpf: '',
    email: '',
    description: '',
    billing: '',
    photo: ''
},
methods: {
    resetPassword: function(userId, event) {
        ...
    },
    disableUser: function(userId, event) {
        ...
    },
    callModalEditUser: function(userId, event) {

        var self = this;
        self.$http.get('/dashboard/usuarios/usuario/' + userId).then(function(response) {

            var user = response.data.user;

            self.firstName    = user.first_name;
            self.lastName     = user.last_name;
            self.cpf          = user.cpf;
            self.email        = user.email;
            self.description  = user.description;
            self.billing      = user.billing;
            self.photo        = user.photo;

            $('#modal-edit-user').modal();
        });
    }
},
ready: function() {
    var self = this;
    self.$http.get('/dashboard/usuarios/listar-usuarios').then(function(response) {
        self.users = response.data;
    });
}
});

The problem itself is in the function callModalEditUser , this is the function called for this purpose, below I show her the call in HTML:

<a data-toggle="tooltip" v-on:click="callModalEditUser(user.id, $event)"
   data-placement="top" title="Editar usuário"
   class="btn btn-primary btn-sm edit-user">
       <i class="fa fa-cog"></i>
</a>

Finally, in the modal, there are the inputs, I will present the forms that I have already tried:

<input type="text" class="form-control" v-bind:value="firstName" id="first_name" placeholder="Primeiro nome">

<input type="text" class="form-control" v-model="firstName" id="first_name" placeholder="Primeiro nome">

<input type="text" class="form-control" v-bind:value="@{{ firstName }}" id="first_name" placeholder="Primeiro nome">

<input type="text" class="form-control" v-model="@{{ firstName }}" id="first_name" placeholder="Primeiro nome">

<input type="text" class="form-control" value="@{{ firstName }}" id="first_name" placeholder="Primeiro nome">

The return of json occurs naturally with user data, within user I get all the information I need to display on the screen.

Remembering that, I use @{{ }} to escape and interpret {{ }} because my backend application is Laravel and I use the Blade Template.

    
asked by anonymous 08.09.2016 / 18:11

2 answers

2

Even in a Laravel application you can use v-model like this: v-model="field". Verify that the modal is within the scope of your Vue instance: the: '# user-table'. That is, the modal must be defined within the tag's html:

<div id="user-table">

 <div class="modal">
  ....
 </div>

</div>
    
09.09.2016 / 13:09
2

Verify that self.firstName = user.first_name is actually being assigned through a console.log or use vue dev tools.

Vue devtools

About the utlize v-model syntax for binding of inputs:

  

Form Input Bindings

     

Basics Usage

     

You can use the v-model directive to create two-way data bindings on   form input and textarea elements. It automatically picks the correct   way to update the element based on the input type. Although a bit   magical, v-model is essentially syntax sugar for updating data on user   input events, plus special care for some edge cases.    link

    
08.09.2016 / 19:08