如何在vue js中创建动态表单/过滤器项?

jjhzyzn0  于 2023-01-31  发布在  Vue.js
关注(0)|答案(1)|浏览(101)

我在我的项目中使用Vue js,我对这种语言非常陌生。我想在Vue js中创建一个动态项目选择器。我不知道从哪里开始。我想要的是我有一个输入文本字段,用户在其中输入项目名称,然后在输入下方显示一个下拉菜单,用户可以选择项目。一旦项目被选中,它将被添加到另一个div中,如所附的图像。用户可以添加尽可能多的项目,因为他想要已经选定的项目将不会显示在下拉列表中。一旦项目被选中,该项目将在表单中用于与数据库交互,以获取详细信息或过滤数据。附件是图像,让你明白我到底想要什么。

我也想知道我们在编程语言中所称的这个特性。

xriantvc

xriantvc1#

那叫芯片,你可以叫自定义下拉芯片,你可以用javascript,jquery,css来达到这个效果,但是在Vue js的情况下,我会推荐给你vuetify.,它是为Vue js设计的,用更少的代码让组件变得更好。
示例:

<template>
      <v-card
        class="mx-auto"
        max-width="500"
      >
        <v-col
          v-if="!allSelected"
          cols="12"
        >
          <v-text-field
            ref="search"
            v-model="search"
            class="search"
            hide-details
            label="Search"
            single-line
          />
        </v-col>
            
        <v-list class="list">
          <template v-for="item in categories">
            <v-list-item
              v-if="!selected.includes(item)"
              :key="item.text"
              :disabled="loading"
              @click="selected.push(item)"
            >
              <template #prepend>
                <v-icon
                  :disabled="loading"
                  :icon="item.icon"
                  :color="item.color"
                />
              </template>
    
              <v-list-item-title v-text="item.text" />
            </v-list-item>
          </template>
        </v-list>
    
        <v-container>
          <v-row
            align="center"
            justify="start"
          >
            <v-col
              v-for="(selection, i) in selections"
              :key="selection.text"
              cols="auto"
              class="py-1 pe-0"
            >
              <v-chip
                :disabled="loading"
                closable
                :color="selection.color"
                @click:close="selected.splice(i, 1)"
              >
                <v-icon
                  :icon="selection.icon"
                  start
                />
    
                {{ selection.text }}
              </v-chip>
            </v-col>
          </v-row>
        </v-container>
    
        <v-divider v-if="!allSelected" />
    
       
    
        <v-divider />
    
        <v-spacer />
    
        <v-btn
          :disabled="!selected.length"
          :loading="loading"
          color="purple"
          variant="text"
          @click="next"
        >
          Next
        </v-btn>
      </v-card>
    </template>

脚本:

<script>
    export default {
      data: () => ({
        items: [
          {
            text: 'Nature',
            icon: 'mdi-nature',
            color: 'red',
          },
          {
            text: 'Nightlife',
            icon: 'mdi-glass-wine',
            color: 'yellow',
          },
          {
            text: 'November',
            icon: 'mdi-calendar-range',
            color: 'green',
          },
          {
            text: 'Portland',
            icon: 'mdi-map-marker',
            color: 'purple',
          },
          {
            text: 'Biking',
            icon: 'mdi-bike',
            color: 'orange',
          },
        ],
        loading: false,
        search: '',
        selected: [],
      }),
    
      computed: {
        allSelected () {
          return this.selected.length === this.items.length
        },
        categories () {
          const search = this.search.toLowerCase()
    
          if (!search) return this.items
    
          return this.items.filter(item => {
            const text = item.text.toLowerCase()
    
            return text.indexOf(search) > -1
          })
        },
        selections () {
          const selections = []
    
          for (const selection of this.selected) {
            selections.push(selection)
          }
    
          return selections
        },
      },
    
      watch: {
        selected () {
          this.search = ''
        },
      },
    
      methods: {
        next () {
          this.loading = true
    
          setTimeout(() => {
            this.search = ''
            this.selected = []
            this.loading = false
          }, 2000)
        },
      },
    }
    </script>

注意:这个例子取自Vuetify's官方网站。同样要得到结果,你首先需要在你的项目中安装Vuetify。

相关问题