ant Design表格使用

react 类组件
import React, { Component, createRef } from "react";
import { Form, Input, Button, Row, Col, message } from "antd";
import { withRouter } from "react-router-dom";
// 导入封装好的验证码
import Caption from '@/components/Captcha'
// 导入需要的模型
import Model from '@/models/common'
class NormalLogin extends Component {
    // 因为父组件需要获取子组件主动传递过来的数据
    //构造函数
    constructor(props){
        super(props)
        this.ref_captcha = createRef()
    }
    // 初始化状态
    state ={
        key:'',
    }
  render() {
    return (
      <div>
        <Form
          {...layout}
          name="basic"
          initialValues={{
            remember: true,
          }}
          onFinish={this.onFinish}
          onFinishFailed={this.onFinishFailed}
        >
          <Form.Item
            label="用户名"
            name="username"
            rules={[
              {
                required: true,
                message: "请输入用户名!",
              },
            ]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="密码"
            name="password"
            rules={[
              {
                required: true,
                message: "请输入密码!",
              },
            ]}
          >
            <Input.Password />
          </Form.Item>
          <Form.Item
            label="验证码"
          >
            <Row gutter={8}>
              <Col span={14}>
                <Form.Item
                  name="captcha"
                  noStyle
                  rules={[
                    {
                      required: true,
                      message: "请输入验证码!",
                    },
                  ]}
                >
                  <Input />
                </Form.Item>
              </Col>
              <Col span={10}>
                {/* 放上验证码组件 */}
                <Caption height = '30' setkey = {this.setkey} ref={this.ref_captcha}/>
              </Col>
            </Row>
          </Form.Item>
          <Form.Item {...tailLayout}>
            <Button type="primary" htmlType="submit" block>
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
    );
  }
  //   点击事件的返回值是一个对象values
  onFinish = (values) => {
    //   将state中的key属性赋值给values对象中的key属性
    values['key'] = this.state.key
    //需要在这里通过导入的axios发送请求
    Model.normalLogin(values).then((ret)=>{
        // 判断结果请求是否成功
        if(ret.errNo === 0){
            // 请求成功,成功之后跳转页面
            message.success(ret.message,1,()=>{
                this.props.history.push('/dashboard')
            })
        }else{
            // 请求失败
            message.error(ret.errText)
            // 调用子组件(验证码组件)刷新验证码
            this.ref_captcha.current.getMessage()
        }
    })
  };

  onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };

//   用于传递给子组件,获取子返回的key
setkey = (key) =>{
    this.setState(()=>{
        return {
            key,
        }
    })
}
}

// 定义表单
const layout = {
  labelCol: {
    span: 8,
  },
  wrapperCol: {
    span: 16,
  },
};
const tailLayout = {
  wrapperCol: {
    offset: 8,
    span: 16,
  },
};

export default withRouter(NormalLogin); 
···


Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐