How to make the cell clickable?

I am using reactJS and want to make the special cell of the grid clickable and change the component when the cell clicked.

origin grid:


when the user clicks the customer name e.g. (Maria), the page will change to 



17 Replies

AG Ajith Govarthan Syncfusion Team April 6, 2020 12:45 PM UTC

Hi Customer, 

Thanks for contacting Syncfusion support. 

Query: “want to make the special cell of the grid clickable and change the component when the cell clicked 
 
From the above query we suspect that you want to bind the click event for the cell elements in EJ2 Grid.  

In this you also mentioned that you need to change the component when the cell is clicked. So, we are unclear about this requirement. Based on your attached screenshots we need to clarify the below details 

1. In first screenshot you have binded the Grid with checkbox column and also some field names. When we click on the any of the cell from the Customer Name field you need to change grid as like second screenshot. 

2. Based on your second screenshot we suspect that you need to change the dataSource for the grid when click on the cell or do you need bind another grid and change the dataSource for the grid based on the cell selection. 

3. In EJ2 Grid we have master-detail support and for your convenience we have attached the documentation. So, please confirm with the documentation and the confirm this is the requirement which is expecting in your application. 


Regards, 
Ajith G. 



DE developer April 7, 2020 01:36 AM UTC

Thank you for your reply, But my case is want to click the cell (Customer Name) only not the whole row select

can you provide source code or sample how to add a click event only the cell of customer name?

after clicking the Cell (customer name), the grid 1 will disappear (store the customer name)  and then another grid will appear (show the customer name).


AG Ajith Govarthan Syncfusion Team April 7, 2020 04:31 PM UTC

Hi Customer, 

For the mentioned query we have prepared sample and in that sample we have rendered two grids in a single page . The first grid is rendered initially and the second grid is hidden state. 

For the first grid we have enabled cell Selection and when you click a cell which will trigger cellSelecting event using that we have hided and showed the both grids.  

Using the getColumnbyIndex method we have taken the field of the column and  using that we have taken the data of the clicked cell from the data passed in the arguments. We have attached the sample so please refer the sample for your reference. 

Code Snippet: 
Index.js 

cellSelecting(args){ 
      var column = this.gridInstance.getColumnByIndex(args.cellIndex.cellIndex).field; 
      if(  column === "CustomerName"){ 
         document.getElementById('clksrc').innerHTML = args.data[column]; 
        this.gridInstance.element.style.display = "none"; 
        this.gridInstance2.element.style.display = ""; 
      } 
    } 

    load(args){ 
      this.gridInstance2.element.style.display = "none"; 
    } 



Please get back to us if you need further assistance. 

Regards, 
Ajith G 



DE developer April 8, 2020 04:32 AM UTC

Thank you for providing a good answer to me, but I still have two question.

1: Because my grid includes a checkbox for the user to select the row. When I change the selectionSetting to "Single" and mode to "Cell", how to make the checkbox still can select the row?
  const selectionSettings = { type: "Single"mode: "Cell" };

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2: 
Can I change the Grid using "useState"? Because when I use the "useState" to change the Grid, the console shows the error message 



------------------------------------------------------------------------------------------------------------------------------------------------------------------------
My Code Example:

The Grid2 is another JSX file for import

Grid2:
import * as React from "react";
import { ButtonComponent } from "@syncfusion/ej2-react-buttons";
import { TextBoxComponent } from "@syncfusion/ej2-react-inputs";
import * as moment from "moment";
import {
  GridComponent,
  ColumnsDirective,
  ColumnDirective,
  Inject,
  Page,
  Edit,
  Toolbar,
  CommandColumn,
from "@syncfusion/ej2-react-grids";
import { dataSource from "./dataSource";

const Grid2 = (=> {

  const gridRef = React.createRef();

  const toolbarOptions = ["Add""Delete""Update""Cancel"];
  const editSettings = {
    allowEditing: true,
    allowAdding: true,
    allowDeleting: true,
    newRowPosition: "Top",
  };

  const commands = [
    { type: "Edit"buttonOption: { iconCss: " e-icons e-edit"cssClass: "e-flat" } }
  ];

  const actionBegin = args => {
  };

  return (

      <div className="grid2-class">
        <GridComponent
          id="grid2"
          dataSource={dataSource}
          toolbar={toolbarOptions}
          editSettings={editSettings}
          actionBegin={actionBegin.bind(this)}
          ref={gridRef}
        >
          <ColumnsDirective>
            <ColumnDirective
              type="checkbox"
              allowSorting={false}
              allowFiltering={false}
              width="60"
            />

            <ColumnDirective field="field1" HeaderText="headerText1" />
              <ColumnDirective field="field2" HeaderText="headerText2" />
               <ColumnDirective field="field3" HeaderText="headerText3" />

          ColumnsDirective>
          <Inject services={[PageCommandColumnToolbarEdit]} />
        GridComponent>
    div>
  );
};

export default Grid2;


Main JSX: 
import Grid2 from "./Grid2"; // To import the Grid 2 component

  const gridRef = React.createRef();

const [gridDetailsetGridDetail] = useState({
    showGrid2: false,
    data: {},
  });

  const cellSelecting = args => {
    const column = gridRef.current.getColumnByIndex(args.cellIndex.cellIndex).field;
    if (column === "CustomerName") {
      setGridDetail({
        showGrid2: true, // change the state to show the grid 2
      });
    }
  };


  return (
    <div>
      {gridDetail.showGrid2? ( // for useState to change the Grid2 Component
        <div>
          <Grid2 />
        div>
      ) : (
        <div>
          <GridComponent
               // .... another grid component setting
               ref={gridRef}
               cellSelecting={cellSelecting.bind(this)}
          >
             // Grid 1 info, I am not typing too much
GridComponent>
        div>
      )}
div>
);



AG Ajith Govarthan Syncfusion Team April 9, 2020 04:00 AM UTC

Hi Customer, 
  
Sorry for the inconveniences. 
  
Query: my grid includes a checkbox for the user to select the row. When I change the selectionSetting to "Single" and mode to "Cell", how to make the checkbox still can select the row? 
  
Based on the query we have prepared sample and in that sample we have rendered two grids in a single page . And the first grid have checkbox column so when click checkbox which will select whole row so we have set selection mode as Both so which will be used to select cell and also row using the checkbox mode. And now you can show and hide the second grid based on the corresponding column click as mentioned in the previous update. We have shared the sample for your reference. 
  
  
Query: Can I change the Grid using "useState"? Because when I use the "useState" to change the Grid, the console shows the error message 
  
From the attached code snippet we are not able to validate the issue properly so please kindly share the below details to validate further on your requirement. 
  
  1. Share the complete grid code sample.
  2. Share video demonstration of the reported issue.
  3. Share the Syncfusion package version.
  
Regards, 
Ajith G 
  



DE developer April 9, 2020 07:17 AM UTC

Hi Ajith,

Thank you for your answer 1, It works.
-----------------------------------------------------------------

About question 2, I am entering the sample in the link: https://stackblitz.com/edit/react-jfzffk?file=index.js

// I want to change the state when cellSelecting (showGrid2 set to true)
 const cellSelecting = args => {
    var column = this.gridInstance.getColumnByIndex(args.cellIndex.cellIndex)
      .field;
    if (column === "CustomerName") {
      setGridDetail({
        showGrid2: true,
        data: args.data
      });
    }
  };


// Using the showGrid2 value to contol which one should be showing
{!gridDetail.showGrid2 ?
          <GridComponent
            dataSource={orderDataSource}
            ref={gridRef}
            toolbar={toolbarOptions}
            allowPaging={true}
            cellSelecting={cellSelecting.bind(this)}
            editSettings={editSettings}
            actionBegin={actionBegin.bind(this)}
            selectionSettings={selectionSettings}
          >
            <ColumnsDirective>
              <ColumnDirective headerText="check" type="checkbox" width="60" />
              <ColumnDirective
                field="OrderID"
                headerText="Order ID"
                width="140"
                textAlign="Right"
                validationRules={this.orderidRules}
                isPrimaryKey={true}
              />
              <ColumnDirective
                field="CustomerName"
                headerText="Customer Name"
                validationRules={this.validationRule}
              />
              <ColumnDirective
                field="Freight"
                headerText="Freight"
                width="140"
                format="C2"
                textAlign="Right"
                editType="numericedit"
              />
            </ColumnsDirective>
            <Inject services={[Page, Toolbar, Edit, Resize]} />
          </GridComponent>
          :
          <Grid2 />
        }

And when I click the cell to change the grid, the error message will be shown



BS Balaji Sekar Syncfusion Team April 10, 2020 01:32 PM UTC

  
Hi Customer, 

Query: Can I change the Grid using "useState"? Because when I use the "useState" to change the Grid, the console shows the error message  

We have validated your query and we found the cause of the problem as ref property is not defined properly in the Grid wrapper. We have modified the following code that has resolve the reported script error. 

[index.js]  

<GridComponent 
                dataSource={orderDataSource} 
                ref={g=>this.gridInstance=g} 
                toolbar={toolbarOptions} 
                allowPaging={true} 
                cellSelecting={cellSelecting.bind(this)} 
                editSettings={editSettings} 
                actionBegin={actionBegin.bind(this)} 
                selectionSettings={selectionSettings} 
              > 
.     .     .    


But we have facing another one issue while the useState method update. useState method is not reflecting change immediately in the cellSelection. Already this issue reported in public forums.  

For your reference, we have shared that forums link that will help to you. 


Regards, 
Balaji Sekar. 




DE developer April 10, 2020 02:31 PM UTC

Thank you for your reply, So I cannot use "useState" to change the grid component until the problem solve?


AG Ajith Govarthan Syncfusion Team April 14, 2020 02:34 AM UTC

Hi Customer, 

Sorry for the inconveniences. 

From the Previous update we mentioned that we are facing the issue while the useState method update that is useState method is not reflecting change immediately in the cellSelection. So until the issue fix at the react side you can’t able to use the useState to show and hide the grid dynamically. 
 
Please get back to us if you need further assistance. 

Regards, 
Ajith G 



DE developer April 14, 2020 02:36 AM UTC

Thank you so much


AG Ajith Govarthan Syncfusion Team April 14, 2020 10:54 AM UTC

Hi Customer, 

Thanks for the update. 

Please get back to us if you need any further assistance. 

Regards, 

Ajith G. 



LE Leela October 12, 2022 03:54 PM UTC

Hi Team, 

For me cellSelecting function is not working. I am using cellSelecting() in React functional component with versions mentioned below. I need to get cell data when I click on cell. I tried with syncfusion/ej2: 17.4.55 version also but didn't work out.


 "react": "^16.12.0",
 "@syncfusion/ej2": "17.4.43",


JC Joseph Christ Nithin Issack Syncfusion Team October 13, 2022 03:48 PM UTC

Hi Leela,


  Thanks for your update.


  Based on the provided information, we suspect that you are trying to select a cell using the `cellSelecting()`, but `cellSelecting` is an event that will be triggered before selecting cell, it cannot be used for selecting a cell. We suggest you to use the `selectCell` method of the EJ2 Grid, using which you can select the required cell.


Please refer the below code example.


 

onDataBound() {

    if (this.initialRender) {

      this.initialRender = false;

      this.gridInstance.selectCell({ rowIndex: 0, cellIndex: 2 });

    }

  }

 


Sample: https://stackblitz.com/edit/react-rcnjqb-st8sag?file=index.js


Please get back to us for further details.


Regards,

Joseph I.



LE Leela October 20, 2022 05:04 AM UTC

Hi,


Thanks for your reply.

My requirement is when ever I click on checkbox it should selects row but it shouldn't select row when we do row clicking. And I need to show update popup when we click on unique id cell. In which way we can achieve it.


Regards,

Leela



JC Joseph Christ Nithin Issack Syncfusion Team October 27, 2022 05:30 PM UTC

Hi Leela,


   Thanks for your update.


   Query 1: Whenever I click on checkbox it should selects row but it shouldn't select row when we do row clicking.


          Based on your requirement, you want to select only using the checkbox, when you click on the row you do not want to select row. We have achieved your requirement, by using the `rowSelecting` event of the EJ2 Grid.


    Please refer the below code example.


 

 

 rowSelecting(args) {

    if (!args.target.parentElement.classList.contains('e-checkbox-wrapper')) {

      args.cancel = true;

    }

  }

 


Sample: https://stackblitz.com/edit/react-rcnjqb-wv8ugm?file=index.js


Query 2: Need to show update popup when we click on unique id cell.


   Before proceeding to the solution, we would like you to explain your requirement in detail. You have mentioned that when you click on the unique id cell, you want to show update popup, do you want to show the edit dialog (Dialog edit mode), or do you want to show a custom popup.


Please get back to us for further details.


Regards,

Joseph I.



LE Leela October 31, 2022 05:25 AM UTC

Hi Joseph,


Thanks for your reply.

Second Query : I need to show a custom popup with prefilled data in fields and editable.


Regards

Leela



JC Joseph Christ Nithin Issack Syncfusion Team November 6, 2022 07:01 PM UTC

Hi Leela,


  We are still not clear of your requirement, kindly explain your requirement in detail and provide the below details to proceed further.


  • You have mentioned you want to show a custom popup with prefilled data in fields and editable, confirm if you want to show a custom popup on clicking the unique cell id, with the details of the row filled in the popup as edit dialog and when you save the dialog you want to update the same in the row?
  • Video demo or screenshot explaining your requirement in detail.
  • Complete grid rendering code you are using.
  • Syncfusion package version you are using.


Regards,

Joseph I.


Loader.
Up arrow icon