javascriptreactjsgridag-gridag-grid-react

ag-grid react to display boolean value instead of checkbox


In AG-Grid-react, If API response has boolean value say true or false, then by default ag-grid-react shows checkbox. IS there a property in ag-grid-react to display value as true or false in column and not checkbox.

Can anyone help if there is any property to achieve this


Solution

  • You can define the property cellDataType and set it to string, so that you do not get the checkbox but the actual value!

    import { ColDef } from "ag-grid-community";
    import "ag-grid-community/styles/ag-grid.css";
    import "ag-grid-community/styles/ag-theme-quartz.css";
    import { AgGridReact } from "ag-grid-react";
    import React, { useState } from "react";
    import { createRoot } from "react-dom/client";
    
    // Row Data Interface
    interface IRow {
      make: string;
      model: string;
      price: number;
      electric: boolean;
    }
    
    // Create new GridExample component
    const GridExample = () => {
      // Row Data: The data to be displayed.
      const [rowData, setRowData] = useState<IRow[]>([
        { make: "Tesla", model: "Model Y", price: 64950, electric: true },
        { make: "Ford", model: "F-Series", price: 33850, electric: false },
        { make: "Toyota", model: "Corolla", price: 29600, electric: false },
      ]);
    
      // Column Definitions: Defines & controls grid columns.
      const [colDefs, setColDefs] = useState<ColDef<IRow>[]>([
        { field: "make" },
        { field: "model" },
        { field: "price" },
        { field: "electric", cellDataType: "string" },
      ]);
    
      // Container: Defines the grid's theme & dimensions.
      return (
        <div
          className={"ag-theme-quartz"}
          style={{ width: "100%", height: "100%" }}
        >
          <AgGridReact rowData={rowData} columnDefs={colDefs} />
        </div>
      );
    };
    
    // Render GridExample
    const root = createRoot(document.getElementById("root")!);
    root.render(<GridExample />);
    

    code sandbox