React antd table使用react-resizable实现伸缩列

首先声明注意点:

1、设置列宽的width一定要用number类型。(如果不使用number类型,会出现第一次点击时卡顿的情况)

2、less中需要设置类的样式,如果无效,可以尝试加上:global(类名)

直接上demo。

index.jsx:  (展示组件)

import React from "react";
import ReactDOM from "react-dom";
import ResizeableTable from "./ResizeableTable";


const data = [
  {
    key: 0,
    date: "2018-02-11",
    amount: 120,
    type: "income",
    note: "transfer"
  },
  {
    key: 1,
    date: "2018-03-11",
    amount: 243,
    type: "income",
    note: "transfer"
  },
  {
    key: 2,
    date: "2018-04-11",
    amount: 98,
    type: "income",
    note: "transfer"
  }
];


const columns = [
  {
    title: "Date",
    dataIndex: "date",
    width: 200,
    sorter: (a, b) => a.date.length - b.date.length,
    sortDirections: ["descend"]
  },
  {
    title: "Amount",
    dataIndex: "amount",
    width: 100,
    sorter: (a, b) => {
      console.log("Amount sort");
      return a.amount - b.amount;
    },
    sortDirections: ["descend"]
  },
  {
    title: "Type",
    dataIndex: "type",
    width: 100
  },
  {
    title: "Note",
    dataIndex: "note",
    width: 100
  },
  {
    title: "Action",
    key: "action",
    render: () => <a href="javascript:;">Delete</a>
  }
];

class App extends React.PureComponent {
  render() {
    return (
      <div>
        <ResizeableTable
          columns={columns}
          dataSource={data}
        />
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Resizeable.js   (排序和拖拽列功能的table组件)

import * as React from "react";
import { Table } from "antd";
import "antd/dist/antd.css";
import { Resizable } from "react-resizable";
import "./resizeable-table.less";

class ResizeableTitle extends React.Component {
  render() {
    const { onResize, width, onClick, ...restProps } = this.props;
    console.log({ ...restProps }, "!!!");

    if (!width) {
      return <th {...restProps} />;
    }
    return (
      <Resizable
        width={width}
        height={0}
        onResizeStart={() => (this.resizing = true)}
        onResizeStop={() => {
          setTimeout(() => {
            this.resizing = false;
          });
        }}
        onResize={onResize}
      >
        <th
          onClick={(...args) => {
            console.log(">>>", this.resizing);
            if (!this.resizing && onClick) {
              onClick(...args);
            }
          }}
          {...restProps}
        />
      </Resizable>
    );
  }
}

class ResizeableTable extends React.PureComponent {
  constructor(props) {
    super(props);

    this.state = {
      columns: props.columns
    };
  }

  components = {
    header: {
      cell: ResizeableTitle
    }
  };

  componentDidMount() {
    const handlers = document.querySelectorAll(
      ".react-resizable .react-resizable-handle"
    );
    console.log(handlers, "@@@");
    handlers.forEach((handler) =>
      handler.addEventListener("click", (e) => {
        console.log("e", e);
        return false;
      })
    );
  }

  render() {
    const columns = this.state.columns.map((col, index) => ({
      ...col,
      width: col.width || 120,  // 列伸缩必须要有宽度
      onHeaderCell: (column) => ({
        width: column.width,
        onResize: this.handleResize(index)
      })
    }));
    console.log(columns, "####");

    const components = Object.assign(
      {},
      this.props.components,
      this.components
    );

    return <Table {...this.props} columns={columns} components={components} />;
  }

  handleResize = (index) => (e, { size }) => {
    console.log("handleResize", e);
    e.stopImmediatePropagation();
    // e.preventDefault();
    this.setState(({ columns }) => {
      const nextColumns = [...columns];
      nextColumns[index] = {
        ...nextColumns[index],
        width: size.width
      };
      return { columns: nextColumns };
    });
  };
}

export default ResizeableTable;

resizeable-table.less   (拖拽列必须要设置的样式) 

.react-resizable {
  position: relative;
}

.react-resizable-handle {
  position: absolute;
  width: 10px;
  height: 100%;
  bottom: 0;
  right: -5px;
  cursor: col-resize;
  background: red;
  z-index: 999;
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用antd-mobile和react-router-dom来实现tabs功能。首先,你需要安装antd-mobile和react-router-dom依赖包。 ```shell npm install antd-mobile react-router-dom ``` 接下来,你可以创建一个Tabs组件,并在其中使用antd-mobile的TabBar组件来实现tabs布局。然后,使用react-router-dom的Route组件来定义每个tab对应的页面。 ```jsx import React from 'react'; import { TabBar } from 'antd-mobile'; import { BrowserRouter as Router, Route, Link } from 'react-router-dom'; const Tabs = () => { const tabItems = [ { title: 'Tab 1', path: '/tab1', component: Tab1 }, { title: 'Tab 2', path: '/tab2', component: Tab2 }, { title: 'Tab 3', path: '/tab3', component: Tab3 }, ]; const renderTabs = () => { return tabItems.map((item) => ( <TabBar.Item key={item.path} title={item.title} icon={<div />} selectedIcon={<div />} selected={window.location.pathname === item.path} onPress={() => { window.location.href = item.path; }} /> )); }; return ( <Router> <div style={{ position: 'fixed', width: '100%', bottom: 0 }}> <TabBar>{renderTabs()}</TabBar> </div> {/* Define routes */} {tabItems.map((item) => ( <Route key={item.path} path={item.path} component={item.component} /> ))} </Router> ); }; const Tab1 = () => <div>Tab 1 content</div>; const Tab2 = () => <div>Tab 2 content</div>; const Tab3 = () => <div>Tab 3 content</div>; export default Tabs; ``` 在上面的代码中,我们定义了三个tab,分别对应不同的路径和组件。通过点击tab,我们可以切换显示不同的内容。你可以根据自己的需求修改tab的数量、标题、路径和对应的组件。 希望这可以帮助你实现antd-mobile和react-router-dom的tabs功能!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值