【问题标题】:How to highlight tree node on click of button in element-ui如何在 element-ui 中单击按钮时突出显示树节点
【发布时间】:2018-02-02 09:01:01
【问题描述】:

我有 el-tree> 元素和 1 个按钮 showNode。 所以单击此按钮时,我想突出显示具有给定索引的节点。 如何在点击按钮时调用highlight-current

【问题讨论】:

    标签: element-ui


    【解决方案1】:

    来自documentation

    highlight-current:当前节点是否高亮

    highlight-current只是一个只读属性,为了突出显示特定节点,你必须调用方法setCurrentKey

    setCurrentKey:按key设置节点高亮,仅在指定node-key时有效,param(key)为节点要高亮的key

    因此,您首先需要为每个节点添加一个键,使用属性键:

    node-key:节点的唯一标识键名,其值在整个树中应该是唯一的

    如下例:

    <el-tree
      :data="myData"
      node-key="id">
    </el-tree>
    

    然后,在附加到按钮的click 事件上调用方法setCurrentKey

    <el-button @click="setCurrentKey(myData[index].id)">Highlight the n-index node</el-button>
    

    希望对你有帮助,再见。

    更新:以上代码仅适用于 2.x 版本,这里是突出显示 1.4.13 版本节点的示例(但我无法展开节点,所以我必须展开所有的树):

    var Main = {
        data() {
          return {
            data: [{
              label: 'Level one 1',
              id: 1,
              children: [{
                label: 'Level two 1-1',
                id: 11
              }]
            }, {
              label: 'Level one 2',
              id: 2,
              children: [{
                label: 'Level two 2-1',
                id: 21,
                children: [{
                  label: 'Level three 2-1-1',
                  id: 211
                }]
              }, {
                label: 'Level two 2-2',
                id: 22,
                children: [{
                  label: 'Level three 2-2-1',
                  id: 221
                }]
              }]
            }],
            defaultProps: {
              children: 'children',
              label: 'label'
            }
          };
        },
        methods: {
          handleNodeClick(data) {
            console.log(data);
          },
          renderContent(h, { node, data, store }) {
            let btn = h('span', {
              props: {type: 'success'},
              domProps: {
                innerHTML: data.label,
                id: "node-id-" + data.id
              }
            })
            return btn
          },
          handleBtnClick(){
            //Highlite the 211
          	document.getElementById("node-id-211").style.backgroundColor="yellow";
          }
        }
      };
    var Ctor = Vue.extend(Main)
    new Ctor().$mount('#app')
    @import url("//unpkg.com/element-ui@1.4.13/lib/theme-default/index.css");
    <script src="//unpkg.com/vue/dist/vue.js"></script>
    <script src="//unpkg.com/element-ui@1.4.13/lib/index.js"></script>
    <div id="app">
    <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick" :render-content="renderContent" :default-expand-all="true"></el-tree>
    <el-button @click="handleBtnClick">Highlight the 211 node</el-button>
    </div>

    【讨论】:

    • 感谢您的帮助和建议。但以上所有组件都存在于 2.o 及更高版本中。但我使用的是以前的版本,你能建议我以前版本的解决方案吗?
    • 抱歉,恐怕在 1.4.13 版本中您无法通过按键突出显示节点。您可以尝试使用 css/javascript 类似:document.getElementsByClassName("el-tree-node")[index].classList.add("selected") 并为 selected 类定义 background-color
    • @POM 我已经用一个技巧更新了我的答案,以在以前的版本中突出显示节点。再见。
    猜你喜欢
    • 1970-01-01
    • 2013-05-24
    • 2021-11-28
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多