zoukankan      html  css  js  c++  java
  • xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

    useState & useEffect

    https://overreacted.io/zh-hans/a-complete-guide-to-useeffect/

    https://reactjs.org/docs/hooks-reference.html#useeffect

    https://reactjs.org/docs/hooks-effect.html

    
    
    
    

    antd table

    antd table 修复 分页初始值 bug

    
    import React, {
      useState,
      useEffect,
    } from 'react';
    
    

    
      const [current, setCurrent] = useState(1);
      const [tableData, setTableData] = useState(regions);
      useEffect(() => {
        setCurrent(1);
        let isSubscribed = true;
        if(isSubscribed) {
           // cancel promise
        }
        return () => isSubscribed = false;
      }, [adcode, regionData, regionName, regions]);
    
    
    
      useEffect(() => {
        setCurrent(1);
        let isSubscribed = true;
        if(isSubscribed) {
          getRegionName(adcode).then(setRegionName);
        }
        const promises = [];
        regions.forEach(item => {
          promises.push(
            getRegionName(item.code)
            .then(name => {item.name = name;})
            // eslint-disable-next-line no-console
            .catch(() => console.log('获取%s地名失败', item.code)),
          );
        });
        // 后端传来部分区划代码是错误的,找不到对应地名,暂时先剔除
        Promise
        .all(promises)
        .then(() => {
          if(isSubscribed) {
            return setTableData(regions.filter(({ name }) => name));
          }
        });
        return () => isSubscribed = false;
      }, [adcode, regionData, regionName, regions]);
    
    

    
      return (
        <ExportableTable
          filename={filename}
          title={() => (
            <>
              {regionName}
              <KpiTips tips={tips} />
            </>
          )}
          rowKey="code"
          bordered={false}
          size="small"
          pagination={{
            current,
            pageSize: 10,
            showSizeChanger: true,
            pageSizeOptions: ['5', '10', '20'],
            showQuickJumper: true,
            showTotal: () => <span>共 {tableData.length} 条记录</span>,
          }}
          columns={columns}
          dataSource={tableData}
          defaultCurrent={1}
          onChange={(p) => {
            setCurrent(p.current);
          }}
        />
      );
    
    

    export excel

    
    import XLSX from 'xlsx';
    
    
    
    import { Table, Button } from 'antd';
    // import { exportExcel } from '@/utils/exportUtils';
    import { exportExcel } from '../../utils/exportUtils';
    
    function noop() {}
    
    const ExportableTable = props => {
      const { filename, title = noop, columns, dataSource } = props;
    
      const _export = () => {
        exportExcel({ name: filename, columns, dataSource });
      };
    
      // 追加导出按钮
      const _renderTitle = () => {
        return (
          <div style={{ display: 'flex', justifyContent: 'space-between' }}>
            <div>{title()}</div>
            <Button icon="download" size="small" onClick={_export}>
              导出
            </Button>
          </div>
        );
      };
    
      return <Table {...props} title={_renderTitle} />;
    };
    
    export default ExportableTable;
    
    
    
    
    
    const exportExcel = ({ name, columns, dataSource }) => {
      const sheetName = 'sheet';
      const aoa = transformAOA({ columns, dataSource });
    
      const sheet = XLSX.utils.aoa_to_sheet(aoa);
    
      const workbook = {
        SheetNames: [sheetName],
        Sheets: { [sheetName]: sheet },
      };
      const opts = { bookSST: false, type: 'binary' };
    
      XLSX.writeFile(workbook, `${name}.xlsx`, opts);
    };
    
    
    

    refs



    ©xgqfrms 2012-2020

    www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


  • 相关阅读:
    c# 反射取其他项目的资源文件
    【分享】免费建立自己的站点
    c# 自定义类型的DataBindings
    ListView 多行拖拽排序
    linq to sql之组装where条件下的'或'语句
    dotfuscator使用方法
    orderBy 传入属性的字符串
    WCF数据交互时长度超过8192
    ASP.net中aspx与cs函数的互调
    c# 读取excel数据的两种方法
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/11771834.html
Copyright © 2011-2022 走看看