React Table Sticky Header without external library

Hello everyone, earlier we have developed a react table view solution for frozen column, if you missed the post please find it here :

https://reactcodes.blogspot.com/2019/11/react-fixed-table-header-code-without.html


Today we will try to make the table header fixed but make the data vertically scrollable, below demo shows how the final result will be :



To get the full codebase you may see my github link :

https://github.com/reactcodes/react-sticky-table


Explanation of process :

Initially if you clone the codebase within App.js you will see that I have separated table header items and put those in an array

const tableHeaders = [
"First Column",
"Second Column",
"Third Column",
"Fourth Column",
"Fifth Column",
"Sixth Column",
"Seventh Column",
"Eight Column",
]

Then I have the table data coming as a json

I have the stickytableheader component, I have passed both info to the component

<StickyHeaderTable headers={tableHeaders} data={json} />

Then I have taken two separate tables within that component, one is for the headers which I am not giving any scroll property via css and keeping it fixed

For second table I have wrapped the table within a div with class name container, giving div a fixed height if you need to increase the height of scrollable area you may adjust the css

Next I have made div's overflow-y property to scroll , that is the main trick

.container{
height: 200px;
overflow-y: scroll;
}

Well this is it, add some extra styles if you need, benefit of using this code is you don't need to rely on any 3rd party dependency, so in case of any bugs you can solve yourself.

Thanks for your time, please consider following my blog... :)


Comments

  1. Hello, I found your idea working, but there are few problems like if we reduce the width of window then horizontal scroll get added to the react table and hence another horizontal scroll get added to the header's table. In one case of react table, I'm using a checkbox to select all data present on that page, previously I was having an access to that data present on specific page using "getTheadTrProps" method but now I am unable to fetch that data and making that functionality working as the header and data tables are different now. Can you suggest any solution for above problems.

    ReplyDelete

Post a Comment

Popular posts from this blog

Best coding practice and structure for redux saga with react hooks | Separate watcher and worker saga | React clean code

How to use redux with React Hooks - Creating TodoList

Setting up Redux Devtools for React applications