123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <template>
- <m-tree
- ref="treeRef"
- :data="list"
- node-key="id"
- show-checkbox
- default-expand-all
- style="margin-bottom:20px">
- </m-tree>
- <m-space>
- <m-button type="primary" @click="setCheckedKeys">通过node-key设置选中</m-button>
- <m-button type="primary" @click="getCheckedNodes">获取选中项的数据数组</m-button>
- <m-button type="primary" @click="getCheckedKeys">获取选中项的keys数组</m-button>
- <m-button type="primary" @click="resetChecked">清空选中项</m-button>
- </m-space>
- </template>
- <script setup>
- import { ref } from 'vue-demi';
- const treeRef = ref(null);
- const list = [
- {
- id: 1,
- label: '一级 1',
- children: [
- {
- id: 4,
- label: '二级 1-1',
- children: [
- {
- id: 10,
- label: '三级 1-1-1'
- },
- {
- id: 11,
- label: '三级 1-1-2'
- }
- ]
- },
- {
- id: 5,
- label: '二级 1-2'
- }
- ]
- },
- {
- id: 2,
- label: '一级 2',
- children: [
- {
- id: 6,
- label: '二级 2-1',
- children: [
- {
- id: 12,
- label: '三级 2-1-1'
- }
- ]
- },
- {
- id: 7,
- label: '二级 2-2'
- }
- ]
- },
- {
- id: 3,
- label: '一级 3',
- children: [
- {
- id: 8,
- label: '二级 3-1'
- },
- {
- id: 9,
- label: '二级 3-2'
- }
- ]
- }
- ]
- const setCheckedKeys = () => {
- treeRef.value.setCheckedKeys([4])
- }
- const getCheckedNodes = () => {
- const nodes = treeRef.value.getCheckedNodes()
- alert(JSON.stringify(nodes))
- console.log(nodes);
- }
- const getCheckedKeys = () => {
- const keys = treeRef.value.getCheckedKeys()
- alert(JSON.stringify(keys))
- console.log(keys);
- }
- const resetChecked = () => {
- treeRef.value.setCheckedKeys([])
- }
- </script>
|