React.js Blueprint is a front-end UI toolkit. It is very optimized and popular for building interfaces that are complex data-dense for desktop applications.
The table component allows the user to display rows of data. We can use the following approach in ReactJS to use the ReactJS Blueprint Table Component.
The EditableCell component on a Column enables double-click-to-edit functionality in the table body.
Table Props:
- numRows: It is used to set the number of rows.
- cellRenderer: It is used to define how data is displayed and we can set it on each column component.
EditableCell props:
- columnIndex: It specifies the column index of the cell.
- interactive: It is a boolean value. It determines whether to enable mouse interaction or not.
- isFocused: It is a boolean value. It determines whether the current cell is focused or not.
- key: A string object.
- cellRef: A ref handle for the outer div.
- loading: It is a boolean value. It determines whether it’s in a loading state or not.
- onCancel: A void callback function that gets triggered when an edit is canceled.
- onChange: A void callback function that gets triggered while editing.
- onConfirm: A void callback function that gets triggered when an edit is confirmed.
- onKeyDown: A callback function invoked when the cell is focused and a key is pressed.
- onKeyPress: A callback function invoked when a character key is pressed.
- onKeyUp: A callback function invoked when a cell is focussed and a key is released.
- rowIndex: It specifies the row index of the cell.
- style: It specifies CSS styles.
- tabIndex: It specifies the tab index of the cell.
- tooltip: An element displayed on hover.
- truncated: It is a boolean value. It determines whether the contents of the cell will be wrapped or not to prevent overflowing.
- value: It shows the value in the text box.
- wrapText: It is a boolean value. It determines whether the contents of the cell will be wrapped or not.
Syntax:
<EditableCell>...</EditableCell>
Prerequisite:
- Introduction and Installation reactJS
- React.js Blueprint Table Component
Creating React Application And Installing Module:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Step 3: After creating the ReactJS application, Install the required module using the following command:
npm install @blueprintjs/core npm install --save @blueprintjs/table
Project Structure: It will look like the following.
Example 1: we are importing { Column, Table,EditableCell } from “@blueprintjs/table”. To apply the default styles of the components we are importing “@blueprintjs/core/lib/css/blueprint.css” and “@blueprintjs/table/lib/css/table.css”.
We have used the table component to show data in the form of a table, here we have shown one Column with a header named Enter Numbers, and we have passed our custom sampleData function which returns the EditableCell component and this function is called five times as we have specified numRows={5}.
App.js
Javascript
import React from 'react' import '@blueprintjs/core/lib/css/blueprint.css' ; import '@blueprintjs/table/lib/css/table.css' ; import { Column, Table, EditableCell } from "@blueprintjs/table" ; function App() { const sampleData = () => { return <EditableCell></EditableCell> }; return ( <div style={{ margin: 20 }}> <h4> ReactJS Blueprint Table JS API EditableCell </h4> <Table numRows={5}> <Column name= " Enter Numbers" cellRenderer={sampleData} /> </Table> </div> ); } export default App; |
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Example 2: In this example, the custom sampleData function returns the EditableCell component showing different values and calls an on-confirm function through onConfirm prop named as onConfirmHandler that shows text in the alert box when we confirmed the edited text.
App.js
Javascript
import React from 'react' import '@blueprintjs/core/lib/css/blueprint.css' ; import '@blueprintjs/table/lib/css/table.css' ; import { Column, Table, EditableCell } from "@blueprintjs/table" ; function App() { const sampleData = (idx) => { return <EditableCell value={idx * 100 + 4 + idx} placeholder= "new data" onConfirm={onConfirmHandler} ></EditableCell> }; // on confirm function const onConfirmHandler = () => { alert( 'number changed !' ) } return ( <div style={{ margin: 20 }}> <h4>ReactJS Blueprint Table JS API EditableCell</h4> <Table numRows={5}> <Column name= " Numbers" cellRenderer={sampleData} /> </Table> </div> ); } export default App; |
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Reference: https://blueprintjs.com/docs/versions/1/#table-js.editablecell