0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ReactのMaterialUIでTBLにソート機能を実装する

Last updated at Posted at 2022-05-30

Material UIでソート機能を実装する。

成果物の完成イメージ

スクリーンショット 2022-05-30 22.42.09.png

App.tsx
import { SampleTable } from "./SampleTable";

export default function App() {
  return <SampleTable />;
}
SampleTable.tsx
import { useState, VFC } from "react";
import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import TableSortLabel from "@mui/material/TableSortLabel";

interface TableLayout {
  id: number;
  name: string;
  score: number;
}

function createData(id: number, name: string, score: number): TableLayout {
  return {
    id,
    name,
    score,
  };
}

const rows = [
  createData(1, "Koji", 75),
  createData(2, "Bryant", 48),
  createData(3, "k.k.Factory", 89),
  createData(4, "Subaru", 78),
  createData(5, "Yokosawa", 55)
];

function descendingComparator<T>(a: T, b: T, orderBy: keyof T) {
  if (b[orderBy] < a[orderBy]) {
    return -1;
  }
  if (b[orderBy] > a[orderBy]) {
    return 1;
  }
  return 0;
}

type Order = "asc" | "desc";

function getComparator<Key extends keyof any>(
  order: Order,
  orderBy: Key
): (
  a: { [key in Key]: number | string },
  b: { [key in Key]: number | string }
) => number {
  return order === "desc"
    ? (a, b) => descendingComparator(a, b, orderBy)
    : (a, b) => -descendingComparator(a, b, orderBy);
}

export const SampleTable: VFC = () => {
  const [order, setOrder] = useState<Order>("asc");
  const handleOrderChange = (property: keyof TableLayout) => (
    event: React.MouseEvent<unknown>
  ) => {
    setOrder(order === "asc" ? "desc" : "asc");
  };

  return (
    <TableContainer>
      <Table>
        <TableHead>
          <TableRow>
            <TableCell>
            <TableSortLabel
                active
                direction={order}
                onClick={handleOrderChange("id")}>ID</TableSortLabel>
              </TableCell>
            <TableCell>
                氏名

            </TableCell>
            <TableCell>
                成績
            </TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          {rows.sort(getComparator(order, "id")).map((row, index) => {
            return (
              <TableRow key={row.id}>
                <TableCell>{row.id}</TableCell>
                <TableCell>{row.name}</TableCell>
                <TableCell>{row.score}</TableCell>
              </TableRow>
            );
          })}
        </TableBody>
      </Table>
    </TableContainer>
  );
};

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?