【问题标题】:Vue.Js Retrieve data from database to fill input using datalistVue.Js 从数据库中检索数据以使用 datalist 填充输入
【发布时间】:2021-10-20 08:33:12
【问题描述】:

我正在使用 Laravel 8 和 Vue 3 做一个项目,并且我有一个学生组件。 其中有一个数据列表,用于查找已创建的学生,我希望当我选择一个学生时,它会用我数据库中的数据填充我的字段。

我已经尝试过使用 vueJs 和 v-model,但我只得到了数据列表输入中的字符串。 当我尝试检索在我的字段或数据列表输入中具有 [object Object] 的数据时,我也遇到了一些问题。

这是我的代码:

//This is the data of my trainees which are my students
 data() {
        return {
            trainees: [],
          },
          //And this is the method that I use to retrieve the student from the database
  methods: {
     getTrainee() {
            axios.get(AXIOS + 'trainee')
                .then(response => this.trainees = response.data)
                .catch(error => console.log(error));
        },
  },
<!--This is a part of my student component, this is the datalist where I can find my students-->
 
 <div class="search_trainee">
            <input id="search" class="search_trainee_input" list="trainees" placeholder=" "
                   type="text">
            <label class="label_search" for="search">Search a trainee</label>
            <datalist  id="trainees">
                <option v-for="user in trainees" :key="user.id">
                    {{ user.firstname }} {{ user.lastname }}
                </option>
            </datalist>
        </div>
        
  <!--This is the other part in my Student component, this is the part where I want my input and filed to be fill with the data of the student I pick-->

        <div class="form_trainee">
            <h3 class=" title_form">Add a trainee</h3>
            <div class="row g-3">
                <div class="col-md-6">
                    <input id="lastname" ref="lastname" class="form-control" name="lastname" placeholder=" "
                           type="text" @blur.prevent="addTrainee();displayAudit()">
                    <label class="label_form" for="lastname">Lastname</label>
                </div>
                <div class="col-md-6">
                    <input id="firstname" ref="firstname" class="form-control" name="firstname" placeholder=" "
                           type="text" @blur.prevent="update">
                    <label class="label_form" for="firstname">Firstname</label>
                </div>
                <div class="col-md-6">
                    <input id="email" ref="email" class="form-control" name="email" placeholder=" " type="email"
                           @blur.prevent="update">
                    <label class="label_form" for="email">Email</label>

                </div>
                <div class="col-md-6">
                    <input id="company" ref="company" class="form-control" name="company" placeholder=" "
                           type="text"
                           @blur.prevent="update">
                    <label class="label_form" for="company">Company</label>

                </div>
                <div class="col-md-6">
                    <input id="vehicle" ref="vehicle" class="form-control" name="vehicle" placeholder=" "
                           type="text"
                           @blur.prevent="update">
                    <label class="label_form" for="vehicle">Vehicle</label>

                </div>
                <div class="col-md-6">
                    <input id="location" ref="location" class="form-control" name="location" placeholder=" "
                           type="text"
                           @blur.prevent="update">
                    <label class="label_form" for="location">Location</label>

                </div>
                <div class="col-md-6">
                    <select id="instructor_id" ref="instructor_id" v-model="instructor" class="form-control"
                            name="instructor_id"
                            @blur.prevent="update">
                        <option value="">--Choose an instructor--</option>
                        <option v-for="user in instructors" :key=user.id v-bind:value="{id:user.id}">{{
                                user.firstname
                            }}
                            {{ user.lastname }}
                        </option>
                    </select>
                </div>
                <div class="col-md-6">
                    <select id="acpCenter" ref="acp_center_id" v-model="acpCenter" class="form-control" name="acpCenter"
                            @blur.prevent="update">
                        <option value="">--Choose an Acp Center--</option>
                        <option v-for="center in acpCenters" :key="center.id" v-bind:value="{id:center.id}">
                            {{ center.city }} {{ center.postal_code }}
                        </option>
                    </select>
                </div>
            </div>
        </div>

欢迎任何帮助或建议或提示,如果需要,我可以提供更多代码。 感谢您的宝贵时间。

【问题讨论】:

  • 你在哪里调用 getTrainee() 方法?
  • @FrancoMitoire 嗯,它是由 vue js 创建的,然后数据进入“Trainees”然后我使用它
  • 您是否尝试在数据选项中定义 selectedUser var,然后 单击一个并查看 vue 开发工具,您的 selectedUser var 中有什么
  • 受训者应该是数组吗?如果是这样,您缺少括号[]
  • @Tim 哦,是的,没错,一开始就是这样,我只是在尝试,我编辑了我的帖子

标签: javascript laravel vue.js vuejs3


【解决方案1】:

实际上,datalist 在 vuejs 中没有事件处理程序。因此,很难实现您想要实现的目标。你为什么不试试vue-select?它使用起来非常简单,并且很容易处理事件。

这里有一个演示:

Vue.component('v-select', VueSelect.VueSelect)

new Vue({
  el: "#app",
  data() {
    return {
      selectedStudent: "",
      acpCenter: "",
      instructor: "",
      instructors: [
        { id: 10, firstname: "instructor1", lastname: "lastname" },
        { id: 11, firstname: "instructor2", lastname: "lastname" },
        { id: 12, firstname: "instructor3", lastname: "lastname" }
      ],
      acpCenters: [
        { id: 10, city: "city", postalCode: "postalCode" },
        { id: 11, city: "city2", postalCode: "postalCode" },
        { id: 12, city: "city3", postalCode: "postalCode" }
      ],
      trainees: []
    };
  },
  mounted() {
    this.getTrainee();
  },
  //And this is the method that I use to retrieve the student from the database
  methods: {
    getTrainee() {
      this.trainees = [
        {
          id: 10,
          firstname: "Ben",
          lastname: "Alphonso",
          company: "company",
          vehicle: "vehicle",
          location: "location",
          email: "something.1@email.com"
        },
        {
          id: 11,
          firstname: "Carry",
          lastname: "Menati",
          company: "company",
          vehicle: "vehicle",
          location: "location",
          email: "something.2@email.com"
        },
        {
          id: 13,
          firstname: "Bruce",
          lastname: "Wayne",
          company: "Wayne Enterprises",
          vehicle: "vehicle",
          location: "location",
          email: "something.2@email.com"
        }
      ];
    },

    onInput(val) {
      if (!val) if (!val) this.selectedStudent = { firstname: "" };
    },

    addTrainee() {
      return false;
    },
    displayAudit() {
      return false;
    },
    update() {
      return true;
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vue-select@latest"></script>
<link rel="stylesheet" href="https://unpkg.com/vue-select@latest/dist/vue-select.css">

<div id="app">
  <template>
  <div class="container">
    <div class="search_trainee">
      <v-select
        v-model="selectedStudent"
        @input="onInput"
        label="firstname"
        :options="trainees"
      ></v-select>
    </div>

    <!--This is the other part in my Student component, this is the part where I want my input and filed to be fill with the data of the student I pick-->

    <div class="form_trainee">
      <h3 class="title_form">Add a trainee</h3>
      <div class="row g-3">
        <div class="col-md-6">
          <input
            id="lastname"
            ref="lastname"
            class="form-control"
            name="lastname"
            v-model="selectedStudent.lastname"
            placeholder=" "
            type="text"
          />
          <label class="label_form" for="lastname">Lastname</label>
        </div>
        <div class="col-md-6">
          <input
            id="firstname"
            ref="firstname"
            class="form-control"
            name="firstname"
            v-model="selectedStudent.firstname"
            placeholder=" "
            type="text"
            @blur.prevent="update"
          />
          <label class="label_form" for="firstname">Firstname</label>
        </div>
        <div class="col-md-6">
          <input
            id="email"
            ref="email"
            class="form-control"
            name="email"
            placeholder=" "
            v-model="selectedStudent.email"
            type="email"
            @blur.prevent="update"
          />
          <label class="label_form" for="email">Email</label>
        </div>
        <div class="col-md-6">
          <input
            id="company"
            ref="company"
            class="form-control"
            name="company"
            placeholder=" "
            v-model="selectedStudent.company"
            type="text"
            @blur.prevent="update"
          />
          <label class="label_form" for="company">Company</label>
        </div>
        <div class="col-md-6">
          <input
            id="vehicle"
            ref="vehicle"
            class="form-control"
            name="vehicle"
            placeholder=" "
            v-model="selectedStudent.vehicle"
            type="text"
            @blur.prevent="update"
          />
          <label class="label_form" for="vehicle">Vehicle</label>
        </div>
        <div class="col-md-6">
          <input
            id="location"
            ref="location"
            class="form-control"
            name="location"
            placeholder=" "
            v-model="selectedStudent.location"
            type="text"
            @blur.prevent="update"
          />
          <label class="label_form" for="location">Location</label>
        </div>
        <div class="col-md-6">
          <select
            id="instructor_id"
            ref="instructor_id"
            v-model="instructor"
            class="form-control"
            name="instructor_id"
            @blur.prevent="update"
          >
            <option value="">--Choose an instructor--</option>
            <option
              v-for="user in instructors"
              :key="user.id"
              v-bind:value="{ id: user.id }"
            >
              {{ user.firstname }}
              {{ user.lastname }}
            </option>
          </select>
        </div>
        <div class="col-md-6">
          <select
            id="acpCenter"
            ref="acp_center_id"
            v-model="acpCenter"
            class="form-control"
            name="acpCenter"
            @blur.prevent="update"
          >
            <option value="">--Choose an Acp Center--</option>
            <option
              v-for="center in acpCenters"
              :key="center.id"
              v-bind:value="{ id: center.id }"
            >
              {{ center.city }} {{ center.postalCode }}
            </option>
          </select>
        </div>
      </div>
    </div>
  </div>
</template>
</div>
注意,这是浏览器版本。将其导入本地 nodejs 项目的方式是:
  1. yarn add vue-selectnpm install vue-select 安装依赖项
  2. import vue-select 组件和 css 像这样:
<script>
import Vue from "vue";
import vSelect from "vue-select";
import "vue-select/dist/vue-select.css";
Vue.component("v-select", vSelect);
</script>

app.vuesomething.vue 文件中的最终代码如下所示:

<template>
  <div class="container">
    <div class="search_trainee">
      <v-select
        v-model="selectedStudent"
        @input="onInput"
        label="firstname"
        :options="trainees"
      ></v-select>
    </div>

    <!--This is the other part in my Student component, this is the part where I want my input and filed to be fill with the data of the student I pick-->

    <div class="form_trainee">
      <h3 class="title_form">Add a trainee</h3>
      <div class="row g-3">
        <div class="col-md-6">
          <input
            id="lastname"
            ref="lastname"
            class="form-control"
            name="lastname"
            v-model="selectedStudent.lastname"
            placeholder=" "
            type="text"
          />
          <label class="label_form" for="lastname">Lastname</label>
        </div>
        <div class="col-md-6">
          <input
            id="firstname"
            ref="firstname"
            class="form-control"
            name="firstname"
            v-model="selectedStudent.firstname"
            placeholder=" "
            type="text"
            @blur.prevent="update"
          />
          <label class="label_form" for="firstname">Firstname</label>
        </div>
        <div class="col-md-6">
          <input
            id="email"
            ref="email"
            class="form-control"
            name="email"
            placeholder=" "
            v-model="selectedStudent.email"
            type="email"
            @blur.prevent="update"
          />
          <label class="label_form" for="email">Email</label>
        </div>
        <div class="col-md-6">
          <input
            id="company"
            ref="company"
            class="form-control"
            name="company"
            placeholder=" "
            v-model="selectedStudent.company"
            type="text"
            @blur.prevent="update"
          />
          <label class="label_form" for="company">Company</label>
        </div>
        <div class="col-md-6">
          <input
            id="vehicle"
            ref="vehicle"
            class="form-control"
            name="vehicle"
            placeholder=" "
            v-model="selectedStudent.vehicle"
            type="text"
            @blur.prevent="update"
          />
          <label class="label_form" for="vehicle">Vehicle</label>
        </div>
        <div class="col-md-6">
          <input
            id="location"
            ref="location"
            class="form-control"
            name="location"
            placeholder=" "
            v-model="selectedStudent.location"
            type="text"
            @blur.prevent="update"
          />
          <label class="label_form" for="location">Location</label>
        </div>
        <div class="col-md-6">
          <select
            id="instructor_id"
            ref="instructor_id"
            v-model="instructor"
            class="form-control"
            name="instructor_id"
            @blur.prevent="update"
          >
            <option value="">--Choose an instructor--</option>
            <option
              v-for="user in instructors"
              :key="user.id"
              v-bind:value="{ id: user.id }"
            >
              {{ user.firstname }}
              {{ user.lastname }}
            </option>
          </select>
        </div>
        <div class="col-md-6">
          <select
            id="acpCenter"
            ref="acp_center_id"
            v-model="acpCenter"
            class="form-control"
            name="acpCenter"
            @blur.prevent="update"
          >
            <option value="">--Choose an Acp Center--</option>
            <option
              v-for="center in acpCenters"
              :key="center.id"
              v-bind:value="{ id: center.id }"
            >
              {{ center.city }} {{ center.postalCode }}
            </option>
          </select>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import vSelect from "vue-select";
import "vue-select/dist/vue-select.css";
Vue.component("v-select", vSelect);

export default {
  name: "TodoList",
  data() {
    return {
      selectedStudent: "",
      acpCenter: "",
      instructor: "",
      instructors: [
        { id: 10, firstname: "instructor1", lastname: "lastname" },
        { id: 11, firstname: "instructor2", lastname: "lastname" },
        { id: 12, firstname: "instructor3", lastname: "lastname" }
      ],
      acpCenters: [
        { id: 10, city: "city", postalCode: "postalCode" },
        { id: 11, city: "city2", postalCode: "postalCode" },
        { id: 12, city: "city3", postalCode: "postalCode" }
      ],
      trainees: []
    };
  },
  mounted() {
    this.getTrainee();
  },
  //And this is the method that I use to retrieve the student from the database
  methods: {
    getTrainee() {
      this.trainees = [
        {
          id: 10,
          firstname: "Ben",
          lastname: "Alphonso",
          company: "company",
          vehicle: "vehicle",
          location: "location",
          email: "something.1@email.com"
        },
        {
          id: 11,
          firstname: "Carry",
          lastname: "Menati",
          company: "company",
          vehicle: "vehicle",
          location: "location",
          email: "something.2@email.com"
        },
        {
          id: 13,
          firstname: "Bruce",
          lastname: "Wayne",
          company: "Wayne Enterprises",
          vehicle: "vehicle",
          location: "location",
          email: "something.2@email.com"
        }
      ];
    },

    onInput(val) {
      if (!val) this.selectedStudent = { firstname: "" };
    },

    addTrainee() {
      return false;
    },
    displayAudit() {
      return false;
    },
    update() {
      return true;
    }
  }
};
</script>

【讨论】:

  • 当我这样做时,我几乎无法点击我的数据列表的下拉菜单(我不知道为什么,当我将鼠标放在它上面时就像剪辑一样,如果我设法点击它,数据列表的输入填充了[Object object],并且我在控制台中有一个警告:Unexpected token
  • 所以我按照vue-select的文档,现在出现这个错误:TypeError: this.$on is not a function,v-select标签高亮显示
  • @Bastien 更新了我的答案,尝试使用最终代码,这是我粘贴的实际工作文件。
  • 是的,你是对的。有一个解决方案。检查此answer 和 cmets。您可以改用this 库,它具有类似的实现,但适用于 vue 3
  • 我看了看,我运行 npm install 和一切但我仍然有:VueNextSelect 未定义,它已导入,它在我的 package.json 中,但它在我的 app.js 和即使我运行 npm install 或类似的东西,它目前也不起作用,但我会更深入地研究一下,感谢您的时间,我会检查您的答案,因为它适用于使用 vue 2 的人
猜你喜欢
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2013-10-17
  • 2019-05-08
  • 2021-08-03
  • 2013-07-03
相关资源
最近更新 更多