acro 实现树形结构 前端搜索和样式设计

本文详细介绍了如何在Vue应用中使用复合tree组件,包括搜索功能、节点拖拽、选中事件处理以及编辑、新增和删除操作。展示了如何使用`v-model`,`computed`和自定义模板来构建可交互的树形结构。
摘要由CSDN通过智能技术生成
```yaml
title:
  zh-CN: 复合 tree
  en-US: composite tree
<template>
  <div class="tree_style">
    <ga-input-search placeholder="请输入内容" style="margin-bottom: 8px;" v-model="searchKey" />
    <ga-tree
      class="tree-node-title-text-block"
      block-node
      extra-mouseenter-show
      draggable
      :data="treeData"
      :default-selected-keys="['all']"
      :field-names="{ key: 'key', title: 'title', children: 'children' }"
      @select="handleOnSelect"
      @drop="handleOnDrop"
    >
      <template #drag-icon></template>
      <template #title="nodeData">
        <div class="tree-title-operation">
          <div class="tree-title-text">{{ nodeData.title }}</div>
          <div v-if="!['all', 'undistributed'].includes(nodeData.id)" class="tree-title-extra">
            <ga-space>
              <icon-edit class="icon_color" @click="handleEdit(nodeData)"></icon-edit>
              <icon-delete class="icon_color" @click="handleDelete(nodeData)"></icon-delete>
              <icon-plus class="icon_color" @click="() => handleAdd(nodeData)" />
            </ga-space>
          </div>
        </div>
      </template>
    </ga-tree>
  </div>
</template>
<script setup>
import { reactive, ref, computed } from 'vue';
// 搜索
const searchKey = ref('');
const treeData = computed(() => {
  if (!searchKey.value) return treeArr;
  return searchData(searchKey.value);
});
const searchData = (keyword) => {
  const loop = (data) => {
    const result = [];
    data.forEach((item) => {
      if (item.title.toLowerCase().indexOf(keyword.toLowerCase()) > -1) {
        result.push({ ...item });
      } else if (item.children) {
        const filterData = loop(item.children);
        if (filterData.length) {
          result.push({
            ...item,
            children: filterData,
          });
        }
      }
    });
    return result;
  };
  return loop(treeArr);
};
// 当前选中项
const handleOnSelect = (val) => {
  console.log('当前选中项---', val);
};
// 拖拽
const handleOnDrop = (val) => {
  console.log('拖拽---', val);
};
// 编辑
const handleEdit = (val) => {
  console.log('编辑---', val);
};
// 新增
const handleAdd = (val) => {
  console.log('新增---', val);
};
// 删除
const handleDelete = (val) => {
  console.log('删除---', val);
};
const baseTreeData = reactive([{ title: '全部', key: 'all', draggable: false }]);
const data = reactive([
  {
    level: 1,
    title: '卡园数据中心',
    key: '0',
    children: [
      {
        title: '哈哈哈哈',
        key: '011',
      },
      {
        title: '恍恍惚惚',
        key: '022',
      },
    ],
  },
  {
    level: 1,
    title: '金桥数据中心',
    key: '1',
    children: [
      {
        title: '业务支撑区',
        key: '122',
      },
      {
        title: '网上交易区',
        key: '133',
      },
    ],
  },
]);
const treeArr = [...baseTreeData, ...data]

</script>

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值