| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <template>
- <div class="select-checked">
- <!-- 下拉加多选框 -->
- <el-select
- v-model="value"
- multiple
- collapse-tags
- placeholder="请选择"
- :popper-append-to-body="false"
- @remove-tag="removeTag"
- size="small"
- style="width:384px;"
- >
- <el-option
- v-for="item in options"
- :key="item.value"
- :label="item.label"
- :value="item.value"
- >
- <el-checkbox v-model="item.check" @change="isCheck(item)">
- {{ item.label }}
- </el-checkbox>
- </el-option>
- </el-select>
- <!-- {{ value }} -->
- </div>
- </template>
-
- <script>
- export default {
- name: 'SelectChecked',
- components: {},
- props: {
- options: {
- type: Array
- }
- },
- data() {
- return {
- value: []
- }
- },
- watch:{
- options:{
- handler(newVal) {
- const allChecked = newVal.every(option => option.value == 'all' || option.check);
- const allOption = newVal.find(option => option.value == 'all');
- if (allOption) {
- allOption.check = allChecked;
- }
- },
- deep: true // 深度监听数组的内部对象变化
- }
- },
- methods: {
- // 多选框触发
- isCheck(item) {
- if (item.check && item.value == 'all') {
- this.value = []
- this.options.forEach(element => {
- element.check = true
- this.value.push(element.value)
- })
- } else if (!item.check && item.value == 'all') {
- this.value = []
- this.options.forEach(element => {
- element.check = false
- })
- }
- if (
- item.check &&
- this.value.indexOf(item.value) == -1 &&
- item.value !== 'all'
- ) {
- this.value.forEach((elm, idx) => {
- if (elm == 'all') {
- this.value.splice(idx, 1)
- }
- })
- this.value.push(item.value)
- if (this.value.length == this.options.length - 1) {
- this.options[0].check = true
- this.value.unshift('all')
- } else {
- this.options[0].check = false
- }
- } else if (!item.check && item.value !== 'all') {
- this.options[0].check = false
- this.value.forEach((elm, idx) => {
- if (elm == item.value || elm == 'all') {
- this.value.splice(idx, 1)
- }
- })
- }
- this.$emit('selectedVal', this.value)
- },
- // 多选模式下移除tag时触发
- removeTag(value) {
- if (value == 'all') {
- this.options.forEach((elm, idx) => {
- elm.check = false
- })
- this.value = []
- } else {
- this.options.forEach((elm, idx) => {
- if (elm.value == value || elm.value == 'all') {
- elm.check = false
- }
- })
- }
- this.$emit('selectedVal', this.value)
- },
- clearSelection() {
- this.value = [];
- this.options.forEach((elm, idx) => {
- elm.check = false
- })
- this.$emit('selectedVal', this.value);
- }
- }
- }
- </script>
-
- <style lang="scss">
- .select-checked {
- .el-select-dropdown.is-multiple .el-select-dropdown__item.selected::after {
- content: '';
- }
- .el-checkbox {
- width: 100%;
- padding: 0 30px;
- .el-checkbox__label {
- margin-left: 20px;
- }
- }
- .el-select-dropdown__item {
- padding: 0;
- }
- }
- </style>
|