Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptReactJS Blueprint Table Component

ReactJS Blueprint Table Component

BlueprintJS is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex data-dense for desktop applications. 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.

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.  

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.

Project Structure

Example 1: Now write down the following code in the App.js file. Here, we have shown a single column inside a table component.

App.js




import React from 'react'
import '@blueprintjs/core/lib/css/blueprint.css';
import '@blueprintjs/table/lib/css/table.css';
import { Column, Cell, Table } from "@blueprintjs/table";
  
function App() {
  
    // Sample Column data
    const sampleColumn = (index) => {
        return <Cell> CellNo{index}</Cell>
    };
  
    return (
        <div style={{ display: 'block'
                      width: 300, 
                      padding: 30 }}>
            <h4>ReactJS Blueprint Table Component</h4>
            <Table numRows={5}>
                <Column name="Numbers" 
                        cellRenderer={sampleColumn} />
            </Table>
        </div>
    );
}
  
export default App;


Explanation: 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 Numbers, and we have passed our custom sampleColumn function which returns a single cell to show sample text CellNo and this function is called five times as we have specified numRows={5}.

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: Now write down the following code in the App.js file. Here, we have shown multiple columns inside a table component.

App.js




import React from 'react'
import '@blueprintjs/core/lib/css/blueprint.css';
import '@blueprintjs/table/lib/css/table.css';
import { Column, Cell, Table } from "@blueprintjs/table";
  
function App() {
  
    // Sample Column One data
    const sampleColumnOne = (index) => {
        return <Cell> CellNo{index}</Cell>
    };
  
    // Sample Column two data
    const sampleColumnTwo = (index) => {
        return <Cell> User{index}</Cell>
    };
  
    return (
        <div style={{ display: 'block',
                      width: 390,
                      padding: 30 }}>
            <h4>ReactJS Blueprint Table Component</h4>
            <Table numRows={5}>
                <Column name="Numbers" 
                        cellRenderer={sampleColumnOne} />
                <Column name="User Name" 
                        cellRenderer={sampleColumnTwo} />
            </Table>
        </div>
    );
}
  
export default App;


Explanation: We have used the table component to show data in the form of a table, here we have shown two Columns with their header named Numbers and UserName respectively. We have passed our custom sampleColumnOne and sampleColumnTwo function which returns a single cell to show sample text CellNo and UserNo respectively and these functions are called five times as we have specified numRows={5}.

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/#table

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments