如何使用导出在非索引文件中呈现react-data-grid?

问题描述 投票:2回答:1

我使用react-data-grid来渲染数据,但它只在index.js文件中工作,但是当我在普通文件中使用时,我无法通过定义类和函数名来导出文件。

尝试使用类名和函数名导出。

import React, { useState, Component } from "react";
import ReactDOM from "react-dom";
import ReactDataGrid from "react-data-grid";
import { Toolbar, Data } from "react-data-grid-addons";
import createRowData from "./createRowData";

const defaultColumnProperties = {
  filterable: true,
  width: 200
};

const selectors = Data.Selectors;

const columns = [
  {
    key: "ID",
    name: "ID"
  },
  {
    key: "Data",
    name: "Data"
  },
  {
    key: "firstname",
    name: "first name"
  },
  {
    key: "amount",
    name: "Amount"
  }
].map(c => ({ ...c, ...defaultColumnProperties }));

const ROW_COUNT = 50;

const handleFilterChange = filter => filters => {
  const newFilters = { ...filters };
  if (filter.filterTerm) {
    newFilters[filter.column.key] = filter;
  } else {
    delete newFilters[filter.column.key];
  }
  return newFilters;
};

function getRows(rows, filters) {
  return selectors.getRows({ rows, filters });
}

function Example({ rows }) {
  const [filters, setFilters] = useState({});
  const filteredRows = getRows(rows, filters);
  return (
    <ReactDataGrid
      columns={columns}
      rowGetter={i => filteredRows[i]}
      rowsCount={filteredRows.length}
      minHeight={300}
      minWidth={1300}
      toolbar={<Toolbar enableFilter={true} />}
      onAddFilter={filter => setFilters(handleFilterChange(filter))}
      onClearFilters={() => setFilters({})}
    />
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Example rows={createRowData(50)} />, rootElement);

需要导出文件以进行路由,需要从另一个文件中获取数据以便在网格视图中发布以及搜索字段的过滤器功能。

reactjs react-data-grid
1个回答
0
投票

我想我理解你所面临的问题。你能改变上面这个吗?

 const ROW_COUNT = 50;


const handleFilterChange = filter => filters => {
    const newFilters = { ...filters };
    if (filter.filterTerm) {
      newFilters[filter.column.key] = filter;
    } else {
      delete newFilters[filter.column.key];
    }
    return newFilters;
  };

  function getRows(rows, filters) {
    return selectors.getRows({ rows, filters });
  }

function Example({rows}) {
    //const rows = createRowData(50);
    const [filters, setFilters] = useState({});
    const filteredRows = getRows(rows, filters);
    return (
      <ReactDataGrid
        columns={columns}
        rowGetter={i => filteredRows[i]}
        rowsCount={filteredRows.length}
        minHeight={500}
        toolbar={<Toolbar enableFilter={true} />}
        onAddFilter={filter => setFilters(handleFilterChange(filter))}
        onClearFilters={() => setFilters({})}
      />
    );
  }
class datagrid extends Component {

    render() {
        return(
        <Example rows={createRowData(50)} />);
    }
}

export default datagrid;
热门问题
推荐问题
最新问题