dlrandy / note-issues

2 stars 0 forks source link

code #147

Open dlrandy opened 4 years ago

dlrandy commented 4 years ago

` import React, { useReducer, useState } from "react"; import ReactDOM, { findDOMNode } from "react-dom"; import ReactDataGrid from "react-data-grid"; import { range } from "lodash"; import { Icon } from 'antd' // import { Menu } from "react-data-grid-addons";

import createRowData, { createFakeRow } from "./createRowData";

// import "./styles.css";

class RecordsTable extends React.Component { constructor(props) { super(props); this.canvas = null; this.copyRef = React.createRef(); this.scrollListener = () => { if ( this.canvas.scrollHeight === this.canvas.scrollTop + this.canvas.clientHeight ) { this.props.loadNextPage(); } }; }

canvas = null; state = { copyData: {} };

componentDidMount() { this.canvas = findDOMNode(this).querySelector(".react-grid-Canvas"); this.canvas.addEventListener("scroll", this.scrollListener); // document.addEventListener("copy", this.handleCopy); }

componentWillUnmount() { if (this.canvas) { this.canvas.removeEventListener("scroll", this.scrollListener); } document.removeEventListener("copy", this.handleCopy); }

parseCopyData = (copyData) => { console.log(copyData, "-------"); const { topLeft, bottomRight } = copyData; if (!topLeft || !bottomRight) { return; } // Loop through each row const text = range(topLeft.rowIdx, bottomRight.rowIdx + 1) .map( // Loop through each column 这里有和没有选择框索引是不一样的 (rowIdx) => columns .slice(topLeft.idx - 1, bottomRight.idx) .map( // Grab the row values and make a text string (col) => this.rowGetter(rowIdx)[col.key] ) .join("\t") ) .join("\n"); return text; };

executeCopy = (copyData) => { const text = this.parseCopyData(copyData); console.log(text); this.copyRef.current.value = text; this.copyRef.current.select(); document.execCommand("copy"); };

handleCopy = (e) => { console.debug("handleCopy Called"); e.preventDefault(); const text = this.parseCopyData(this.state.copyData); console.debug("text", text); e.clipboardData.setData("text/plain", text); };

rowGetter = (i) => this.props.records[i];

render() { return (