antd级联框Cascader动态加载的用法

本文介绍了Ant Design的Cascader组件如何实现动态加载。通过官方文档和具体代码示例,展示了根据产品类型获取产品的动态加载过程。讨论了isLeaf属性的使用,以及如何利用selectedOptions.length来控制多级下拉框的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,官方文档:https://ant.design/components/cascader-cn/#header

二,具体代码(以根据产品类型获取产品为例):

import React, { Component } from 'react';
import { Cascader,Form,} from 'antd';


//级联获取产品类型
const getProductType= () => axios.post(config.HOST + '/work/getProductType', qs.stringify({
    userToken: JSON.parse(localStorage.getItem('user')).token,
})).then(function (response) {
    return response.data;
}).catch(function (error) {
    console.log(error);
});
//级联根据产品类型获取产品
const getProductOfType= (productTypeName) => axios.post(config.HOST + '/work/getProductOfType', qs.stringify({
    userToken: JSON.parse(localStorage.getItem('user')).token,
    productTypeName:productTypeName,
})).then(function (response) {
    return response.data;
}).catch(function (error) {
    console.log(error);
});

//创建form组件
const CollectionCreateForm = Form.create()(
    (props) => {
        const {productTypeOptions,getProductsLoadData,productDisplayRender} = props;
    
    return(
        <div>
            <form>
                <FormItem  >
                          {getFieldDecorator('minProduct', )
                                (
                                    <Cascader options={productTypeOptions}
                                              loadData={getProductsLoadData}
                                              displayRender={productDisplayRender}
                                              expandTrigger="hover"
                                     />
                                 )}
                </FormItem>
            </form>
        </div>
    )
)

//创建组件
class formDemo extends Component {
    state={
            productTypeOptions:[],//产品类型option
          }
}

//组件生命周期
componentDidMount() {
        this.getProducttypes(); //在渲染前调用
    }

//级联获取产品类型
getProducttypes=()=>{
    getProductType().then(res=>{
        const productTypeList = res.productTypeList;
            const productTypeOption = productTypeList&&productTypeList.map(productType=>productType.id? {
                     value:productType.id,
                     label:productType.name,
                     isLeaf:false,
                  }:"");
            this.setState({
                productTypeOptions:productTypeOption
            });
        });
    };

//级联根据产品类型获取产品
getProductsLoadData=(selectedOptions)=>{
     const targetOption = selectedOptions[selectedOptions.length - 1];
          getProductOfType(targetOption.label).then(res => {
           const productList = res.productList;
           targetOption.children = productList && productList.map(product => product.id ?
                    {
                        value: product.id,
                        label: product.name,
                        isLeaf: true,
                    } : "");
                this.setState({
                    productTypeOptions: [...this.state.productTypeOptions],
                });
            });
    }

//级联选择后的界面渲染
productDisplayRender = (label) => {
        return label[label.length - 1];
};

//渲染
 render() {
    <span>
        <CollectionCreateForm
           productTypeOptions={this.state.productTypeOptions}//产品级联一级下拉框
           getProductsLoadData={this.getProductsLoadData}//产品级联二级下拉框
           productDisplayRender={this.productDisplayRender}//产品级联选择框学则后的渲染函数
        />
    </span>
}

 三,要点说明:

(1),isLeaf为true时,表示当层下拉框是最后一级下拉框,默认为true;

(2),若有n级下拉框,可根据selectedOptions.length来做if判断进行控制,它意味当前级联框的级数。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值