123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- <template>
- <ElTree :key="treeKey" ref="ElTreeRef"
- :class="[ui,submitCounts?'tree-line1':'']" :default-expanded-keys="defaultExpandedCids" :indent="0"
- :load="ElTreeLoadNode"
- :props="ElTreeProps" accordion
- class="hc-tree-node tree-line el-radio-group" highlight-current
- lazy node-key="primaryKeyId" @node-click="ElTreeClick"
- @node-contextmenu="ElTreeLabelContextMenu">
- <template #default="{ node, data }">
- <div :id="`${idPrefix}${data['primaryKeyId']}`" class="data-custom-tree-node">
- <!--树组件,节点名称-->
- <div :class="node.level === 1?'level-name':''" class="label">
- <span
- v-if="isColor"
- :class="data?.colorStatus === 2?'text-blue':data?.colorStatus === 3?'text-orange':data?.colorStatus === 4?'text-green':''">{{
- node.label
- }}</span>
- <span v-else>{{ node.label }}</span>
- </div>
- <!--树组件,统计数量-->
- <div v-if="isSubmitCounts" class="text-blue submit-counts">
- 【{{ data.submitCounts ?? 0 }}】
- </div>
- <!--树组件,操作菜单-->
- <div v-if="node.level !== 1 && menusData.length > 0" :class="node.showTreeMenu?'show':''"
- class="menu-icon1">
- <div class="cu-tree-node-popover-menu-icon"
- @click.prevent.stop="ElTreeLabelContextMenu($event,data,node)">
- <HcIcon name="apps" ui="text-2xl"/>
- </div>
- </div>
- <!--树组件,操作菜单 END-->
- </div>
- </template>
- </ElTree>
- <!--右键菜单-->
- <HcContextMenu v-if="menusData.length > 0" ref="contextMenuRef" :datas="menusData" @closed="handleMenuClosed"
- @item-click="handleMenuSelect">
- <template #mark="{item}">
- <HcIcon :fill="treeRefData?.isFirst" :name="item.icon" class="menu-item-icon"/>
- <span class="menu-item-name">{{ treeRefData?.isFirst ? '取消标记为首件' : '标记为首件' }}</span>
- </template>
- <template #sort="{item}">
- <HcIcon :line="false" :name="item.icon" class="menu-item-icon"/>
- <span class="menu-item-name">{{ item.label }}</span>
- </template>
- </HcContextMenu>
- </template>
- <script setup>
- import {ref, nextTick, watch} from "vue";
- import dataFillQuery from '~api/data-fill/query';
- import {isItem, getArrValue, getObjValue, isValueNull} from "vue-utils-plus"
- //参数
- const props = defineProps({
- menus: {
- type: Array,
- default: () => ([])
- },
- projectId: {
- type: [String, Number],
- default: ''
- },
- contractId: {
- type: [String, Number],
- default: ''
- },
- autoExpandKeys: {
- type: Array,
- default: () => ([])
- },
- isMark: {
- type: Boolean,
- default: false
- },
- idPrefix: {
- type: String,
- default: 'wbs-tree-'
- },
- isAutoKeys: {
- type: Boolean,
- default: true
- },
- isAutoClick: {
- type: Boolean,
- default: true
- },
- isColor: {
- type: Boolean,
- default: false
- },
- ui: {
- type: String,
- default: ''
- },
- submitCounts: {
- type: Boolean,
- default: false
- },
- classifyType: {
- type: String,
- },
- treeKey: {
- type: [String, Number],
- }
- })
- //变量
- const ElTreeRef = ref(null)
- const treeRefNode = ref(null)
- const treeRefData = ref(null)
- const ElTreeProps = ref({
- label: 'title',
- children: 'children',
- isLeaf: 'notExsitChild'
- })
- const menusData = ref(props.menus)
- const menuMark = ref(props.isMark)
- const isAutoKeys = ref(props.isAutoKeys)
- const TreeExpandKey = ref(props.autoExpandKeys)
- const projectId = ref(props.projectId);
- const contractId = ref(props.contractId);
- const idPrefix = ref(props.idPrefix);
- const isSubmitCounts = ref(props.submitCounts);
- const classifyTypedata = ref(props.classifyType);
- const treeKeyData = ref(props.treeKey)
- //监听
- watch(() => [
- props.menus,
- props.isMark,
- props.isAutoKeys,
- props.autoExpandKeys,
- props.projectId,
- props.contractId,
- props.idPrefix,
- props.submitCounts,
- props.classifyType,
- props.treeKey,
- ], ([menus, isMark, AutoKeys, expandKeys, UserProjectId, UserContractId, UserIdPrefix, submitCounts, ClassifyType, TreeKey]) => {
- menusData.value = menus
- menuMark.value = isMark
- isAutoKeys.value = AutoKeys
- TreeExpandKey.value = expandKeys
- projectId.value = UserProjectId
- contractId.value = UserContractId
- idPrefix.value = UserIdPrefix
- isSubmitCounts.value = submitCounts
- classifyTypedata.value = ClassifyType
- treeKeyData.value = TreeKey
- })
- //事件
- const emit = defineEmits(['menuTap', 'nodeTap', 'nodeLoading'])
- //树形结构异步加载数据
- const defaultExpandedCids = ref([])
- const rootNode = ref({})
- const rootResolve = ref(null)
- const ElTreeLoadNode = async (node, resolve) => {
- let contractIdRelation = '', parentId = '', primaryKeyId = '';
- if (node.level !== 0) {
- rootNode.value = node
- rootResolve.value = resolve
- const nodeData = getObjValue(node?.data);
- contractIdRelation = nodeData?.contractIdRelation || ''
- parentId = contractIdRelation ? nodeData?.primaryKeyId : nodeData?.id
- primaryKeyId = nodeData?.id || ''
- }
- //获取数据
- const {error, code, data} = await dataFillQuery.queryWbsTreeData({
- contractId: contractId.value || '',
- contractIdRelation,
- primaryKeyId,
- parentId,
- classifyType: classifyTypedata.value
- })
- //处理数据
- if (!error && code === 200) {
- let clickKey = '', defaultExpandedArr = [];
- const keys = TreeExpandKey.value || []
- const resData = getArrValue(data)
- if (keys.length > 0) {
- let lastKey = keys[keys.length - 1];
- for (const item of resData) {
- //自动展开
- if (isItem(keys, item?.primaryKeyId)) {
- defaultExpandedArr.push(item?.primaryKeyId)
- }
- //最后一个,选中点击
- if (item?.primaryKeyId === lastKey) {
- clickKey = item?.primaryKeyId
- }
- }
- } else if (node.level === 0) {
- defaultExpandedArr.push(resData[0]?.primaryKeyId)
- }
- //自动展开
- defaultExpandedCids.value = defaultExpandedArr
- if (node.level === 0) {
- emit('nodeLoading')
- }
- resolve(resData)
- //最后一个,执行点击
- if (props.isAutoClick && clickKey) {
- await nextTick(() => {
- document.getElementById(`${idPrefix.value}${clickKey}`)?.click()
- })
- }
- } else {
- if (node.level === 0) {
- emit('nodeLoading')
- }
- resolve([])
- }
- }
- watch(classifyTypedata, (val) => {
- if (val) {
- classifyTypedata.value = val
- }
- },
- {immediate: true}
- )
- //节点被点击
- const ElTreeClick = async (data, node) => {
- if (isAutoKeys.value) {
- let autoKeysArr = []
- await getNodeExpandKeys(node, autoKeysArr)
- const autoKeys = autoKeysArr.reverse()
- emit('nodeTap', {node, data, keys: autoKeys})
- } else {
- emit('nodeTap', {node, data, keys: []})
- }
- }
- //处理自动展开的节点KEY
- const getNodeExpandKeys = async (node, newKeys) => {
- const parent = node?.parent ?? []
- const primaryKeyId = node?.data?.primaryKeyId ?? ''
- if (primaryKeyId) {
- newKeys.push(primaryKeyId)
- await getNodeExpandKeys(parent, newKeys)
- }
- }
- //鼠标右键事件
- const contextMenuRef = ref(null)
- const ElTreeLabelContextMenu = (e, data, node) => {
- const rows = menusData.value || [];
- if (node.level !== 1 && rows.length > 0) {
- e.preventDefault();
- treeRefNode.value = node;
- treeRefData.value = data;
- node.showTreeMenu = true
- //展开菜单
- contextMenuRef.value?.showMenu(e)
- }
- }
- //鼠标右键菜单被点击
- const handleMenuSelect = async ({key}) => {
- const node = treeRefNode.value;
- const data = treeRefData.value;
- //如果为标记菜单
- if (key === 'mark' && menuMark.value) {
- if (data.isFirst === true) {
- emit('menuTap', {key: 'cancel_mark', node, data})
- } else {
- emit('menuTap', {key: 'mark', node, data})
- }
- } else {
- if (isAutoKeys.value) {
- let autoKeysArr = []
- await getNodeExpandKeys(node, autoKeysArr)
- const autoKeys = autoKeysArr.reverse()
- emit('menuTap', {key, node, data, keys: autoKeys})
- }
- }
- }
- const handleMenuClosed = () => {
- const node = treeRefNode.value;
- if (!isValueNull(node)) {
- treeRefNode.value['showTreeMenu'] = false
- }
- }
- //设置树菜单的标记数据
- const setElTreeMenuMark = (keys, isFirst) => {
- keys.forEach(item => {
- //根据 data 或者 key 拿到 Tree 组件中的 node
- let node = ElTreeRef.value.getNode(item)
- if (!!node) node.data.isFirst = isFirst;
- })
- }
- //设置树菜单的标记数据
- const removeElTreeNode = (key) => {
- //根据 data 或者 key 拿到 Tree 组件中的 node
- let node = ElTreeRef.value.getNode(key)
- //删除 Tree 中的一个节点,使用此方法必须设置 node-key 属性
- ElTreeRef.value.remove(node)
- }
- // 暴露出去
- defineExpose({
- setElTreeMenuMark,
- removeElTreeNode,
- // resetNode
- })
- </script>
- <style lang="scss" scoped>
- @import "../../../styles/app/tree.scss";
- .el-radio-group {
- width: 100% !important;
- display: inline-grid;
- }
- .data-custom-tree-node {
- flex: 1;
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- .label {
- flex: 1;
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- }
- .submit-counts {
- position: unset;
- font-size: 14px;
- }
- .menu-icon1 {
- position: unset;
- vertical-align: bottom;
- display: inline-block;
- pointer-events: none;
- transition: opacity 0.2s;
- opacity: 0;
- right: 0;
- background: rgba(255, 255, 255, 0.25);
- border-radius: 2px;
- .cu-tree-node-popover-menu-icon {
- display: flex;
- align-items: center;
- justify-content: center;
- }
- }
- &:hover {
- .menu-icon1 {
- opacity: 1;
- pointer-events: all;
- cursor: context-menu;
- }
- }
- .menu-icon1.show {
- opacity: 1;
- pointer-events: all;
- cursor: context-menu;
- }
- }
- </style>
- <style lang="scss">
- .el-tree.hc-tree-node .el-tree-node {
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- .el-tree-node_content {
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- }
- }
- </style>
|