123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <template>
- <div :class="$style.unitSelect">
- <a-select
- ref="select"
- v-model="onKeys"
- mode="multiple"
- :max-tag-count="2"
- @focus="focus"
- @change="selectChange"
- >
- <a-select-option v-for="(item, i) in mockData" :key="i" :value="item.key">{{
- item.title
- }}</a-select-option>
- </a-select>
- <a-modal v-model="showTree" title="请选择" @ok="ok" @cancel="cancel">
- <!-- 单选 -->
- <div :class="$style.tree">
- <a-input-search
- style="margin-bottom: 8px"
- placeholder="搜索"
- allow-clear
- @change="onChange"
- />
- <a-radio-group v-if="single" v-model="onGroupKey">
- <a-tree
- v-model="checkedKeys"
- :expanded-keys="expandedKeys"
- :auto-expand-parent="autoExpandParent"
- :selected-keys="selectedKeys"
- :tree-data="treeData"
- :load-data="onLoadData"
- :default-expanded-keys="['100000']"
- :default-expand-parent="true"
- multiple
- show-icon
- @expand="onExpand"
- @select="onSelect"
- @check="onCheck"
- >
- <template slot="custom" slot-scope="{ key }">
- <a-radio :value="key"></a-radio>
- </template>
- </a-tree>
- </a-radio-group>
- <!-- 多选 -->
- <a-checkbox-group v-else v-model="onCheckBoxGroupKey">
- <a-tree
- v-model="checkedKeys"
- :expanded-keys="expandedKeys"
- :auto-expand-parent="autoExpandParent"
- :selected-keys="selectedKeys"
- :tree-data="treeData"
- :load-data="onLoadData"
- :default-expanded-keys="['100000']"
- :default-expand-parent="true"
- multiple
- show-icon
- @expand="onExpand"
- @select="onSelect"
- @check="onCheck"
- >
- <template slot="custom" slot-scope="{ key }">
- <a-checkbox :value="key"></a-checkbox>
- </template>
- </a-tree>
- </a-checkbox-group>
- </div>
- </a-modal>
- </div>
- <!-- 分级数据 -->
- </template>
- <script>
- import components from './_import-components/audit-unit-select-import'
- import AuditPortraitService from './audit-portrait-service'
- export default {
- name: 'AuditUnitSelect',
- metaInfo: {
- title: 'AuditUnitSelect',
- },
- components,
- model: {
- prop: 'value',
- event: 'change',
- },
- props: {
- value: {
- type: Array,
- default: () => [],
- },
- single: {
- type: Boolean,
- default: true,
- },
- },
- data() {
- return {
- // 弹窗
- showTree: false,
- onKeys: [],
- mockData: [],
- checkedKeys: [],
- expandedKeys: ['100000'],
- autoExpandParent: true,
- selectedKeys: [],
- treeData: [],
- searchValue: '',
- onGroupKey: '',
- onCheckBoxGroupKey: [],
- }
- },
- created() {
- this.initTreeData()
- },
- methods: {
- selectChange(e) {
- this.onKeys = e
- const isArr = this.mockData.filter((val) => this.onKeys.includes(val.key))
- const arr = isArr.map((val) => ({
- orgCode: val.key + '',
- name: val.title,
- }))
- this.$emit('change', arr)
- },
- ok() {
- this.showTree = false
- // 寻找对应的name
- this.onKeys = this.single ? [this.onGroupKey] : this.onCheckBoxGroupKey
- // 遍历数组看是否有 100000
- // 如果没有则插入
- const isType = this.mockData.find((val) => val.key === '100000')
- if (!isType) {
- this.mockData.push({
- key: '100000',
- title: '国家电力投资集团有限公司',
- lev: 1,
- isLeaf: false,
- parent: 'root',
- scopedSlots: { icon: 'custom' },
- })
- }
- const isArr = this.mockData.filter((val) => this.onKeys.includes(val.key))
- const arr = isArr.map((val) => ({
- orgCode: val.key + '',
- name: val.title,
- }))
- this.$emit('change', arr)
- },
- focus() {
- this.showTree = true
- this.onCheckBoxGroupKey = this.onKeys
- this.onGroupKey = this.onKeys[0] || ''
- // 展开第一个
- this.expandedKeys = ['100000']
- // 去除焦点
- this.$refs.select.blur()
- },
- cancel() {
- this.showTree = false
- // 清空组件内数据
- this.checkedKeys = []
- this.expandedKeys = []
- this.autoExpandParent = true
- this.selectedKeys = []
- this.onGroupKey = ''
- },
- initTreeData() {
- // 100000
- const params = {
- mdgOrgCode: '100000',
- lev: 1,
- }
- // 国家电力投资集团有限公司
- AuditPortraitService.getGroupsByCode(params).then((res) => {
- this.treeData = [
- {
- key: '100000',
- title: '国家电力投资集团有限公司',
- lev: 1,
- scopedSlots: { icon: 'custom' },
- children: res.data.map((res) => {
- return {
- key: res.code,
- title: res.name,
- parent: '100000',
- isLeaf: false,
- lev: 2,
- scopedSlots: { icon: 'custom' },
- }
- }),
- },
- ]
- this.mockData = []
- this.mockData.push({
- key: '100000',
- title: '国家电力投资集团有限公司',
- lev: 1,
- isLeaf: false,
- parent: 'root',
- scopedSlots: { icon: 'custom' },
- })
- this.mockData = res.data.map((res) => {
- return {
- key: res.code + '',
- title: res.name,
- parent: '100000',
- isLeaf: false,
- lev: 2,
- scopedSlots: { icon: 'custom' },
- }
- })
- // 判断value是否有值如果有值且mock中没有该值则添加
- this.value.forEach((val) => {
- if (!this.mockData.find((item) => item.key === val.orgCode)) {
- this.mockData.push({
- key: val.orgCode + '',
- title: val.name,
- parent: '',
- isLeaf: false,
- lev: 2,
- scopedSlots: { icon: 'custom' },
- })
- }
- })
- this.$nextTick(() => {
- if (this.value.length > 0) {
- this.onKeys = [this.value[0].orgCode]
- const isArr = this.mockData.filter((val) => this.onKeys.includes(val.key))
- const arr = isArr.map((val) => ({
- orgCode: val.key + '',
- name: val.title,
- }))
- this.$emit('change', arr)
- }
- })
- })
- },
- onLoadData(treeNode) {
- return new Promise((resolve) => {
- if (treeNode.dataRef.children) {
- return resolve()
- }
- const params = {
- mdgOrgCode: treeNode.dataRef.key,
- lev: treeNode.dataRef.lev,
- }
- AuditPortraitService.getGroupsByCode(params).then((res) => {
- treeNode.dataRef.children = res.data.map((res) => {
- return {
- key: res.code + '',
- title: res.name,
- isLeaf: true,
- lev: treeNode.dataRef.lev,
- parent: treeNode.dataRef.key,
- scopedSlots: { icon: 'custom' },
- }
- })
- // 遍历遍历数组如果res.data中有value的值则删除mockData中的该值
- this.value.forEach((val) => {
- if (res.data.find((item) => item.code === val.orgCode)) {
- this.mockData = this.mockData.filter((item) => item.key !== val.orgCode)
- }
- })
- this.mockData = [
- ...this.mockData,
- ...res.data.map((res) => {
- return {
- key: res.code + '',
- title: res.name,
- parent: treeNode.dataRef.key,
- isLeaf: true,
- lev: treeNode.dataRef.lev,
- scopedSlots: { icon: 'custom' },
- }
- }),
- ]
- resolve()
- })
- })
- },
- onExpand(expandedKeys) {
- this.expandedKeys = expandedKeys
- this.autoExpandParent = false
- },
- onSelect(selectedKeys) {
- console.log('🚀 ~ onCheck ~ checkedKeys:', selectedKeys)
- if (this.single) {
- this.onGroupKey = selectedKeys[0]
- } else {
- if (this.onCheckBoxGroupKey.includes(selectedKeys[0])) {
- this.onCheckBoxGroupKey = this.onCheckBoxGroupKey.filter(
- (item) => item !== selectedKeys[0]
- )
- } else {
- this.onCheckBoxGroupKey = [...this.onCheckBoxGroupKey, selectedKeys[0]]
- }
- }
- this.selectedKeys = []
- // this.onGroupKey = selectedKeys[0]
- // 寻找父节点 展开
- // this.expandedKeys = [
- // '100000',
- // ...this.mockData.filter((item) => item.key === selectedKeys[0]).map((item) => item.parent),
- // ]
- // 只能选择一个节点
- },
- onCheck(checkedKeys) {
- this.checkedKeys = checkedKeys
- if (checkedKeys.length > 1 && this.single) {
- this.checkedKeys = [this.checkedKeys[checkedKeys.length - 1]]
- } else {
- this.onCheckBoxGroupKey = checkedKeys
- this.checkedKeys = checkedKeys
- }
- },
- onChange(e) {
- const { value } = e.target
- const selectedKeys = this.mockData
- .map((item) => {
- if (item.title.indexOf(value) > -1) {
- return item
- }
- return null
- })
- .filter((item) => item)
- console.log('🚀 ~ onChange ~ selectedKeys:', selectedKeys)
- const dataTree = [
- {
- key: '100000',
- title: '国家电力投资集团有限公司',
- lev: 1,
- scopedSlots: { icon: 'custom' },
- children: [],
- },
- ]
- selectedKeys.map((val) => {
- if (val.lev === 2) {
- dataTree[0].children.push(val)
- }
- })
- dataTree[0].children.forEach((val2) => {
- selectedKeys.map((val3) => {
- if (val2.lev === 3 && val2.key === val3.parent) {
- val2.children.push(val3)
- }
- })
- })
- console.log('🚀 ~ onChange ~ dataTree:', dataTree)
- this.treeData = dataTree
- },
- },
- }
- </script>
- <style module lang="scss">
- @use '@/common/design' as *;
- .unit-select {
- min-width: 200px;
- }
- .tree {
- height: 500px;
- // 滚动
- overflow-y: auto;
- }
- </style>
|