简体   繁体   English

laravel vue选择所选选项不显示值

[英]laravel vue select selected option not display value

i'm trying to get the selected option of a user task. 我正在尝试获取用户任务的选定选项。

myresult 我的结果

what my expected result is like this expected 什么我预期的结果是这样的预期

Here is my Html code 这是我的HTML代码

<div class="form-group has-float-label">
    <input v-model="form.name" type="text" name="name" placeholder="Name" class="form-control" :class="{ 'is-invalid': form.errors.has('name') }">
    <has-error :form="form" field="name"></has-error>
</div>

<div class="form-group">
    <select name="tasks" v-model="form.tasks" id="tasks" class="form-control" :class="{ 'is-invalid': form.errors.has('tasks') }">
        <option disabled value="">Please select one</option>
        <option v-for="t in alltask" :key="t"
        v-bind:value="t"  >{{t.name}}
        </option>                            
    </select>
    <br />
    {{form.tasks}}
</div>

Below is my JS code 下面是我的JS代码

<script>
    export default {
        data() {
            return {
                editmode: false,
                users : {},
                alltask : {},
                form : new Form({                   
                    id: '',
                    name: '',
                    tasks: {},
                })
            }
        },
        methods: {            
            loadUsers(){
                axios.get("/api/v1/users").then(({ data }) => (
                    this.alltask = data.alltask,
                    this.users = data.users

                    ));
            },
            editModal(user){
                this.editmode = true;
                this.form.reset();
                $('#users').modal('show');                
                this.form.fill(user);
            }
        },
        created() {
           this.loadUsers();
        }
    }
</script>

this is my json response 这是我的json回应

{
    "users": [
        {
            "id": 1,
            "name": "zxc",
            "username": "zxc",
            "tasks": [
                {
                    "id": 1,
                    "name": "cooking"
                }
            ]
        },
        {
            "id": 2,
            "name": "foo",
            "username": "foo",
            "tasks": [
                {
                    "id": 2,
                    "name": "cleaning"
                }
            ]
        }
    ],
    "alltask": [
        {
            "id": 1,
            "name": "cooking"
        },
        {
            "id": 2,
            "name": "cleaning"
        }
    ]
}

the value of v-model is the same with option value but i don't get it to get pre selected upon clicking update button, but if i change the option the v-model code that i put below the v-model itself is getting changed following the option list and i can update it to the db v-model的值与选项值相同,但是在单击“更新”按钮后我没有得到它的预先选择,但是如果我更改选项,我放置在v-model本身下方的v-model代码将得到根据选项列表进行了更改,我可以将其更新到数据库

The Problem is that you bind the whole object as value. 问题是您将整个对象绑定为值。 Even though the object you receive from your call and the object you used to bind the value property to have the same "content" (props & values) they are not the same. 即使您从调用中收到的对象和用于绑定value属性以具有相同“内容”(属性和值)的对象也不相同。 So it will not be preselected. 因此它将不会被预先选择。

What you actually should do is only bind the id of the task and if you want to display the result find the object that has the id from your alltask list 您实际应该做的只是绑定任务的ID,如果要显示结果,请从alltask列表中找到具有ID的对象

https://jsfiddle.net/eywraw8t/382079/ https://jsfiddle.net/eywraw8t/382079/

<select v-model="form.task">
  <option>Please Select</option>
  <option :value="t.id" v-for="(t, i) in alltask" :key="i">{{ t.name }}</option>
</select>

<p>
  {{ selectedTask }}
</p>

As you only select one task I was wondering why you have "form.tasks" - for my example I changed it to singular. 当您仅选择一个任务时,我想知道您为什么拥有“ form.tasks”-例如,我将其更改为单数。

The computed prop selectedTask could look like this 计算出来的道具selectedTask可能看起来像这样

computed: {
  selectedTask() {
    return this.alltask.find(task => {
      return task.id === this.form.task
    })
  }
}

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

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