React

React Ag-grid 띄우기

쿠카곰돌이 2023. 7. 19. 15:40
반응형

 

npx create-react-app . --template typescript

npm install typescript @types/node @types/react @types/react-dom @types/jest

yarn add typescript @types/node @types/react @types/react-dom @types/jest

npm start


* Cannot find module 'react-dom/client' or its corresponding type declarations.ts(2307)
-- npm install --save @types/react-dom
npm i @types/react-router-dom


npm i react-redux

npm i @types/react-router-dom
npm i react-router-dom



Module not found: Error: Can't resolve 'ag-grid-community/styles/ag-grid.css

npm i @ag-grid-community/react

npm i -s react-router-dom

npm install @ag-grid-community/all-modules

 

 

import { useCallback, useMemo, useState } from "react";
import { AgGridReact } from "ag-grid-react";

import '@ag-grid-community/core/dist/styles/ag-grid.css'
import "@ag-grid-community/core/dist/styles/ag-theme-alpine.css";

import {
  CheckboxSelectionCallbackParams,
  ColDef,
  ColGroupDef,
  Grid,
  GridOptions,
  GridReadyEvent,
  HeaderCheckboxSelectionCallbackParams,
} from "ag-grid-community";

export interface IOlympicData {
  athlete: string;
  age: number;
  country: string;
  year: number;
  date: string;
  sport: string;
  gold: number;
  silver: number;
  bronze: number;
  total: number;
}

var checkboxSelection = function (params: CheckboxSelectionCallbackParams) {
  // we put checkbox on the name if we are not doing grouping
  return params.columnApi.getRowGroupColumns().length === 0;
};

var headerCheckboxSelection = function (
  params: HeaderCheckboxSelectionCallbackParams
) {
  // we put checkbox on the name if we are not doing grouping
  return params.columnApi.getRowGroupColumns().length === 0;
};

type props = {
  rowData: IOlympicData[];
  pagination: number;
};

export const GridComponent = ({ rowData, pagination }: props) => {
  const containerStyle = useMemo(
    () => ({ width: "100%", height: "600px" }),
    []
  );
  const gridStyle = useMemo(() => ({ height: "100%", width: "100%" }), []);

  const [columnDefs, setColumnDefs] = useState<ColDef[]>([
    {
      field: "athlete",
      minWidth: 170,
      checkboxSelection: checkboxSelection,
      headerCheckboxSelection: headerCheckboxSelection,
    },
    { field: "age" },
    { field: "country" },
    { field: "year" },
    {
      field: "date",
      filter: "agDateColumnFilter",
      filterParams: {
        // provide comparator function
        comparator: (filterLocalDateAtMidnight: any, cellValue: any) => {
          const dateAsString = cellValue;

          if (dateAsString == null) {
            return 0;
          }

          // In the example application, dates are stored as dd/mm/yyyy
          // We create a Date object for comparison against the filter date
          const dateParts = dateAsString.split("/");
          const year = Number(dateParts[2]);
          const month = Number(dateParts[1]) - 1;
          const day = Number(dateParts[0]);
          const cellDate = new Date(year, month, day);

          // Now that both parameters are Date objects, we can compare
          if (cellDate < filterLocalDateAtMidnight) {
            return -1;
          } else if (cellDate > filterLocalDateAtMidnight) {
            return 1;
          }
          return 0;
        },
      },
    },
    { field: "sport" },
    { field: "gold" },
    { field: "silver" },
    { field: "bronze" },
    { field: "total" },
  ]);

  const defaultColDef = useMemo<ColDef>(() => {
    return {
      editable: true,
      sortable: true,
      resizable: true,
      filter: true,
      flex: 1,
      minWidth: 100,
    };
  }, []);

  const onGridReady = useCallback((params: GridReadyEvent) => {}, []);

  return (
    <div style={containerStyle}>
      <div style={gridStyle} className="ag-theme-alpine">
        <AgGridReact
          rowData={rowData}
          columnDefs={columnDefs}
          defaultColDef={defaultColDef}
          suppressRowClickSelection={true}
          groupSelectsChildren={true}
          rowSelection={"multiple"}
          rowGroupPanelShow={"always"}
          pivotPanelShow={"always"}
          enableRangeSelection={true}
          pagination={true}
          paginationPageSize={pagination}
          onGridReady={onGridReady}
        ></AgGridReact>
      </div>
    </div>
  );
};
{
  "name": "ag-grid-poc",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@ag-grid-community/all-modules": "^27.3.0",
    "@ag-grid-community/react": "^30.0.5",
    "@reduxjs/toolkit": "^1.9.5",
    "@types/jest": "^27.5.2",
    "@types/node": "^17.0.45",
    "@types/react": "^17.0.62",
    "@types/react-dom": "^17.0.20",
    "@types/react-router-dom": "^5.3.3",
    "ag-grid-community": "^26.2.1",
    "ag-grid-react": "^26.2.0",
    "bootstrap": "^5.2.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^8.1.1",
    "react-router-dom": "^6.14.2",
    "react-scripts": "5.0.0",
    "typescript": "^4.9.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
반응형