实战:element-ui 树型控件自定义图标(给节点添加图标)

效果图:
在这里插入图片描述html:

<el-input
   v-model="searchVal"
   placeholder="请输入关键词"
   clearable
   style="margin-bottom: 12px"
 />
 <div class="treeBox">
   <el-tree
     :data="sourceData"
     node-key="id"
     default-expand-all
     :expand-on-click-node="false"
     :props="defaultProps"
     @node-click="handleNodeClick"
   >
   	 // 重点:给节点添加图标
     <span slot-scope="{ node, data }" class="slot-t-node">
       <template>
         <i
           :class="{
             'el-icon-folder': !node.expanded,       // 节点收缩时的图标
             'el-icon-folder-opened': node.expanded, // 节点展开时的图标
             'el-icon-user-solid': data.type === 2   // data.type是后端配合提供的识别字段,最后一级
           }"
           style="color: #409eff;" // 图标颜色
         />
         <span>{{ node.label }}</span>
       </template>
     </span>
   </el-tree>
 </div>

js:

data() {
    return {
      searchVal: '', // 输入的关键词
      defaultProps: {
        children: 'childrenList', // 将tree中的每项的 childrenList 映射为 children
        label: 'name' // 将tree中的每项的 name 映射为 label
      },
      sourceData: [ // 树源数据
        {
          id: null,
          name: '深圳市XXX有限公司',
          parentId: '0',
          childrenList: [
            {
              id: null,
              name: '研发中心',
              parentId: '1',
              childrenList: [
                {
                  id: null,
                  name: '研发1组',
                  parentId: '2',
                  childrenList: [
                    {
                      id: null,
                      name: 'IOS测试机',
                      parentId: null,
                      childrenList: null,
                      type: 2
                    }
                  ],
                  type: 1
                },
                {
                  id: null,
                  name: '安卓测试机',
                  parentId: null,
                  childrenList: null,
                  type: 2
                }
              ],
              type: 1
            },
            {
              id: null,
              name: '产品中心',
              parentId: '1',
              childrenList: [],
              type: 1
            },
            {
              id: null,
              name: '员工1',
              parentId: null,
              childrenList: null,
              type: 2
            },
            {
              id: null,
              name: '员工12',
              parentId: null,
              childrenList: null,
              type: 2
            },
            {
              id: null,
              name: '员工13',
              parentId: null,
              childrenList: null,
              type: 2
            }
          ],
          type: 1
        }
      ]
    }
  },
  methods: {
  	// 节点点击时
    handleNodeClick(data) {
      // 。。。
    }
  }
  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
如果你想在 Element树型控件中使用自定义过滤,可以使用 `filter-node-method` 属性来指定一个函数。该函数会在每个节点被渲染之前调用,你可以在这里根据自己的需求对节点进行过滤。函数的参数包括节点数据以及节点的父节点数据,你可以根据这些信息来确定当前节点是否需要被渲染。 下面是一个示例代码: ```html <el-tree :data="treeData" :filter-node-method="filterNode" default-expand-all ></el-tree> ``` ```javascript export default { data() { return { treeData: [ { label: '一级 1', children: [ { label: '二级 1-1', children: [ { label: '三级 1-1-1' }, { label: '三级 1-1-2' } ] }, { label: '二级 1-2', children: [ { label: '三级 1-2-1' }, { label: '三级 1-2-2' } ] } ] }, { label: '一级 2', children: [ { label: '二级 2-1', children: [ { label: '三级 2-1-1' }, { label: '三级 2-1-2' } ] }, { label: '二级 2-2', children: [ { label: '三级 2-2-1' }, { label: '三级 2-2-2' } ] } ] } ] } }, methods: { filterNode(value, data) { if (!value) { return true; } return data.label.indexOf(value) !== -1; } } } ``` 在上面的示例中,我们使用 `filterNode` 方法来对节点进行过滤。如果搜索框中没有输入任何内容,我们直接返回 `true`,表示所有节点都需要被渲染。如果搜索框中有输入内容,我们根据节点的 `label` 属性来判断是否需要渲染该节点。如果节点的 `label` 属性包含搜索框中输入的内容,我们返回 `true`,否则返回 `false`,表示该节点不需要被渲染。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值