✨(component): cascader组件 新增replaceFields属性 用于自义定key
This commit is contained in:
parent
237f4fd508
commit
fe99ab0c1f
@ -44,6 +44,7 @@ export interface LayCascaderProps {
|
|||||||
decollator?: string;
|
decollator?: string;
|
||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
onlyLastLevel?: boolean;
|
onlyLastLevel?: boolean;
|
||||||
|
replaceFields?: { label: string, value: string, children: string }
|
||||||
}
|
}
|
||||||
const props = withDefaults(defineProps<LayCascaderProps>(), {
|
const props = withDefaults(defineProps<LayCascaderProps>(), {
|
||||||
options: null,
|
options: null,
|
||||||
@ -51,6 +52,13 @@ const props = withDefaults(defineProps<LayCascaderProps>(), {
|
|||||||
decollator: "/",
|
decollator: "/",
|
||||||
placeholder: "",
|
placeholder: "",
|
||||||
onlyLastLevel: false,
|
onlyLastLevel: false,
|
||||||
|
replaceFields: () => {
|
||||||
|
return {
|
||||||
|
label: 'label',
|
||||||
|
value: 'value',
|
||||||
|
children: 'children'
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
const emit = defineEmits(["update:modelValue", "change", "clear"]);
|
const emit = defineEmits(["update:modelValue", "change", "clear"]);
|
||||||
|
|
||||||
@ -110,7 +118,7 @@ const initTreeData = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function getMaxFloor(treeData: any) {
|
function getMaxFloor(treeData: any) {
|
||||||
let floor = 0;
|
//let floor = 0;
|
||||||
let max = 0;
|
let max = 0;
|
||||||
function each(data: any, floor: any) {
|
function each(data: any, floor: any) {
|
||||||
data.forEach((e: any) => {
|
data.forEach((e: any) => {
|
||||||
@ -118,8 +126,8 @@ function getMaxFloor(treeData: any) {
|
|||||||
if (floor > max) {
|
if (floor > max) {
|
||||||
max = floor;
|
max = floor;
|
||||||
}
|
}
|
||||||
if (e.children && e.children.length > 0) {
|
if (e[props.replaceFields.children] && e[props.replaceFields.children].length > 0) {
|
||||||
each(e.children, floor + 1);
|
each(e[props.replaceFields.children], floor + 1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -132,16 +140,16 @@ function findData(orginData: any, level: number) {
|
|||||||
const element = orginData[i];
|
const element = orginData[i];
|
||||||
if (level === 1) {
|
if (level === 1) {
|
||||||
data.push({
|
data.push({
|
||||||
value: element.value,
|
value: element[props.replaceFields.value],
|
||||||
label: element.label,
|
label: element[props.replaceFields.label],
|
||||||
slot: element.slot || false,
|
slot: element.slot || false,
|
||||||
children: element.children ?? false,
|
children: element[props.replaceFields.children] ?? false,
|
||||||
orginData:element
|
orginData: element
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (level !== 1 && element.children && element.children.length > 0) {
|
if (level !== 1 && element[props.replaceFields.children] && element[props.replaceFields.children].length > 0) {
|
||||||
findData(element.children, level - 1);
|
findData(element[props.replaceFields.children], level - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
|
@ -300,6 +300,7 @@ const options = [
|
|||||||
<lay-cascader :options="options" v-model="value1" decollator="-" placeholder="我可以自定义分割符号" style="width:250px"></lay-cascader>
|
<lay-cascader :options="options" v-model="value1" decollator="-" placeholder="我可以自定义分割符号" style="width:250px"></lay-cascader>
|
||||||
<span style="margin-left:20px">输出的值:{{value1}}</span>
|
<span style="margin-left:20px">输出的值:{{value1}}</span>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref } from "vue";
|
import { ref } from "vue";
|
||||||
const value1=ref(null)
|
const value1=ref(null)
|
||||||
@ -628,6 +629,62 @@ const options2 = [
|
|||||||
|
|
||||||
:::
|
:::
|
||||||
|
|
||||||
|
::: title Cascader 自定义字段名
|
||||||
|
:::
|
||||||
|
::: demo 也许你当前数据键名并不是`label`、`value`、`children`,这时只需要使用replaceFields属性来自定义key
|
||||||
|
<template>
|
||||||
|
<lay-cascader :options="options3" :replaceFields="replaceFields" placeholder="自义定key"></lay-cascader>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { ref } from "vue";
|
||||||
|
const replaceFields={
|
||||||
|
label:'name',
|
||||||
|
value:'id',
|
||||||
|
children:'group'
|
||||||
|
}
|
||||||
|
const options3=[
|
||||||
|
{
|
||||||
|
name:"张三",
|
||||||
|
id:1,
|
||||||
|
group:[
|
||||||
|
{
|
||||||
|
name:"张三-1",
|
||||||
|
id:2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name:"张三-2",
|
||||||
|
id:3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name:"张三-3",
|
||||||
|
id:4
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name:"李四",
|
||||||
|
id:5,
|
||||||
|
group:[
|
||||||
|
{
|
||||||
|
name:"李四-1",
|
||||||
|
id:6
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name:"李四-2",
|
||||||
|
id:7
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name:"李四-3",
|
||||||
|
id:8
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
</script>
|
||||||
|
|
||||||
|
:::
|
||||||
|
|
||||||
::: title Cascader 属性
|
::: title Cascader 属性
|
||||||
:::
|
:::
|
||||||
|
|
||||||
@ -639,7 +696,8 @@ const options2 = [
|
|||||||
| v-model / modelValue | 值 |
|
| v-model / modelValue | 值 |
|
||||||
| decollator | 分割符号,默认为 / |
|
| decollator | 分割符号,默认为 / |
|
||||||
| options | 选项参数 格式请见上面的demo |
|
| options | 选项参数 格式请见上面的demo |
|
||||||
| onlyLastLevel | 回显displayValue仅显示最后一级,默认为 `false` |
|
| onlyLastLevel | 回显display仅显示最后一级,默认为 `false` |
|
||||||
|
| replaceFields | 自定义数据key名,可配置项为`label`,`value`,`children`,用法详见上面案例 |
|
||||||
:::
|
:::
|
||||||
|
|
||||||
::: title Cascader 事件
|
::: title Cascader 事件
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
<ul>
|
<ul>
|
||||||
<li>[修复] layer 组件 Notify 关闭图标样式问题。 by @SmallWai</li>
|
<li>[修复] layer 组件 Notify 关闭图标样式问题。 by @SmallWai</li>
|
||||||
<li>[优化] cascader 组件 优化change回调参数 by @SmallWai</li>
|
<li>[优化] cascader 组件 优化change回调参数 by @SmallWai</li>
|
||||||
|
<li>[新增] cascader 组件 新增replaceFields属性 用于自义定key by @SmallWai</li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
Loading…
Reference in New Issue
Block a user