✨(component): cascader级联选择器 新增onlyLastLevel属性 控制displayValue回显数据层级
This commit is contained in:
parent
f215f47210
commit
0a4dc242f6
@ -1,48 +1,23 @@
|
|||||||
<template>
|
<template>
|
||||||
<lay-dropdown
|
<lay-dropdown class="layui-cascader" ref="dropdownRef" :autoFitMinWidth="false" :updateAtScroll="true">
|
||||||
class="layui-cascader"
|
<lay-input v-model="displayValue" readonly suffix-icon="layui-icon-down" :placeholder="placeholder"
|
||||||
ref="dropdownRef"
|
v-if="!slots.default"></lay-input>
|
||||||
:autoFitMinWidth="false"
|
|
||||||
:updateAtScroll="true"
|
|
||||||
>
|
|
||||||
<lay-input
|
|
||||||
v-model="displayValue"
|
|
||||||
readonly
|
|
||||||
suffix-icon="layui-icon-down"
|
|
||||||
:placeholder="placeholder"
|
|
||||||
v-if="!slots.default"
|
|
||||||
></lay-input>
|
|
||||||
<slot v-else></slot>
|
<slot v-else></slot>
|
||||||
|
|
||||||
<template #content>
|
<template #content>
|
||||||
<div class="layui-cascader-panel">
|
<div class="layui-cascader-panel">
|
||||||
<template v-for="(itemCol, index) in treeData">
|
<template v-for="(itemCol, index) in treeData">
|
||||||
<lay-scroll
|
<lay-scroll height="180px" class="layui-cascader-menu" :key="'cascader-menu' + index"
|
||||||
height="180px"
|
v-if="itemCol.data.length">
|
||||||
class="layui-cascader-menu"
|
<div class="layui-cascader-menu-item" v-for="(item, i) in itemCol.data" :key="index + i"
|
||||||
:key="'cascader-menu' + index"
|
@click="selectBar(item, i, index)" :class="[
|
||||||
v-if="itemCol.data.length"
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
class="layui-cascader-menu-item"
|
|
||||||
v-for="(item, i) in itemCol.data"
|
|
||||||
:key="index + i"
|
|
||||||
@click="selectBar(item, i, index)"
|
|
||||||
:class="[
|
|
||||||
{
|
{
|
||||||
'layui-cascader-selected': itemCol.selectIndex === i,
|
'layui-cascader-selected': itemCol.selectIndex === i,
|
||||||
},
|
},
|
||||||
]"
|
]">
|
||||||
>
|
<slot :name="item.slot" v-if="item.slot && slots[item.slot]"></slot>
|
||||||
<slot
|
|
||||||
:name="item.slot"
|
|
||||||
v-if="item.slot && slots[item.slot]"
|
|
||||||
></slot>
|
|
||||||
<template v-else>{{ item.label }}</template>
|
<template v-else>{{ item.label }}</template>
|
||||||
<i
|
<i class="layui-icon layui-icon-right" v-if="item.children && item.children.length"></i>
|
||||||
class="layui-icon layui-icon-right"
|
|
||||||
v-if="item.children && item.children.length"
|
|
||||||
></i>
|
|
||||||
</div>
|
</div>
|
||||||
</lay-scroll>
|
</lay-scroll>
|
||||||
</template>
|
</template>
|
||||||
@ -68,12 +43,14 @@ export interface LayCascaderProps {
|
|||||||
modelValue?: string;
|
modelValue?: string;
|
||||||
decollator?: string;
|
decollator?: string;
|
||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
|
onlyLastLevel?: boolean
|
||||||
}
|
}
|
||||||
const props = withDefaults(defineProps<LayCascaderProps>(), {
|
const props = withDefaults(defineProps<LayCascaderProps>(), {
|
||||||
options: null,
|
options: null,
|
||||||
modelValue: "",
|
modelValue: "",
|
||||||
decollator: "/",
|
decollator: "/",
|
||||||
placeholder: "",
|
placeholder: "",
|
||||||
|
onlyLastLevel: false
|
||||||
});
|
});
|
||||||
const emit = defineEmits(["update:modelValue", "change", "clear"]);
|
const emit = defineEmits(["update:modelValue", "change", "clear"]);
|
||||||
|
|
||||||
@ -195,11 +172,16 @@ const selectBar = (item: any, selectIndex: number, parentIndex: number) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
extractData(treeData.value, data, 0);
|
extractData(treeData.value, data, 0);
|
||||||
|
if (!props.onlyLastLevel) {
|
||||||
displayValue.value = data
|
displayValue.value = data
|
||||||
.map((e: any) => {
|
.map((e: any) => {
|
||||||
return e.label;
|
return e.label;
|
||||||
})
|
})
|
||||||
.join(` ${props.decollator} `);
|
.join(` ${props.decollator} `);
|
||||||
|
} else {
|
||||||
|
let _data = data.map((e: any) => { return e.label; })
|
||||||
|
displayValue.value = _data[_data.length - 1];
|
||||||
|
}
|
||||||
let value = data
|
let value = data
|
||||||
.map((e: any) => {
|
.map((e: any) => {
|
||||||
return e.value;
|
return e.value;
|
||||||
|
@ -300,10 +300,19 @@ 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>
|
||||||
|
:::
|
||||||
|
|
||||||
|
::: title 控制回显层级
|
||||||
|
:::
|
||||||
|
::: demo 使用 `onlyLastLevel` 属性 可以仅在回显的displayValue显示选中项最后一级的标签,而不是完整路径, 注意绑定的v-model仍然是完整的。
|
||||||
|
<template>
|
||||||
|
<lay-cascader :options="options" v-model="valueLv" :onlyLastLevel="true" placeholder="仅显示最后一级" style="width:250px"></lay-cascader>
|
||||||
|
<span style="margin-left:20px">输出的值:{{valueLv}}</span>
|
||||||
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref } from "vue";
|
import { ref } from "vue";
|
||||||
const value1=ref(null)
|
const valueLv=ref(null)
|
||||||
</script>
|
</script>
|
||||||
:::
|
:::
|
||||||
|
|
||||||
@ -626,7 +635,7 @@ const options2 = [
|
|||||||
| v-model / modelValue | 值 |
|
| v-model / modelValue | 值 |
|
||||||
| decollator | 分割符号,默认为 / |
|
| decollator | 分割符号,默认为 / |
|
||||||
| options | 选项参数 格式请见上面的demo |
|
| options | 选项参数 格式请见上面的demo |
|
||||||
|
| onlyLastLevel | 回显displayValue仅显示最后一级,默认为 `false` |
|
||||||
:::
|
:::
|
||||||
|
|
||||||
::: title Cascader 事件
|
::: title Cascader 事件
|
||||||
@ -634,7 +643,7 @@ const options2 = [
|
|||||||
|
|
||||||
::: table
|
::: table
|
||||||
|
|
||||||
| 属性 | 描述 |
|
| 方法名 | 描述 |
|
||||||
| ---- | ------------ |
|
| ---- | ------------ |
|
||||||
| change | 选中后数据改变的回调 |
|
| change | 选中后数据改变的回调 |
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user