Need a special offer?Find out if your project fits.
+

Add checkbox as column in react

Answered
vasantjk asked on February 3, 2022

Hi Team
Hi Team, I want to add checkbox as a column (the same requirement as here) how do we implement in react with functional component.
I am not able to get the output for it. And need to make the checkbox have check based on condition like 
if(id<10)
check 
else 
uncheck 
if the id is greater than 10 then check box has to be checked . needed this requirement in react with functional component.
Can you help me resolve it.

Maksym Diachenko created this ticket from #44099

6 answers

Public
Maksym Diachenko Maksym Diachenko Flexmonster February 4, 2022

Hello!

Thank you for reaching out to us.

We recommend you the following approach for this task:
1. Create an additional ID property, which will be a container for a checkbox:

{
ID: 11, /*ID value for checkbox*/
City: "Kyiv",
Sales: 1000,
}

2. Add the customizeCell function to add checkboxes. We recommend creating a separate class for these checkboxes to retrieve data from them easily: 

const customizeCellFunction = (cell, data) => {
if (data.type !== "header" && data.measure.uniqueName === "ID") {
cell.style["text-align"] = "center";
cell.style["z-index"] = 2; /*z-index is needed to make checkboxes clickable*/
cell.text = `<input type="checkbox" class="myinput" ${data.value > 10 ? 'checked' : ''}>`
}
}

3. Add this function as a property of React component.

customizeCell={customizeCellFunction}

4. To get the checked property from checkboxes, you can use the getElementsByClassName function. 

Please check the attached archive with a React application to see the working example. To run the project, open the terminal from a root folder and type:

npm install
npm start

This sample also dynamically displays indexes of checked boxes. You can see the implementation of this logic in showIndexesOfChecked and addListeners functions.

We hope that you will find this answer helpful. Feel free to contact us if any other questions arise.

Best Regards,
Maksym

Attachments:
react-example.zip

Public
vasantjk February 4, 2022

Thanks for Answering my queries.
How do i add function to this input like onchange and get the data of the checkbox.
Can you help me out with this query.

Public
vasantjk February 7, 2022

Hi team, I am having another requirement .
And I want to like compare the other column values and make the check box checked or unchecked.
{
ID: 11,/*ID value for checkbox*/
City: "Kyiv",
Sales: 1000
}
for the checkbox condition i have to check ID and Sales if the id is>11 and sales values is >1000
i have to make the checkbox checked in the ID column.
is it possible to get the values of the other column and apply the condition for the checkbox field

Attachments:
checkbox.png

Public
Maksym Diachenko Maksym Diachenko Flexmonster February 7, 2022

Hello!
 
Thank you for your questions.
 
1. We recommend adding event listeners to handle changes in checkboxes. Kindly note the example below: 

const addListeners = () => {
   var elements = document.getElementsByClassName("myinput");
   for (var i = 0; i < elements.length; i++) {
       elements[i].addEventListener('change', changeEventHandler);
}
}

To get the data, you can use the similar code, where "myinput" is the class, specified while adding checkboxes in customizeCell function:

let checkboxes = document.getElementsByClassName("myinput");

2. The customizeCell function works with each particular cell separately. If you need to get another column's value, you can directly access data from the report and find the object with the corresponding ID value.
Please refer to this modified function from the sample project we have sent you earlier:

let dataJson = getReport().dataSource.data;

const customizeCellFunction = (cell, data) => {
        if (data.type !== "header" && data.measure.uniqueName === "ID") {
            /* CODE */
            for (var i = 0; i < dataJson.length; i++) {
                if (dataJson[i].ID === data.value) {
                    var checked = data.value > 10 && dataJson[i].Sales > 1000 ? 'checked' : '';
break;
}
}
            cell.text = `<input type="checkbox" class="myinput" ${checked}>`
}
}

dataJSON is the data, which is used in your report.
 
Hope you will find this answer helpful.
 
Best Regards,
Maksym
 
 

Public
vasantjk February 7, 2022

Thanks for your help

Public
vasantjk February 8, 2022

Hi team how do we handle data which is in this format 
[{
ID:1,
activestatus:true,
name:["john","mayars"]
},
{
ID:2,
activestatus:false,
name:["john","cena"]
},
{
ID:1,
activestatus:true,
name:["john","wick"]
}
]
when the data is in this format the table is not showing the values in the table how to handle this kind of format

Maksym Diachenko created a new ticket #44169 from this answer

Please login or Register to Submit Answer