• React版Ant Design 4.5.4中Table动态获取数据(React)


     
    import React from 'react'
    import { Row, Col, Space, Table, Button, notification, Pagination } from 'antd';
    import axios from 'axios'
    
    export default class TableCom extends React.Component {
      state = {
        tableData: [],
        selectedRowKeys: [], // Check here to configure the default column
        total: 30, // for Pagination
        columns: [
          {
            title: 'ID',
            dataIndex: 'id',
             30,
          },
          {
            title: '标题',
            dataIndex: 'title',
             500,
          },
          {
            title: '内容',
            dataIndex: 'content'
          },
        ]
      };
    
      handleDeleteArticle(){
        if(this.state.selectedRowKeys.length === 0){
          notification['error']({
            message: '错误提示',
            description:
              '请选择要删除的内容!',
          });
        }else{
          notification['success']({
            message: '正确提示',
            description:
              `将要删除${JSON.stringify(this.state.selectedRowKeys)}`,
          });
        }
      }
    
      onSelectChange = selectedRowKeys => {
        console.log('selectedRowKeys changed: ', selectedRowKeys);
        this.setState({ selectedRowKeys });
      };
    
      // 获取表格数据
      getData(pageNumber, pageSize) {
        axios.get(`/article/all/?pageSize=${pageSize}&pageNumber=${pageNumber}&sortName=id&sortOrder=desc&_=1595230808893`).then((resp) => {
          console.log("all data:");
          console.log(resp);
          let ajaxData = [];
          for (let i = 0; i < resp.data.rows.length; i++) {
            ajaxData.push({
              key: resp.data.rows[i].id,
              id: resp.data.rows[i].id,
              title: resp.data.rows[i].title,
              content: resp.data.rows[i].content,
            });
          }
    
          this.setState({
            tableData: ajaxData,
            total: resp.data.total
          })
    
        }, (err) => {
          console.log(err);
        });
      }
    
      onChange = (pageNumber, pageSize) => {
        this.getData(pageNumber, pageSize);
      };
    
      componentDidMount() {
        this.getData(1, 10);
      }
    
      render() {
        const { selectedRowKeys } = this.state;
    
        const rowSelection = {
          selectedRowKeys,
          onChange: this.onSelectChange
        };
    
        return (<>
          <Row style={{paddingBottom: 15}}>
            <Col span={24}><Space size="small"><Button type="primary" ghost>添加</Button><Button type="primary" ghost onClick={()=>this.handleDeleteArticle()}>删除</Button></Space></Col>
          </Row>
          <Row>
            <Col span={24}>
              <Table columns={this.state.columns} dataSource={this.state.tableData} rowSelection={rowSelection} pagination={false} bordered
                onRow={record => {
                  return {
                    onClick: event => { console.log(record) },
                    onDoubleClick: event => { console.log(event) }
                  }
                }}
              >
              </Table>
              <div style={{ height: 15 }}></div>
              <Pagination
                total={this.state.total}
                showSizeChanger
                showQuickJumper
                showTotal={total => `共 ${total} 条`}
                onChange={this.onChange}
              />
            </Col>
          </Row>
        </>);
      }
    }

    其中:package.json

    {
      "name": "antd-demo",
      "version": "0.1.0",
      "private": true,
      "dependencies": {
        "@ant-design/icons": "^4.2.1",
        "@babel/core": "7.9.0",
        "@svgr/webpack": "4.3.3",
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.3.2",
        "@testing-library/user-event": "^7.1.2",
        "@typescript-eslint/eslint-plugin": "^2.10.0",
        "@typescript-eslint/parser": "^2.10.0",
        "antd": "^4.4.1",
        "axios": "^0.19.2",
        "babel-eslint": "10.1.0",
        "babel-jest": "^24.9.0",
        "babel-loader": "8.1.0",
        "babel-plugin-named-asset-import": "^0.3.6",
        "babel-preset-react-app": "^9.1.2",
        "bootstrap": "^3.3.7",
        "bootstrap-table": "^1.11.1",
        "bootstrapvalidator": "^0.5.4",
        "camelcase": "^5.3.1",
        "case-sensitive-paths-webpack-plugin": "2.3.0",
        "css-loader": "3.4.2",
        "dotenv": "8.2.0",
        "dotenv-expand": "5.1.0",
        "eslint": "^6.6.0",
        "eslint-config-react-app": "^5.2.1",
        "eslint-loader": "3.0.3",
        "eslint-plugin-flowtype": "4.6.0",
        "eslint-plugin-import": "2.20.1",
        "eslint-plugin-jsx-a11y": "6.2.3",
        "eslint-plugin-react": "7.19.0",
        "eslint-plugin-react-hooks": "^1.6.1",
        "file-loader": "4.3.0",
        "fs-extra": "^8.1.0",
        "html-webpack-plugin": "4.0.0-beta.11",
        "identity-obj-proxy": "3.0.0",
        "jest": "24.9.0",
        "jest-environment-jsdom-fourteen": "1.0.1",
        "jest-resolve": "24.9.0",
        "jest-watch-typeahead": "0.4.2",
        "jquery": "^3.3.1",
        "mini-css-extract-plugin": "0.9.0",
        "optimize-css-assets-webpack-plugin": "5.0.3",
        "pnp-webpack-plugin": "1.6.4",
        "postcss-flexbugs-fixes": "4.1.0",
        "postcss-loader": "3.0.0",
        "postcss-normalize": "8.0.1",
        "postcss-preset-env": "6.7.0",
        "postcss-safe-parser": "4.0.1",
        "react": "^16.13.1",
        "react-app-polyfill": "^1.0.6",
        "react-dev-utils": "^10.2.1",
        "react-dom": "^16.13.1",
        "react-router": "^4.2.0",
        "react-router-dom": "^4.2.0",
        "react-transition-group": "^4.4.1",
        "resolve": "1.15.0",
        "resolve-url-loader": "3.1.1",
        "sass-loader": "8.0.2",
        "semver": "6.3.0",
        "style-loader": "0.23.1",
        "terser-webpack-plugin": "2.3.5",
        "ts-pnp": "1.1.6",
        "url-loader": "2.3.0",
        "webpack": "4.42.0",
        "webpack-dev-server": "3.10.3",
        "webpack-manifest-plugin": "2.2.0",
        "workbox-webpack-plugin": "4.3.1",
        "ztree": "^3.5.24"
      },
      "scripts": {
        "start": "node scripts/start.js",
        "build": "node scripts/build.js",
        "test": "node scripts/test.js"
      },
      "eslintConfig": {
        "extends": "react-app"
      },
      "proxy": "http://localhost:9999",
      "homepage": ".",
      "browserslist": {
        "production": [
          ">0.2%",
          "not dead",
          "not op_mini all"
        ],
        "development": [
          "last 1 chrome version",
          "last 1 firefox version",
          "last 1 safari version"
        ]
      },
      "jest": {
        "roots": [
          "<rootDir>/src"
        ],
        "collectCoverageFrom": [
          "src/**/*.{js,jsx,ts,tsx}",
          "!src/**/*.d.ts"
        ],
        "setupFiles": [
          "react-app-polyfill/jsdom"
        ],
        "setupFilesAfterEnv": [
          "<rootDir>/src/setupTests.js"
        ],
        "testMatch": [
          "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
          "<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
        ],
        "testEnvironment": "jest-environment-jsdom-fourteen",
        "transform": {
          "^.+\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
          "^.+\.css$": "<rootDir>/config/jest/cssTransform.js",
          "^(?!.*\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
        },
        "transformIgnorePatterns": [
          "[/\\]node_modules[/\\].+\.(js|jsx|ts|tsx)$",
          "^.+\.module\.(css|sass|scss)$"
        ],
        "modulePaths": [],
        "moduleNameMapper": {
          "^react-native$": "react-native-web",
          "^.+\.module\.(css|sass|scss)$": "identity-obj-proxy"
        },
        "moduleFileExtensions": [
          "web.js",
          "js",
          "web.ts",
          "ts",
          "web.tsx",
          "tsx",
          "json",
          "web.jsx",
          "jsx",
          "node"
        ],
        "watchPlugins": [
          "jest-watch-typeahead/filename",
          "jest-watch-typeahead/testname"
        ]
      },
      "babel": {
        "presets": [
          "react-app"
        ]
      }
    }
    
  • 相关阅读:
    GTC China 2016观感
    关于OpenGL的绘制上下文
    Voreen(三) 光线投射参数介绍
    分享一些DICOM数据下载网站
    Voreen (二) 入点出点计算
    Voreen (一) GPU Raycast主流程
    GPU渲染和GDI
    程序媛壮志雄心尝试装机,命运多舛壮志未酬失败告终~
    安装Newton版Glance
    安装Newton版Swift,配合keystone认证
  • 原文地址:https://www.cnblogs.com/samve/p/13455312.html
Copyright © 2020-2023  润新知