laravel - Vue 多选 laravel 提交表单

我有简单的形式:

<form action="" method="post">
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
  </div>
  <div class="form-group form-check">
    <input type="checkbox" class="form-check-input" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Check me out</label>
  </div>
  <div class="form-group">
    <tags-multiselect></tags-multiselect>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

和 Vue tags-multiselect 组件:

<!-- Vue component -->
<template>
  <div>
    <multiselect v-model="value" :options="options" :multiple="true" :close-on-select="false" :clear-on-select="false" :preserve-search="true" placeholder="Выберите тэги" label="name" track-by="name" :preselect-first="true">
    <template slot="selection" slot-scope="{ values, search, isOpen }"><span class="multiselect__single" v-if="values.length &amp;&amp; !isOpen">{{ values.length }} выбрано</span></template>
    </multiselect>
  </div>
</template>

<script>
  import Multiselect from 'vue-multiselect'

  export default {
    components: { Multiselect },
    data () {
      return {
        value: [],
        options: [
          { name: 'Vue.js', language: 'JavaScript' },
          { name: 'Adonis', language: 'JavaScript' },
          { name: 'Rails', language: 'Ruby' },
          { name: 'Sinatra', language: 'Ruby' },
          { name: 'Laravel', language: 'PHP' },
          { name: 'Phoenix', language: 'Elixir' }
        ]
      }
    }
  }
</script>

<!-- New step!
     Add Multiselect CSS. Can be added as a static asset or inside a component. -->
<style src="vue-multiselect/dist/vue-multiselect.min.css"></style>

我需要在我的表单上选择字段。当我发送表单时,在 Controller 中我需要接受来自选择标签的数组。我如何使用 vue 多选来做到这一点?或者我需要使用简单的选择多个吗?请帮助我。

最佳答案

您可以通过使选项仅成为标识符(在您的情况下为名称)并使用可自行过滤的自定义标签来解决此问题。像这样:

<multiselect 
  v-model="rule.id" 
  :options="types.map(type => type.name)" 
  :custom-label="opt => types.find(x => x.id == opt).language">
</multiselect>

还要确保删除 trackBy 属性,因为您的选项不再是对象,它现在是 ['Vue.js', 'Adonis',... ]

来源:https://github.com/shentao/vue-multiselect/issues/432

https://stackoverflow.com/questions/54172717/

相关文章:

python - gremlin-python : what is a valid vertex c

flutter - 为什么 build() 在后台的屏幕上被多次调用?

css - VS Code 中的“导航到声明/实现”

r - 使用 R 中的 DBI 和 ActiveDirectoryPassword 身份验证连接到

angular - NRWL 中的多个应用程序可以访问定义文件的位置

reactjs - 从 DateInput BlueprintJS 更改月份名称

azure - AADSTS50011 : The reply url specified in t

android - Cordova 插件文件路径 : Unable to resolve files

reactjs - 用于 Web 的 React 向上滑动面板

apache-spark - 我的 SparkSession 初始化需要很长时间才能在我的笔记本电脑