123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <template>
- <view class="hc-tree-box">
- <template v-if="nodeData.length > 0" v-for="item in nodeData" :key="item.key">
- <hc-tree-node ref="treeNodeRef"
- :item="item"
- :check="isCheck"
- :strictly="isStrictly"
- :radio="isRadio"
- :counts="isCounts"
- :currentKey="currentNodeKey"
- @nodeLoad="treeLoadTap"
- @nodeTap="treeNodeTap"
- />
- </template>
- <template v-else>
- <view class="no-data">暂无相关数据</view>
- </template>
- </view>
- </template>
- <script setup>
- import {getCurrentInstance, nextTick, onMounted, ref, watch} from 'vue'
- import {getArrValue, isNullES} from "js-fast-way";
- const instance = getCurrentInstance().proxy
- //参数
- const props = defineProps({
- nodeKey: {
- type: String,
- default: 'primaryKeyId',
- },
- labelKey: {
- type: String,
- default: 'title',
- },
- leafKey: {
- type: String,
- default: 'notExsitChild',
- },
- check: {
- type: Boolean,
- default: false,
- },
- //父子是否不关联
- strictly: {
- type: Boolean,
- default: false,
- },
- checkKey: {
- type: Array,
- default: () => ([]),
- },
- radio: {
- type: Boolean,
- default: false,
- },
- radioKey: {
- type: String,
- default: '',
- },
- currentKey: {
- type: String,
- default: '',
- },
- counts: {
- type: Boolean,
- default: false,
- },
- })
- //节点数据
- const treeNodeRef = ref(null)
- const nodeData = ref([]);
- //参数配置
- const isCheck = ref(props.check)
- const isRadio = ref(props.radio)
- const isStrictly = ref(props.strictly)
- const isCounts = ref(props.counts)
- const currentNodeKey = ref(props.currentKey)
- //事件
- const emit = defineEmits(['load', 'nodeTap'])
- //渲染完成
- onMounted(() => {
- getLazyLoad({
- level: 0,
- data: {},
- parentNodes: {},
- isLoad: false
- })
- })
- const treeLoadTap = (item) => {
- getLazyLoad(item)
- }
- //懒加载数据
- const getLazyLoad = (item) => {
- if (!item.isLoad) {
- emit('load', item, (data) => {
- setLazyLoad(item, data)
- })
- }
- }
- //处理获取到的数据
- const setLazyLoad = (data, arr) => {
- const children = getArrValue(arr), newNodeData = []
- if (children.length <= 0) {
- data.loading = false
- data.isExpand = false
- data.isLeaf = true
- data.isLoad = true
- //如果是跟节点,直接赋值
- if (data.level === 0) {
- nodeData.value = newNodeData
- }
- } else {
- for (let i = 0; i < children.length; i++) {
- const item = children[i]
- newNodeData.push({
- level: data.level + 1,
- key: item[props.nodeKey] ?? '',
- label: item[props.labelKey] ?? '',
- isLeaf: item[props.leafKey] ?? false,
- isExpand: false,
- loading: false,
- childNodes: [],
- isCheck: ifCheckData(data, item),
- isRadio: false,
- data: item,
- parentNodes: data,
- isLoad: item[props.leafKey] ?? false,
- })
- }
- //设置父级数据
- data.childNodes = newNodeData
- data.loading = false
- data.isExpand = setBrotherExpand(data.parentNodes)
- data.isLoad = true
- //如果是跟节点,直接赋值
- if (data.level === 0) {
- nodeData.value = newNodeData
- }
- }
- }
- //设置兄弟节点展开状态
- const setBrotherExpand = (nodes) => {
- const children = getArrValue(nodes.childNodes)
- for (let i = 0; i < children.length; i++) {
- nodes.childNodes[i].isExpand = false
- }
- return true
- }
- //0未选中 1选中 2半选
- const ifCheckData = (data, item) => {
- const keys = getArrValue(props.checkKey)
- if (keys.indexOf(item[props.nodeKey]) > -1) {
- return 1
- } else {
- return 0
- }
- }
- //树节点被点击
- const treeNodeTap = async (item) => {
- currentNodeKey.value = item.key
- emit('nodeTap', item)
- }
- //获取选中的key
- const getCheckKeys = async () => {
- const nodes = nodeData.value
- const nodeKeys = {
- nodes: [], keys: [],
- halfNodes: [], halfKeys: [],
- }
- if (!isCheck.value) {
- return nodeKeys
- }
- await forCheckKeys(nodes, nodeKeys)
- return nodeKeys
- }
- //遍历获取选中的key
- const forCheckKeys = async (nodes, nodeKeys) => {
- for (let i = 0; i < nodes.length; i++) {
- const item = nodes[i]
- //0未选中 1选中 2半选
- if (nodes[i].isCheck === 1) {
- nodeKeys.nodes.push(item)
- nodeKeys.keys.push(item.key)
- } else if (nodes[i].isCheck === 2) {
- nodeKeys.halfNodes.push(item)
- nodeKeys.halfKeys.push(item.key)
- }
- if (item.childNodes.length > 0) {
- await forCheckKeys(item.childNodes, nodeKeys)
- }
- }
- }
- //导出方法函数
- defineExpose({
- getCheckKeys
- })
- </script>
- <style lang="scss">
- @import "./style.scss";
- </style>
|