Question:
I am not able to get the Header and Footer of the React table in the UI

Problem:

This is the code that I am using. To render this table I am using the React table library


This a Basic table component where I am rendering the table header, body, footer


    import { useMemo } from "react";

import { useTable } from "react-table";

import { COLUMNS } from "./columns";

import MOCK_DATA from "./MOCK_DATA.json";

import "./Basictable.css";


export const BasicTable = () => {

  const columns = useMemo(() => COLUMNS, []);

  const data = useMemo(() => MOCK_DATA, []);

  //   useTable({ columns: COLUMNS, data: MOCK_DATA });

  //   useTable({ columns: columns, data: data });

  const tableInstance = useTable({ columns, data });

    const {

      getTableProps,

      getTableBodyProps,

      headerGroups, // a group of headers,

      footerGroups,

      rows, //on each we access the cells, and from each cell we call the render function passing 

  in the string "cell".It picks the  value from MOCK_JSON data for each column.

      prepareRow,

    } = tableInstance;

    return (

      <table {...getTableProps()}>

        <thead>

          {headerGroups.map((headerGroup) => {

            <tr {...headerGroup.getHeaderGroupProps()}>

              {headerGroup.headers.map((column) => {

                <th {...column.getHeaderProps()}>{column.render("Header")}</th>;

                console.log(column.render("Header"));

              })}

            </tr>;

          })}

        </thead>

        <tbody {...getTableBodyProps()}>

          {rows.map((row) => {

            prepareRow(row);

            return (

              <tr {...row.getRowProps()}>

                {row.cells.map((cell) => {

                  // console.log(cell);

                  return (

                    <td {...cell.getCellProps()}>{cell.render("Cell")} </td>

                  );

                })}

              </tr>

            );

          })}

        </tbody>

        <tfoot>

          {footerGroups.map((footerGroup) => {

            <tr {...footerGroup.getFooterGroupProps()}>

              {footerGroup.headers.map((column) => {

                <td {...column.getFooterProps}>{column.render("Footer")} </td>;

                console.log(column.render("Footer"));

              })}

            </tr>;

          })}

        </tfoot>

      </table>

    );

};


column.js

    export const COLUMNS = [

  {

    Header: "Id",

    accessor: "id",

    Footer: "Id",

  },

  {

    Header: "First Name",

    accessor: "first_name",

    Footer: "First Name",

  },

  {

    Header: "Last Name",

    accessor: "last_name",

    Footer: "Last Name",

  },

  {

    Header: "Date of Birth",

    accessor: "date_of_birth",

    Footer: "Date of Birth",

  },

  {

    Header: "Country",

    accessor: "country",

    Footer: "Country",

  },

  {

    Header: "Phone",

    accessor: "phone"

    Footer: "Phone",

  },

];


But from this code I am only able to see the table body. I am not able to see the table header and footer. Please help if someone knows the solution.


And this is what I am getting in the UI 



And this is expected. enter image description here


Solution:

Do this changes in your code :



<table {...getTableProps()}>

      <thead>

        {headerGroups.map((headerGroup) => (

          <tr {...headerGroup.getHeaderGroupProps()}>

            {headerGroup.headers.map((column) => (

              <th {...column.getHeaderProps()}>{column.render("Header")}</th>

            ))}

          </tr>

        ))}

      </thead>

      <tbody {...getTableBodyProps()}>

        {rows.map((row) => {

          prepareRow(row);

          return (

            <tr {...row.getRowProps()}>

              {row.cells.map((cell) => (

                <td {...cell.getCellProps()}>{cell.render("Cell")}</td>

              ))}

            </tr>

          );

        })}

      </tbody>

      <tfoot>

        {footerGroups.map((footerGroup) => (

          <tr {...footerGroup.getFooterGroupProps()}>

            {footerGroup.headers.map((column) => (

              <td {...column.getFooterProps()}>{column.render("Footer")}</td>

            ))}

          </tr>

        ))}

      </tfoot>

    </table>


Suggested blogs:

>Step by Step guide to Deploy Terraform in Azure using GitHub Actions

>Testing react components using Hooks and Mocks

>How to destroy a custom object within the use Effect hook in TypeScript?

>Create a function that convert a dynamic Json to a formula in Typescript

>Narrow SomeType vs SomeType[]

>Use Firebase Realtime Database with ASP.NET MVC App

>Use of Singletons in .NET Core in AWS Lambda

>How to do PHP Decryption from Node.js Encryption

>How to Set up the Android emulator?

>How to Set up the local environment for Angular development?

>How to solve encoding issue when writing to a text file, with Python?


Ritu Singh

Ritu Singh

Submit
0 Answers