VUE(4) : vue-element-admin[4] : 多选下拉框及回显

时间:2025-04-08 07:48:29

参考 : 

        【vue】下拉框回显问题(显示数字,实际应该显示中文)_易小花的博客-****博客_vue下拉框回显 

vue+element 多选下拉框的传值及回显_Humor_L的博客-****博客_vue多选下拉框回显数据

标签

        <el-form-item :label="$t('')" prop="roleCodes">
          <el-select v-model="" multiple class="filter-item" placeholder="请选择角色">
            <el-option v-for="item in calendarTypeOptions" :key="" :label="item.display_name" :value="" />
          </el-select>
        </el-form-item>

注 : 去除[multiple]属性为单选下拉框 

静态下拉框

export default {
  name: 'ComplexTable',
  data() {
    return {
      calendarTypeOptions: () ,
    }
  },
  methods: {
    getStatic() {
      var rs = []
      var o1 = { key: 'CN', display_name: 'China' }
      var o2 = { key: 'US', display_name: 'USA' }
      var o3 = { key: 'JP', display_name: 'Japan' }
      var o4 = { key: 'EU', display_name: 'Eurozone' }
      (o1,o2,o3,o4)
      return rs
    }
  }
}

动态下拉框

export default {
  name: 'ComplexTable',
  data() {
    return {
      calendarTypeOptions: ()
    }
  },
  methods: {
    getAllRoles() {
      var rs = []
      listAll().then(response => {
        (d => {
          var o = { key: , display_name:  }
          (o)
        })
      })
      (rs)
      return rs
    }
  }
}

回显

      const hostids = []
      ('CN','US')
       = hostids

push的字段和标签[:key=""]的key保持一致即可回显 

注 : 单选下拉框回显直接等于key即可,如  = 'CN'

完整代码

<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        {{ $t('') }}
      </el-button>
    </div>
    <el-dialog :="dialogFormVisible">
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item :label="$t('')" prop="roleCodes">
          <el-select v-model="" multiple class="filter-item" placeholder="请选择角色">
            <el-option v-for="item in calendarTypeOptions" :key="" :label="item.display_name" :value="" />
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          {{ $t('') }}
        </el-button>
        <el-button type="primary" @click="create()">
          {{ $t('') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pages, add, update } from '@/api/yongHuGuanLi/yongHuLieBiao'
import { listAll } from '@/api/yongHuGuanLi/jueSheGuanLi'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

var currentSort = null
var currentSortField = null

export default {
  data() {
    return {
      calendarTypeOptions: () ,
      temp: {
        userName: undefined,
        account: undefined,
        phone: undefined,
        roleCodes: undefined,
        password: undefined
      },
      downloadLoading: false,
      dialogFormVisible: false
    }
  },
  created() {
  },
  methods: {
    getAllRoles() {
      var rs = []
      var o1 = { key: 'CN', display_name: 'China' }
      var o2 = { key: 'US', display_name: 'USA' }
      var o3 = { key: 'JP', display_name: 'Japan' }
      var o4 = { key: 'EU', display_name: 'Eurozone' }
      (o1,o2,o3,o4)
      return rs
    },
    resetTemp() {
       = {
        userName: undefined,
        account: undefined,
        phone: undefined,
        roleNames: undefined,
        password: undefined
      }
    },
    handleCreate() {
      // 显示添加页面
       = true
      // 回显
      const hostids = []
      ('CN','US')
       = hostids
    },
    create() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          add().then(() => {
            ()
             = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            ()
          })
        }
      })
    }
  }
}
</script>