vue3 antd 树形组件,组件搜索基本用法

在这里插入图片描述

参考antd组件 https://www.antdv.com/components/tree-cn#components-tree-demo-search
1.组件结构代码

 <a-input placeholder="按enter键搜索" v-model:value="searchValue" />
 <a-tree  :tree-data="list" :expanded-keys="expandedKeys" :auto-expand-parent="autoExpandParent" @expand="onExpand">
      <template #title="{ title }">
        <span v-if="title.indexOf(searchValue) > -1">
          {{ title.substr(0, title.indexOf(searchValue)) }}
          <span style="color: #f50">{{ searchValue }}</span>
          {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
        </span>
        <span v-else>{{ title }}</span>
      </template>
 </tree>
<script>
	import { ref, watch } from 'vue';
	setup (props, context) {
		const list = ref([
		 {
	        key: 1,
	        title: '团队一',
	        children: [
          {
            key: 22,
            title: '研发',
            children: [
              {
                key: 23,
                title: '张三',
                icon: 1
              }
            ]
          },
        ]
      },
	])	
		//展开的key
		const expandedKeys = ref([])
		//搜索关键字
	    const searchValue = ref('');
	    //是否展开树
	    const autoExpandParent = ref(true);
		
		// 展开收起触发
		const onExpand = keys => {
	      expandedKeys.value = keys;
	      autoExpandParent.value = false;
   		}
   		
   		//处理数据,拿到 提取 key title  格式为 :{key:key,title: node.title } Antd组件方法
   		
		const dataList = [];
	    const generateList = data => {
	      for (let i = 0; i < data.length; i++) {
	        const node = data[i];
	        const key = node.key;
	        console.log('node', node);
	        dataList.push({
	          key,
	          title: node.title,
	        });
	        if (node.children) {
	          generateList(node.children);
	        }
	      }
	    };
	
	   	 generateList(list.value);
		
		//andt 组件方法 获取父级节点key
   		 const getParentKey = (key, tree) => {
	      let parentKey;
	      for (let i = 0; i < tree.length; i++) {
	        const node = tree[i];
	
	        if (node.children) {
	          if (node.children.some(item => item.key === key)) {
	            parentKey = node.key;
	          } else if (getParentKey(key, node.children)) {
	            parentKey = getParentKey(key, node.children);
	          }
	        }
	      }
	
	      return parentKey;
	    };

		//搜索事件
		 watch(searchValue, value => {
		      if (!value) return autoExpandParent.value = false;
		      const expanded = dataList.map(item => {
		        if (item.title.indexOf(value) > -1) {
		          return getParentKey(item.key, list.value);
		        }
		
		        return null;
		      }).filter((item, i, self) => item && self.indexOf(item) === i);
		      expandedKeys.value = expanded;
		      searchValue.value = value;
		      autoExpandParent.value = true;
		    });
		}
</script>
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3中,可以通过`ref`和`teleport`来实现父组件调用子组件的modal。 首先,在子组件中,需要使用`teleport`将modal渲染到body中,这样可以避免modal被父组件的样式影响。代码如下: ```html <template> <teleport to="body"> <div class="modal"> <!-- modal内容 --> </div> </teleport> </template> ``` 然后,在父组件中,需要使用`ref`来获取子组件的实例,并通过调用子组件的方法来打开或关闭modal。代码如下: ```html <template> <div> <button @click="openModal">打开modal</button> <ChildComponent ref="childComponentRef" /> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent, }, methods: { openModal() { this.$refs.childComponentRef.openModal(); }, }, }; </script> ``` 在子组件中,需要定义一个`openModal`方法来打开modal,并通过`emits`来向父组件发送事件。代码如下: ```html <template> <teleport to="body"> <div class="modal" v-if="visible"> <!-- modal内容 --> </div> </teleport> </template> <script> import { defineEmits } from 'vue'; export default { emits: ['update:visible'], data() { return { visible: false, }; }, methods: { openModal() { this.visible = true; this.$emit('update:visible', true); }, closeModal() { this.visible = false; this.$emit('update:visible', false); }, }, }; </script> ``` 在父组件中,需要通过`v-model`来监听子组件的`visible`属性,并在子组件的`update:visible`事件触发时更新`visible`属性。代码如下: ```html <template> <div> <button @click="visible = true">打开modal</button> <ChildComponent v-model:visible="visible" /> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent, }, data() { return { visible: false, }; }, }; </script> ``` 这样,就可以实现父组件调用子组件的modal了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一头小绵羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值