Quasar中<q-tree>使用的部分说明与使用示例

官网地址:http://v1.quasarchs.com/vue-components/tree/#%E7%BB%BC%E5%90%88%E5%AE%9E%E4%BE%8B

<template #option>
  <q-tree
    ref="treeRef"
    :nodes="simple"
    node-key="label"
    selected-color="primary"
    :selected.sync="type"
    no-selection-unset
    @update:selected="closeCheckBox"
  />
</template>

代码说明:

  • ref是用来后续获取当前选中的数值(后面有示例,简单明了)
  • :nodes表示tree数据
  • node-key表示tree数据中的唯一主键,类似与v-for中的:key
  • selected-color表示选中后的颜色展示
  • :selected.sync表示选择以后展示的内容
  • no-selection-unset表示选中以后再次点击不会被取消
  • default-expand-all表示默认展开所有节点
<template>
  <div style="height: 500px;width: 500px;">
    <div style="height: 200px;width: 200px">
      <span>
        此时选中的值为:{{ this.type }}
      </span>
      <br>
      <span>
        此时选中的值的id为:{{ this.selectNodeValue }}
      </span>
    </div>
    <q-select
      style="width: 200px;height: 50px;"
      outlined
      v-model="type"
      :options="simple"
      emit-value
      map-options
    >
      <template #option>
        <q-tree
          ref="treeRef"
          :nodes="simple"
          node-key="label"
          selected-color="primary"
          :selected.sync="type"
          no-selection-unset
          @update:selected="closeCheckBox"
        />
      </template>
    </q-select>
  </div>
</template>

<script>
export default {
  data() {
    return {
      type: "",
      selectNodeValue: '',
      simple: [
        {
          "label": "我是一类",
          "data": {
            "id": "1",
            "name": "我是一类"
          },
          "children": [
            {
              "label": "我是二类",
              "data": {
                "id": "12",
                "name": "我是二类"
              },
              "children": [
                {
                  "label": "我是三类之一",
                  "data": {
                    "id": "1231",
                    "name": "我是三类之一"
                  },
                  "children": null
                },
                {
                  "label": "我是三类之二",
                  "data": {
                    "id": "1232",
                    "name": "我是三类之二"
                  },
                  "children": null
                },
                {
                  "label": "我是三类之三",
                  "data": {
                    "id": "1233",
                    "name": "我是三类之三"
                  },
                  "children": null
                }
              ]
            }
          ]
        }
      ],
    }
  },
  methods: {
    closeCheckBox(newVal) {
      this.type = newVal //这个值是选框中所展示的数值
      
      const nodeData = this.$refs.treeRef.getNodeByKey(newVal)
      this.selectNodeValue = nodeData.data.id // 用来获取选框中选择的数据的id
    },
  }

}
</script>

图片示例:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值