vue-ant-design 动态增减表单,级联选择器怎么做数据回显?
vue-ant-design 动态增减表单,懒加载的级联选择器怎么做数据回显?
<template> <a-cascader
:options="options"
:load-data="loadData"
placeholder="Please select"
change-on-select
@change="onChange"
/>
</template>
<script>
export default {
data() {
return {
options: [
{
value: 'zhejiang',
label: 'Zhejiang',
isLeaf: false,
},
{
value: 'jiangsu',
label: 'Jiangsu',
isLeaf: false,
},
],
};
},
methods: {
onChange(value) {
console.log(value);
},
loadData(selectedOptions) {
const targetOption = selectedOptions[selectedOptions.length - 1];
targetOption.loading = true;
// load options lazily
setTimeout(() => {
targetOption.loading = false;
targetOption.children = [
{
label: `${targetOption.label} Dynamic 1`,
value: 'dynamic1',
},
{
label: `${targetOption.label} Dynamic 2`,
value: 'dynamic2',
},
];
this.options = [...this.options];
}, 1000);
},
},
};
</script>
回答:
<template> <a-cascader
v-model="selectedOptions"
:options="options"
:load-data="loadData"
placeholder="Please select"
change-on-select
@change="onChange"
/>
</template>
<script>
export default {
data() {
return {
selectedOptions: [], // 存选中的选项
options: [
{
value: 'zhejiang',
label: 'Zhejiang',
isLeaf: false,
},
{
value: 'jiangsu',
label: 'Jiangsu',
isLeaf: false,
},
],
};
},
methods: {
onChange(value) {
console.log(value);
},
loadData(selectedOptions) {
const targetOption = selectedOptions[selectedOptions.length - 1];
targetOption.loading = true;
// load options lazily
setTimeout(() => {
targetOption.loading = false;
targetOption.children = [
{
label: `${targetOption.label} Dynamic 1`,
value: 'dynamic1',
},
{
label: `${targetOption.label} Dynamic 2`,
value: 'dynamic2',
},
];
this.options = [...this.options];
}, 1000);
},
},
mounted() {
// 你想要回显的数据是 ['zhejiang', 'dynamic1']
this.selectedOptions = ['zhejiang', 'dynamic1'];
}
};
</script>
以上是 vue-ant-design 动态增减表单,级联选择器怎么做数据回显? 的全部内容, 来源链接: utcz.com/p/934719.html