elment-ui tree單選實現

elment-ui 裏面的樹形控件,默認是多選的,如果你仔細看過各個屬性和方法就可以實現單選。

  1. check-strictly 關閉父子關聯
  2. node-key 設置唯一標識
  3. show-checkbox 允許節點可選
  4. ref=“tree”
  5. 添加方法 @check=“treeChange”
<el-tree
              ref="tree"
              :data="menuTreeData"
              node-key="id"
              :check-strictly="true"
              @check="treeChange"
              :props="{
                label: 'title', // 根據你數據的字段設置
                children: 'children'
              }"
              show-checkbox
              accordion
            >
            </el-tree>

check方法:

    treeChange(now, tree) {
        this.$nextTick(() => {
          this.$refs.tree.setCheckedKeys([now.id], true); //[now.id]表示當前只選擇只一個id標識
        });
    }

獲取選中的節點key值:

let kes= this.$refs.tree.getCheckedKeys();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章