cmc-web/packages/common/components/group-transfer/index.vue

192 lines
4.3 KiB
Vue
Raw Normal View History

2024-08-20 12:11:31 +00:00
<template>
<el-row :gutter="10">
<el-col :span="12">
<el-card>
<div slot="header">
2024-08-21 01:17:14 +00:00
<span>{{ titles[0] }}</span>
<el-button type="primary" class="pull-right" style="margin-top: 5px" size="mini" @click="addItems"
>移入
2024-08-20 12:11:31 +00:00
<i class="el-icon-right"></i>
</el-button>
</div>
2024-08-21 01:17:14 +00:00
<div style="height: 360px; overflow-y: auto">
2024-08-20 12:11:31 +00:00
<el-row v-for="(item, key) in source" :key="key">
2024-08-21 01:17:14 +00:00
<el-col :span="24" class="cell-title">{{ prefix }}{{ key }}</el-col>
<el-col class="cell" :span="24" v-for="(cell, key) in item" :key="key">
<el-checkbox v-model="cell.checked">{{ cell[label] }}</el-checkbox>
2024-08-20 12:11:31 +00:00
</el-col>
</el-row>
</div>
</el-card>
</el-col>
<el-col :span="12">
<el-card>
<div slot="header">
2024-08-21 01:17:14 +00:00
<span class="pull-right">{{ titles[1] }}</span>
2024-08-20 12:11:31 +00:00
<el-button type="danger" size="mini" @click="removeItems">
<i class="el-icon-back"></i>
移除
</el-button>
</div>
2024-08-21 01:17:14 +00:00
<div style="height: 360px; overflow-y: auto">
2024-08-20 12:11:31 +00:00
<el-row v-for="(item, key) in target" :key="key">
2024-08-21 01:17:14 +00:00
<el-col :span="24" class="cell-title">{{ prefix }}{{ key }}</el-col>
2024-08-20 12:11:31 +00:00
<el-col class="cell" :span="24" v-for="(cell, key) in item" :key="`${key}${item.name}`">
2024-08-21 01:17:14 +00:00
<el-checkbox v-model="cell.checked">{{ cell[label] }}</el-checkbox>
2024-08-20 12:11:31 +00:00
</el-col>
</el-row>
</div>
</el-card>
</el-col>
</el-row>
</template>
<script>
export default {
model: {
prop: 'checkedIds',
event: 'change'
},
props: {
titles: {
type: Array,
default: function () {
return ['列表1', '列表2']
}
},
label: {
type: String,
default: 'name'
},
value: {
type: String,
default: 'id'
},
data: {
type: Array,
required: true
},
groupKey: {
type: String,
default: 'group'
},
prefix: {
type: String,
default: ''
},
checkedIds: {
type: Array,
default: function () {
return []
}
}
},
2024-08-21 01:17:14 +00:00
data() {
2024-08-20 12:11:31 +00:00
return {
selectList: [],
sourceList: []
}
},
2024-08-21 01:17:14 +00:00
created() {
2024-08-20 12:11:31 +00:00
this.init()
},
watch: {
data: {
2024-08-21 01:17:14 +00:00
handler(newVal, oldVal) {
this.selectList = []
this.sourceList = []
2024-08-20 12:11:31 +00:00
this.init()
}
}
},
computed: {
2024-08-21 01:17:14 +00:00
source() {
2024-08-20 12:11:31 +00:00
return this.formatData(this.sourceList)
},
2024-08-21 01:17:14 +00:00
target() {
2024-08-20 12:11:31 +00:00
return this.formatData(this.selectList)
}
},
methods: {
2024-08-21 01:17:14 +00:00
init() {
;[...this.data].forEach((item, key) => {
2024-08-20 12:11:31 +00:00
const result = {
...item,
checked: false,
orderKey: key
}
if (this.checkedIds.includes(item[this.value])) {
this.selectList.push(result)
} else {
this.sourceList.push(result)
}
})
},
2024-08-21 01:17:14 +00:00
formatData(data) {
2024-08-20 12:11:31 +00:00
const source = {}
data.forEach(item => {
const key = item[this.groupKey]
if (source[key]) {
source[key].push(item)
} else {
source[key] = [item]
}
})
return source
},
2024-08-21 01:17:14 +00:00
addItems() {
const list = []
2024-08-20 12:11:31 +00:00
this.sourceList.forEach(item => {
if (item.checked) {
this.selectList.push({
...item,
checked: false
})
} else {
list.push(item)
}
})
this.sourceList = [...list].sort((a, b) => {
return a.orderKey - b.orderKey
2024-08-21 01:17:14 +00:00
})
2024-08-20 12:11:31 +00:00
this.getChekckIds()
},
2024-08-21 01:17:14 +00:00
removeItems() {
const list = []
2024-08-20 12:11:31 +00:00
this.selectList.forEach(item => {
if (item.checked) {
this.sourceList.push({
...item,
checked: false
})
} else {
list.push(item)
}
})
2024-08-21 01:17:14 +00:00
this.selectList = [...list]
2024-08-20 12:11:31 +00:00
this.getChekckIds()
},
2024-08-21 01:17:14 +00:00
getChekckIds() {
2024-08-20 12:11:31 +00:00
const checkedIds = this.selectList.map(item => item[this.value])
this.$emit('change', checkedIds)
}
}
}
</script>
<style scoped>
2024-08-21 01:17:14 +00:00
.cell-title {
font-size: 12px;
font-weight: bold;
}
.cell {
padding: 5px;
color: #606266;
font-weight: 500;
white-space: nowrap;
font-size: 14px;
overflow: hidden;
text-overflow: ellipsis;
}
2024-08-20 12:11:31 +00:00
</style>