elementui el-select设置默认值且默认值不允许删除
参考网址:【记录】el-select 已选项禁止删除 el-select编辑时已选择的项不允许删除 、element-ui里面的下拉多选框 el-select 时,默认值不可删除
在项目中 el-select 设置默认值且默认值不允许删除和取消选中
通过vue全局指令实现该要求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | export default { update(el, bindings) { const [defaultValues, selectValue] = bindings.value // 需要隐藏的标签索引 const indexs = [] selectValue.map((item, index) => { defaultValues.map(defaultItem => { if (item == defaultItem) { indexs.push(index) } }) }) const dealStyle = function (tags) { tags.forEach((el, index) => { if ( indexs.includes(index) && ![...el.classList].includes( 'select-tag-close-none' ) ) { el.style.display = 'none' // close 图标隐藏掉 } }) } // 设置样式隐藏close icon let tags = el.querySelectorAll( '.el-tag__close' ) if (tags.length === 0) { // 初始化tags为空处理 setTimeout(() => { let tagTemp = el.querySelectorAll( '.el-tag__close' ) dealStyle(tagTemp) }) } else { setTimeout(() => { dealStyle(tags) }) } } } |
指令文件夹下新建index.js文件
1 2 3 4 5 6 7 8 9 10 11 12 | import defaultSelect from './select/defaultSelect' const install = function (Vue) { // Vue.directive('xxx', xxx) // 其他指令 Vue.directive( 'defaultSelect' , defaultSelect) } if (window.Vue) { Vue.use(install) } export default install |
在main.js 导入指令
1 2 3 | import directive from './directive' Vue.use(directive) |
在页面中使用指令
// 设置已选择的不可删除 selectIdList:选中值数组[1,2,3] form.selectId选中值id字符串,逗号分割 1,2,3 v-default-select="[selectIdList,form.selectId]" // 设置已选择的不可操作 :disabled=“selectIdList.includes(dict.value)”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <el-select ref= "efficacyIdSelect" v-model= "form.selectId" v- default -select= "[selectIdList,form.selectId]" multiple filterable placeholder= "请选择" class = "select-width" @keydown.native. delete .capture.stop.prevent @change= "$forceUpdate()" > <el-option v- for = "dict in dict.List" :key= "dict.value" :label= "dict.label" :value= "dict.value" :disabled= "selectIdList.includes(dict.value)" /> </el-select> data(){ return { form:{}, selectIdList:[] } } |
分类:
elementUI
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通