【后台管理系统】—— Ant Design Pro组件使用(一)

一、搜索Search

    

  • 搜索框
    <Search placeholder="请输入关键字" 
            defaultValue={kw && kw != 'null' ? kw : ''}
            className={styles.search} 
            onChange={() => this.handleKwChange()} 
            onSearch={(e) => this.handleSearch(e)}
    />
  • 引入工具方法:去掉收尾空格

    import {trimStr} from '@/utils/utils';
    
    // utils.js
    export function trimStr(str){
      return str.replace(/(^\s*)|(\s*$)/g,"");
    }
  • 搜索按钮触发搜索方法,输入内容改变自动搜索

    handleSearch = e => {
        const { dispatch } = this.props;
        const { currentPage } = this.state;
        let kw = trimStr(e);
        this.setState({ keyword : kw });
        dispatch({
          type: 'newMallOrder/fetch',
          payload: {
            currentPage,
            e: {
              keyword: kw
            },
            showCount: 10
          },
        });
      };
    
      handleKwChange = () => {
        const { dispatch } = this.props;
        const { currentPage } = this.state;
        if(event && event.target && event.target.value){
          let value = event.target.value;
          this.handleSearch(value)
        }else{
          dispatch({
            type: 'newMallOrder/fetch',
            payload: {
              currentPage,
              e: {
                keyword: null
              },
              showCount: 10
            },
          });
        }
      }

 

二、选择器Select & TreeSelect

        

  • 表单中嵌入Select选择器
    <FormItem>
         {getFieldDecorator('tempTypeId',{
              initialValue: 0
         })(
          <Select placeholder="请选择" style={
         { width: '100%' }} 
                  onChange={
         this.handleTempType}>
              <Option value={0}>H5在线编辑</Option>
              <Option value={1}>贺卡</Option>
              <Option value={2}>海报</Option>
              <Option value={3}>壁纸</Option>
              <Option value={4}>全部</Option>
          </Select>
         )}
    </FormItem>

    选择方法:

    handleTempType = value => {
        const { dispatch } = this.props;
        const { keyword } = this.state;
    
        this.setState({
          tempType: tempTypeMap[value]
        })
        dispatch({
          type: 'temp/fetch',
          payload: {
            currentPage: 1,
            e: {
              keyword: keyword,
              subjectClass: tempTypeMap[value]
            },
            showCount: 2
          }
        });
        dispatch({
          type: 'temp/fetchType',
          payload: {
            ofClass: tempTypeMap[value]
          },
          callback: (res) => {
            if(res.code == 200){
               let typeList = res.data;  // 获取联动选择框的数据
      
               typeList.forEach((typeItem, index) => {
                  dispatch({
                    type: 'temp/fetchThirdType',
                    payload: typeItem.id,
                    callback: (res) => {
                        if(res.code == 200 && res.data.length){
                           typeList[index].list = res.data;
                        }
                    }
                  })
               })
               setTimeout(() => this.setState({ typeList }), 0)
            }
          }
        });
      }
  • 联动选择的第一个选择框的父级数据
    let ParentTypeData = [
        {
          title: 'H5在线编辑',
          value: 0,
          key: 0,
        },
        {
          title: '贺卡',
          value: 1,
          key: 1,
        },
        {
          title: '海报',
          value: 2,
          key: 2,
        },
        {
          title: '壁纸',
          value: 3,
          key: 3,
        },
        {
          title: '全部',
          value: 4,
          key: 4,
        },
      ];
  • 处理获取到的联动选择第二个选择框的数据为TreeSelect需要的数据格式
    let typeData = [];
    const typeTree = (typeList, typeData) => {
          if(typeList.length) {
            for(let i=0; i<typeList.length; i++){
              typeData[i] = {
                title: typeList[i].kind,
                value: typeList[i].id,
                key: typeList[i].id
              }
              //二级分类
              if(typeList[i].list){
                typeData[i].children = [];
                typeTree(typeList[i].list, typeData[i].children)
              }
            }
          }
      }
    typeTree(typeList, typeData);
  • 表单中嵌入TreeSelect选择器

     <FormItem label="主题类别" {...formLayout}>
             <TreeSelect
                   defaultValue={tempType == null ? 4 : tempTypeMap.indexOf(tempType)}
                   value={parentTypeId}
                   style={
         {display: `${editDisable ? 'none' : 'inline-block'}`, width: '47%', marginRight: '6%'}}
                   dropdownStyle={
         { maxHeight: 400, overflow: 'auto' }}
                   treeData={ParentTypeData}
                   placeholder="请选择"
                   onChange={handleParentType}
               />
              {form.getFieldDecorator('typeIds', {
                   rules: [{ type:"array", required: true, message: '请选择主题类别'}],
                   initialValue: detail.types && detail.types.length
                                ? detail.types.map((type) => type.id)
                                : []
              })(
              <TreeSelect
                   multiple   // 多选
                   style={
         {width: `${editDisable ? '100%' : '47%'}`}}
    
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值