vue+element 多选下拉框的传值及回显

给select下拉框添加多选属性multiple

<el-select  v-model="ruleForm.hostid" multiple filterable size="small" >
     <el-option v-for="(item,index) in hostTypelist" :key="index" :value="item.hostid" :label="item.name">{{ item.name }}</el-option>
</el-select>

1、多选下拉框传值

定义hostTypelist数组,

hostTypelist:[],
ruleForm: {
             hostid:[],     
           },

传值时:

//表单,下拉框多选传值
                let hostids = '';
                let hostnames = '';
               if(this.ruleForm.hostid.length>0){
                    this.ruleForm.hostid.forEach(function (item,index) {
                        if(index !== 0){
                            hostids+=',';
                            hostnames+=',';
                        }
                        hostids+=item;
                        this.hostTypelist.forEach(function (itx,idx) {
                            if(item === itx.hostid){
                                hostnames+=itx.name;
                            }
                        })

                    })
                }
                let form = {
                                hostid:hostids,
                            };
//form 对象经过处理传给后台

2、多选下拉框回显

this.$axios.post('方法名', ‘参数’).then(function (data) {
                    if(data.host){//获取到后台返回数据
                        let hostids=[];
                        data.host.forEach(function (item,index) {
                            if(index!==-1){
                                hostids.push(item.hostid)
                            }
                        });
                        this.ruleForm.hostid=hostids;
                    }
                });

因为要回显,所以回显的格式与之前保持一致(要数组形式),在最后循环的时候不能用刚开始定义的hostTypelist数组去接收,此时能回显出个数,但是显示内容是重复的,仔细思考这儿

<el-option v-for="(item,index) in hostTypelist" :key="index" :value="item.hostid" :label="item.name">{{ item.name }}</el-option>

回显循环的到底是什么。最后要的是hostid。

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的vue element selected下拉框虚拟列表组件,可以根据需求进行修改和扩展: ```vue <template> <el-select v-model="selected" placeholder="请选择" @visible-change="handleVisibleChange"> <el-option v-for="(item, index) in options" :key="item.value" :label="item.label" :value="item.value" :disabled="item.disabled" :style="getOptionStyle(index)" ></el-option> </el-select> </template> <script> export default { data() { return { selected: null, options: [], visibleOptions: [], itemHeight: 36, // 每一项的高度 visibleItemCount: 10, // 可见项的数量 bufferItemCount: 3, // 缓冲项的数量,用于提高滚动性能 startIndex: 0, // 可见项的起始索引 endIndex: 0, // 可见项的结束索引 scrollTop: 0, // 滚动的距离 scrollHeight: 0, // 滚动区域的高度 lastScrollTime: 0, // 上一次滚动的时间戳 } }, computed: { totalHeight() { return this.itemHeight * this.options.length }, }, watch: { options: { handler() { this.updateVisibleOptions() }, deep: true, }, scrollTop() { this.updateVisibleOptions() }, }, mounted() { this.updateVisibleOptions() window.addEventListener('resize', this.handleWindowResize) }, beforeDestroy() { window.removeEventListener('resize', this.handleWindowResize) }, methods: { // 更新可见选项 updateVisibleOptions() { // 计算可见项的起始索引和结束索引 const scrollTop = Math.max(0, Math.min(this.scrollTop, this.totalHeight - this.clientHeight)) const startIndex = Math.floor(scrollTop / this.itemHeight) const endIndex = Math.min( this.options.length - 1, startIndex + this.visibleItemCount + this.bufferItemCount * 2 - 1 ) const visibleOptions = this.options.slice(startIndex, endIndex + 1) // 更新状态 this.startIndex = startIndex this.endIndex = endIndex this.visibleOptions = visibleOptions }, // 获取选项的样式 getOptionStyle(index) { const style = {} if (index < this.startIndex - this.bufferItemCount || index > this.endIndex + this.bufferItemCount) { style.display = 'none' } else { style.display = 'block' style.height = this.itemHeight + 'px' style.transform = `translateY(${(index - this.startIndex) * this.itemHeight}px)` } return style }, // 获取滚动区域的高度 getScrollHeight() { return Math.min(this.totalHeight, this.visibleItemCount * this.itemHeight) }, // 处理下拉框的可见性变化 handleVisibleChange(visible) { if (visible) { this.scrollHeight = this.getScrollHeight() this.lastScrollTime = Date.now() } }, // 处理窗口大小变化 handleWindowResize() { this.scrollHeight = this.getScrollHeight() }, // 处理滚动事件 handleScroll(event) { const now = Date.now() if (now - this.lastScrollTime > 100) { this.scrollTop = event.target.scrollTop this.lastScrollTime = now } }, }, } </script> <style scoped> /* 设置下拉框的样式 */ .el-select__caret { display: none; } .el-select-dropdown .el-scrollbar__wrap { max-height: none !important; } .el-select-dropdown .el-scrollbar__thumb { background-color: #ccc; border-radius: 4px; } .el-select-dropdown .el-scrollbar__thumb:hover { background-color: #aaa; } </style> ``` 使用方法: ```vue <template> <div> <virtual-select :options="options" v-model="selected" /> </div> </template> <script> import VirtualSelect from './VirtualSelect' export default { components: { VirtualSelect, }, data() { return { selected: null, options: [ { label: '选项一', value: '1' }, { label: '选项二', value: '2' }, { label: '选项三', value: '3' }, // ... ], } }, } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值