How to upload a file to a cell in Handsontable data grid

Akash Mittal Recipes / September 14, 2023

How to upload a file to a cell in Handsontable data grid

File upload is an essential process for storing blobs of data that encompass various formats such as images, videos, documents, presentations, spreadsheets, PDFs, and more. Without the capability to upload files, it would be challenging to display entities like images on a webpage.

Whilebase64strings can be used for this purpose, they come with downsides, such as increased bulkiness, lack of support for multipart batch uploading, and the consumption of memory space instead of storage.

In this article, you’ll learn how to integrate file upload functionality with the Handsontable data grid seamlessly. We’ll explain the process of managing various states, including uploading, completion, and failure.

Overview of custom cell rendering and file upload functionality

Custom cells in the Handsontable data grid significantly enhance the user experience by showcasing complex content. They allow the rendering of HTML components in place of simple text-based data. In order to incorporate file upload functionality, the utilization of a file picker becomes essential. This file picker can be seamlessly integrated into data grid cells through custom cell rendering.

Live Demo

Prerequisites & required libraries

For this project, we’ll use the following packages:

We’re also going to use TailwindCSS and DaisyUI for styling, but this is optional. You may use any CSS framework of your choice.

Step-by-step implementation of file upload in Handsontable data grid

Step 1: Create a React project using tools like Vite or Create React App. It’s worth noting that using Vite is encouraged by the React community. After setting up the project, proceed to install the required libraries.

Step 2: Tailwind CSS and DaisyUI require specific configuration settings. Create the below files at the root of the project directory.

tailwind.config.js

postcss.config.js

Additionally, include these annotations at the beginning of the index.css file. Ensure that you import this CSS file within an entry script, such as index.js.

index.css

Step 3: Before we delve into creating the folder structure for our project, it’s essential to gain a clear understanding of how the final project layout will be organized.

a graphic showing employees list in a data grid

These are the components:

1. Main Heading: This represents the primary title of our application.

2. Subheading: A secondary title that provides additional context or information.

3. Data Grid: The central component of our project, comprising four columns.

4. File Upload Column: The fourth column, designed exclusively for uploading files. We’re limiting uploads to PNG images.

5. Column States: The last column has multiple states:

6. File Browser: When no image is set for a row, a file browser will be displayed.

7. Loader with Preview: The uploading state triggers a loader along with a preview image.

8. Image Display: Successful uploads result in the rendering of the uploaded image.

9. Fake Upload Component: To simulate the asynchronous nature of file uploads without a live server, we’ve created a fake upload component. This component mimics the upload process. However, we’ve also implemented actual server upload functionality for a comprehensive demo experience.

Step 4: Now let’s understand the folder structure:

constants.js — This file houses constants utilized for strings such as headings, subheadings, and file states, among others.

FakeUpload.js — It contains the logic responsible for both file upload functionality and the generation of image previews.

ImageColumnRenderer.js — In this script, you’ll find functions designed to generate HTML representations corresponding to various states within the file upload process. For instance, it creates a file picker for the NOT_AVAILABLE state, a progress bar for the UPLOADING state, and an image display for the UPLOADED state.

TableData.js — Data to display in the table like Employee name, Phone, Role etc.

App.js — Displaying Handsontable data grid.

Step 5: Let’s write the code:

constants.js

FakeUpload.js

ImageColumnRenderer.js

TableData.js

App.css

App.js

Conclusion

We’ve come to the end of this hands-on guide. Along the way, you’ve learned how custom cell rendering is key to showing complex content in a Handsontable data grid. We implemented this knowledge to create a convenient way for directly uploading images into table cells. This finds practical use in scenarios such as maintaining employee records complete with their photographs, or archiving invoices for tax and auditing purposes.

You might also be interested in our article demonstrating how to integrate React Hook Form with the React data grid. Take a look!