Vue中使用 ElementUi 的 el-select 實現(xiàn)全選功能(思路詳解)
前言:在開發(fā)中,有一個需求是 選項組件中使用到一個 全選的功能,特在這記錄下實現(xiàn)的方法,方便后續(xù)的查閱,以及方便大家查閱借鑒。若是有更好的方法,請大家傳授傳授。
效果圖:
思路:就圍繞是 ‘全選’ 還是 單選展開,用布爾字段 selectAll 來標識,selectAll 默認是 false 非全選。
全選分支:只有全選和取消權限操作。
單選分支:判斷下選擇到數(shù)組長度 是否 與所有選項長度一樣,是的話就說明是全選。否則就是單選。
直接上簡單的 demo:
<template> <div> <el-form ref="searchform" label-width="150px"> <el-form-item label="select多選demo:"> <el-select v-model="selectValue" @change="changeSelect" multiple clearable placeholder="請選擇" > <el-option key="selectAll" label="全部" value="selectAll"></el-option> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" > </el-option> </el-select> </el-form-item> </el-form> </div> </template> <script> export default { data() { return { options: [ { value: '選項1', label: '黃金糕' }, { value: '選項2', label: '雙皮奶' }, { value: '選項3', label: '蚵仔煎' }, { value: '選項4', label: '龍須面' }, { value: '選項5', label: '北京烤鴨' } ], selectValue: [], selectAll: false // 用于標識是否全選--默認不全選 } }, created() { console.log(...this.options) console.log(this.options.filter(p => p.value != 'selectAll')) }, methods: { changeSelect(value) { // selectAll 為true 的時候,就走全選分支,全選后出現(xiàn)的情況就是取消權限 if (this.selectAll) { this.selectAll = false if (value.indexOf('selectAll') > -1) { this.selectValue = value.filter(p => p != 'selectAll') } else { this.selectValue = [] } } else { // 是否點擊了‘全選'選項 if (value.indexOf('selectAll') > -1) { // 有‘全選'選項,則將‘全部'和其他值放置一塊 const optionsValue = [] this.options.forEach(item => { optionsValue.push(item.value) }) this.selectValue = ['selectAll', ...optionsValue] this.selectAll = true } else { // 若是勾選選擇的長度和提供的選項長度是一樣的,則是 ‘全選' if (value.length === this.options.length) { const optionsValue = [] this.options.forEach(item => { optionsValue.push(item.value) }) this.selectValue = ['selectAll', ...optionsValue] this.selectAll = true } else { // 都是單選 this.selectValue = value } } } // 真實的勾選值 const realSelect = this.selectValue.filter(item => item != 'selectAll') console.log('realSelect', realSelect) } } } </script> <style lang="scss" scoped></style>
這次的例子比較簡單,希望對您有幫助,謝謝支持。
到此這篇關于VUE中使用 ElementUi 的 el-select 實現(xiàn)全選功能的文章就介紹到這了,更多相關vue ElementUi 全選內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- vue之elementUi的el-select同時獲取value和label的三種方式
- vue3?element?plus?table?selection展示數(shù)據(jù),默認選中功能方式
- Vue?elementui如何實現(xiàn)表格selection的默認勾選
- vue-treeselect(適配Vue3.2)及Element-plus的TreeSelect組件使用
- vue elementui select標簽監(jiān)聽change事件失效問題
- vue2結合element-ui實現(xiàn)TreeSelect樹選擇功能
- Vue Element如何獲取select選擇框選擇的value和label
相關文章
vue封裝自定義指令之動態(tài)顯示title操作(溢出顯示,不溢出不顯示)
這篇文章主要介紹了vue封裝自定義指令之動態(tài)顯示title操作(溢出顯示,不溢出不顯示),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11uniapp使用webview內(nèi)嵌H5的注意事項詳解
在移動應用開發(fā)中,uniApp框架提供了一種跨平臺的解決方案,允許開發(fā)者使用一套代碼來構建iOS、Android等多平臺的應用,這篇文章主要給大家介紹了關于uniapp使用webview內(nèi)嵌H5的注意事項,需要的朋友可以參考下2024-07-07