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

参考 : 

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

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

标签

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

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

静态下拉框

export default {
  name: 'ComplexTable',
  data() {
    return {
      calendarTypeOptions: this.getStatic() ,
    }
  },
  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' }
      rs.push(o1,o2,o3,o4)
      return rs
    }
  }
}

动态下拉框

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

回显

      const hostids = []
      hostids.push('CN','US')
      this.temp.roleCodes = hostids

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

注 : 单选下拉框回显直接等于key即可,如 this.temp.roleCodes = '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('table.add') }}
      </el-button>
    </div>
    <el-dialog :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item :label="$t('yongHuGuanLiYongHuLieBiao.table.roles')" prop="roleCodes">
          <el-select v-model="temp.roleCodes" multiple class="filter-item" placeholder="请选择角色">
            <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          {{ $t('table.cancel') }}
        </el-button>
        <el-button type="primary" @click="create()">
          {{ $t('table.confirm') }}
        </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: this.getAllRoles() ,
      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' }
      rs.push(o1,o2,o3,o4)
      return rs
    },
    resetTemp() {
      this.temp = {
        userName: undefined,
        account: undefined,
        phone: undefined,
        roleNames: undefined,
        password: undefined
      }
    },
    handleCreate() {
      // 显示添加页面
      this.dialogFormVisible = true
      // 回显
      const hostids = []
      hostids.push('CN','US')
      this.temp.roleCodes = hostids
    },
    create() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          add(this.temp).then(() => {
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    }
  }
}
</script>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值