vue3 Select 下拉选择器 数据格式改成自定义key

Select选择器​​​​​​官网地址

当前数据格式改成格式

[

{
id: 'id',
pid: 'pid', //可能多个0
name: 'name',
code: 'code', /
}

...

]

[ {
code: 'code',
label: 'label',
children: 'children',
}

.....

]

<template>
    <vxe-column field="relationShipName" title="关系" align="center" min-width="120">
        <template #default="{ row }">
            <el-tree-select
            v-model="row.relationShip"
            :props="props"
            filterable
            value-key="code"
            node-key="code"
            :data="relationShipTreeData"
            :render-after-expand="false"
            @change="checkGx(row)"
                  />
            </template>
        </vxe-column>
</template>
<script setup>
    const props = {
      //value: 'id',
      code: 'code',
      label: 'label',
      children: 'children',
    }
    //点击事件
    const clearHz = (row) => {
      
    }

    //获取字典数据--转化成select标准格式
    const getDictionary = () => {
      proxy.$api.a.b.dictionary('relationShip')
          .then(res =>{
            state.relationShipTreeData = transformToTree(res.data.data);
          })
          .catch(err => {})
    }

    // 转换函数
    const transformToTree = (arr) =>{
      let map = {}, node, roots = [], i;
      for (i = 0; i < arr.length; i += 1) {
        map[arr[i].id] = i;
        arr[i].code = arr[i].code;
        arr[i].label = arr[i].name;
        arr[i].children = [];
      }
      for (i = 0; i < arr.length; i += 1) {
        node = arr[i];
        if (node.pid !== 0) {
          arr[map[node.pid]].children.push(node);
        } else {
          roots.push(node);
        }
      }
      return roots;
    }
</script>

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Vue 3 Select 下拉选择器组件的封装示例: ```vue <template> <div class="select-container" @click="toggleDropdown"> <div class="selected-option">{{ selectedOption.label }}</div> <div class="dropdown" v-show="showDropdown"> <div class="option" v-for="(option, index) in options" :key="index" @click="selectOption(option)"> {{ option.label }} </div> </div> </div> </template> <script> import { ref } from 'vue'; export default { props: { options: { type: Array, required: true, }, }, setup(props) { const selectedOption = ref(props.options[0]); // 初始选中的选项 const showDropdown = ref(false); // 下拉列表是否显示 const toggleDropdown = () => { showDropdown.value = !showDropdown.value; }; const selectOption = (option) => { selectedOption.value = option; showDropdown.value = false; }; return { selectedOption, showDropdown, toggleDropdown, selectOption, }; }, }; </script> <style> .select-container { position: relative; width: 200px; height: 30px; border: 1px solid #ccc; border-radius: 4px; cursor: pointer; } .selected-option { line-height: 30px; padding-left: 10px; } .dropdown { position: absolute; top: 100%; left: 0; z-index: 10; width: 100%; max-height: 200px; overflow-y: auto; border: 1px solid #ccc; border-top: none; border-radius: 0 0 4px 4px; background-color: #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); display: none; } .dropdown .option { line-height: 30px; padding: 0 10px; cursor: pointer; } .dropdown .option:hover { background-color: #f5f5f5; } </style> ``` 使用方法: ```vue <template> <div> <select-component :options="options"></select-component> </div> </template> <script> import SelectComponent from './SelectComponent.vue'; export default { components: { SelectComponent, }, data() { return { options: [ { value: 'option1', label: '选项1' }, { value: 'option2', label: '选项2' }, { value: 'option3', label: '选项3' }, ], }; }, }; </script> ``` 这个简单的 Select 下拉选择器组件的实现并不完整,你可以根据实际需求进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值