-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
code #147
Comments
import React from 'react'; import Toolbar from './components/Toolbar';
} import { Select } from 'antd';
} import { debounce } from 'lodash'; const InputArea = Input.TextArea; export class TextEditor extends React.Component {
} export class TextNumEditor extends React.Component {
} const dateFormat = 'YYYY-MM-DD HH:mm:ss';
} |
`
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 (
<textarea ref={this.copyRef} style={{ width: 0, height: 0, opacity:0 }} />
<ReactDataGrid
ref={"grid"}
minHeight={this.props.minHeight}
rowHeight={30}
headerRowHeight={40}
enableCellSelect={true}
cellRangeSelection={{
// onStart: (args) => console.log('start',args),
// onUpdate: (args) => console.log('update',args),
onComplete: (args) => {
console.log("complete", args);
this.setState((state) => ({
copyData: args
}));
this.executeCopy(args);
}
}}
columns={this.props.columns}
rowGetter={this.rowGetter}
rowsCount={this.props.records.length}
rowSelection={{
showCheckbox: true,
enableShiftSelect: true,
onRowsSelected: (rows) => {
const ids = rows.map(({ row }) => row.id);
const records = this.props.records.filter(
(r) => ids.indexOf(r.id) > -1
);
this.props.selectRecords(records);
},
onRowsDeselected: (rows) => {
const ids = rows.map(({ row }) => row.id);
const records = this.props.records.filter(
(r) => ids.indexOf(r.id) > -1
);
this.props.deselectRecords(records);
},
selectBy: {
// indexes: this.props.selectedRecords.map((r) =>
// this.props.records.indexOf(r)
// )
}
}}
getCellActions={this.props.getCellActions}
/>
);
}
}
const defaultColumnProperties = {
// sortable: true,
width: 120,
resizable: true
};
const columns = [
{
key: "id",
name: "ID",
sortDescendingFirst: true
},
{
key: "title",
name: "Title"
},
{
key: "firstName",
name: "First Name"
},
{
key: "lastName",
name: "Last Name"
},
{
key: "email",
name: "Email"
},
{
key: "street",
name: "Street"
},
{
key: "zipCode",
name: "ZipCode"
},
{
key: "date",
name: "Date"
},
{
key: "jobTitle",
name: "Job Title"
},
{
key: "catchPhrase",
name: "Catch Phrase"
},
{
key: "jobArea",
name: "Job Area"
},
{
key: "jobType",
name: "Job Type"
}
].map((c) => ({ ...c, ...defaultColumnProperties }));
const ROW_COUNT = 50;
let pages = 1;
const firstNameActions = [
{
icon:,
callback: () => {
alert("Deleting");
}
},
{
icon: ,
actions: [
{
text: "Option 1",
callback: () => {
alert("Option 1 clicked");
}
},
{
text: "Option 2",
callback: () => {
alert("Option 2 clicked");
}
}
]
}
];
export default function Example({ rows = createRowData(50 * pages), loadNextPage=() => createRowData(50 * ++pages) }) {
const [state, dispatch] = React.useReducer(
(state, action) => state.concat(action),
rows
);
function getCellActions(column, row) {
const cellActions = {
jobType: firstNameActions
};
return cellActions[column.key];
}
return (
<RecordsTable
columns={columns}
rowGetter={(i) => rows[i]}
rowsCount={ROW_COUNT}
minHeight={500}
records={state}
loadNextPage={() => dispatch(loadNextPage())}
cellRangeSelection={{
onStart: (args) => console.log(rows),
onUpdate: (args) => console.log(rows),
onComplete: (args) => console.log(rows)
}}
getCellActions={getCellActions}
/>
);
}
`
The text was updated successfully, but these errors were encountered: