简体   繁体   English

如何使用 Element 从多个选择中获取 ID?

[英]How can i get an ID from a select multiple using Element?

I have a select for persons assigned to a proyect , so i need to send the ID from the object selected to a function i tried:我有一个proyect给项目的personsselect ,所以我需要将所选objectID发送到我尝试过的function

 <el-form-item label="Assign to:" prop="person">
                <el-select v-model="form.persons"  multiple value-key="id"  id="person_id" @change="getData()" placeholder="Select Personal" class="max-input" filterable>
                  <el-option  v-for="person in orderedPersons"  
                            :label="person.name +' '+ person.last_name" 
                            :key="person.id"
                            :value="person">
                  </el-option>
                </el-select>

Array persons:阵列人员:

form: { persons: [],}

Method方法

getData() {
   var pid;
   this.pid = document.getElementById("person_id").value;
   console.log(pid);
  this.$axios.get("/person/hasproject/" + this.pid ).then(response => {
    try {             
      notifications_success(response.data.userMessage);
    } catch (error) {}
  });
},

The pid (person id) is undefined and i can't find a solution for my case, i will appreciate help and comments. pid (个人 ID)未定义,我找不到适合我的情况的解决方案,我将不胜感激帮助和评论。

since the selected people are being populated through the v-model, you can access the person IDs by looking in your this.form.persons array and iterating through it.由于所选人员是通过 v-model 填充的,因此您可以通过查看 this.form.persons 数组并遍历它来访问人员 ID。

So let's say所以让我们说

form: { 
 persons: [
  {name: "You", id: 1}, 
  {name: "Her", id: 2}, 
  {name: "Him", id: 3}
 ]
}

then you could do something like.那么你可以做类似的事情。

getData(){  
   this.form.persons.forEach(person=>{
    this.$axios.get("/person/hasproject/" + person.id )
        .then(//et cetera)

   })  
}

You are facing what is referenced in Vue.js documentation as change detection caveats .您正面临 Vue.js 文档中引用的变更检测警告

Tl;dr Tl;博士

Due to the nature of javascript, Vue.js can't watch for changes inside objects.由于 javascript 的性质,Vue.js 无法监视对象内部的变化。 You have to tell him explicitly that some changes occured by using the provided method Vue.set(object, key, value) .您必须明确告诉他,使用提供的方法Vue.set(object, key, value)发生了一些变化。 You can give a try by using it, or avoiding it by, for example, doing so:您可以尝试使用它,或者避免它,例如,这样做:

Template模板

 <el-form-item label="Assign to:" prop="person">
   <el-select
       multiple value-key="id"
       id="person_id"
       @change="getData()" // removed
       placeholder="Seleccione personal"
       class="max-input" filterable
     >
     <el-option  v-for="person in orderedPersons"  
       :label="person.name +' '+ person.last_name" 
       :key="person.id"
       :value="person.id"
       @click="updateData(person)" // added
     >
     </el-option>
  </el-select>

Script脚本

form: { persons: []},

updateData(person) {
    for (const index in this.form.persons) {
        if (this.form.persons[i].id === person.id) {
            this.form.persons.splice(i, 1);
            // it was already inside, and we put it outside
            // the form.persons array, so we're done
            return 0;
        }
    }
    // If we are here, then person wasn't inside form.persons
    // So we add it
    this.form.persons.push(person);
    this.getDatas(person.id);
},
getData(id) {
    console.log(id);
    this.$axios.get("/person/hasproject/" + id )
    .then(response => {
      try {             
        notifications_success(response.data.userMessage);
      } catch (error) {}
  });
},

It's just a way to avoid the problem of detection caveat, but if I understand what you are trying to attempt, then it will probably be sufficient.这只是避免检测警告问题的一种方法,但是如果我了解您要尝试的内容,那么它可能就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM