Don't select checkbox when open context menu

Hi Syncfusion team

I am using the checkbox and context menu in the Grid component. When I open the context menu, the checkbox is also selected. I want to disable this feature (the checkbox is only selected when I click it).

How can I do that?


Thanks
Regards,
Hung Ton

10 Replies 1 reply marked as answer

RR Rajapandi Ravi Syncfusion Team March 10, 2022 04:15 PM UTC

Hi Ton, 

Greetings from Syncfusion support 

You can resolve this problem by cancelling the selection action on context menu open as explained below, 

Initially override the Grid context menu module’s beforeOpen event where a global flag variable is enabled to identify this case and then the default source action is executed. 

constructor() { 
    super(...arguments); 
    this.preventSelect = false; 
} 
 
// Grid’s created event handler 
onCreated() { 
    // The Grid context menu’s beforeOpen event is stored in a variable 
    var contextMenuOpenFn = this.gridInstance.contextMenuModule.contextMenu.beforeOpen; 
    // Root application instance is stored in a variable 
    var curInstance = this; 
    // The Grid context menu’s beforeOpen event is overridden 
    this.gridInstance.contextMenuModule.contextMenu.beforeOpen = function (e) { 
        // The global flag variable is enabled here to identify this case 
        // Since the context menu instance will be available here, we are accessing the global flag from the application root instance stored in the variable 
        curInstance.preventSelect = true; 
        // The default source beforeOpen method(stored in the variable) is executed 
        contextMenuOpenFn.call(this, e) 
    } 
} 

Now in the Grid’s rowSelecting event(triggers before a row is selected), cancel the action if the global flag is enabled by setting the ‘cancel’ event arguments as ‘true’

// Grid’s rowSelecting event handler 
rowSelecting(args) { 
    if (this.preventSelect) { 
        // Condition executes if the global flag is enabled 
        // The selection action is prevented 
        args.cancel = true; 
        // The global flag is disabled here so that consecutive checkbox selection action is not prevented 
        this.preventSelect = false; 
    } 
} 

We have prepared a sample based on the above queries for your reference. Please find it below, 



Let us know if you have any concerns. 

Regards, 
Rajapandi R 


Marked as answer

TT Ton That Hung March 11, 2022 06:53 AM UTC

Thanks for your solution.

Regards, 
Hung Ton


RR Rajapandi Ravi Syncfusion Team March 14, 2022 05:05 AM UTC

Hi Ton, 

We are happy to hear that the provided solution works fine at our end. 

Please get back to us if you need further assistance. 

Rajapandi R 



TT Ton That Hung April 7, 2022 02:42 AM UTC

Hi Rajapandi Ravi,

When I use your solution and Hierarchy feature, it works in React v17 but doesn't work (checkbox is still checked when open context menu) in React v18.

Sample React v17: https://stackblitz.com/edit/react-uyjgxh-iabsdr?file=index.js
Sample React v18: https://stackblitz.com/edit/react-e8j9t4-9zkulc?file=index.js

How can I fix it?

Thanks



JC Joseph Christ Nithin Issack Syncfusion Team April 7, 2022 04:40 PM UTC

Currently we are validating your query, we will provide further details on or before April 11th 2022. We appreciate your patience until then.



TT Ton That Hung replied to Joseph Christ Nithin Issack April 8, 2022 04:05 AM UTC

Thank you.

I will wait for your solution.



JC Joseph Christ Nithin Issack Syncfusion Team April 8, 2022 12:44 PM UTC

Thanks for the update. We will provide further details as promised.



RR Rajapandi Ravi Syncfusion Team April 11, 2022 02:27 PM UTC

Hi Ton,


Thanks for your patience


We have checked your shared problem and we found that the problem was not occurs based on the React V17/18. The problem comes from the Grid older package version, in your shared React v18 sample the Grid has 19.2.62 older version. In React v17 sample the Grid has the 19.4.54 newer version. So, we would like to inform you that the problem was not related to the React version and it was related to the Syncfusion Grid package version.


To resolve the problem, we suggest you use the latest package version and achieve your requirement. Please refer the below modified React v18 sample for more information.


Sample: https://stackblitz.com/edit/react-e8j9t4-m6g7b4?file=package.json


Screenshot:



Regards,

Rajapandi R



TT Ton That Hung April 12, 2022 01:29 AM UTC

Thanks for your support.



RR Rajapandi Ravi Syncfusion Team April 12, 2022 05:39 AM UTC

Hi Ton,


We are happy to hear that the provided solution works fine at our end.


Please get back to us if you need further assistance.


Regards,

Rajapandi R


Loader.
Up arrow icon