2

I have added an editable react-table (https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/kitchen-sink) to my project and everything works fine. But when I added a column with checkboxes, and tick the checkboxes and go to a different page (or sort or search) and come back, ticks are gone. This is how I have added the checkbox to the 'columns' field,

{
   Header: 'On Leave',
   accessor: 'onLeave',
   Filter: SelectColumnFilter,
   filter: 'includes',
   disableGroupBy: true,
   Cell: row => { return(
            <div style={{'text-align':'center'}}>
              <input type="checkbox" 
                value={row.value == "Yes" ? "on" : "off"} 
                onBlur={(event) => updateMyData(parseInt(row.row.id), row.column.id, event.target.checked ? "Yes" : "No")}  />
            </div>)},
}

updateMyData() is fired when focus is lost from checkbox and console.log prints correct data,

0 : 0 : onLeave : Yes
1 : 1 : onLeave : Yes
2 : 2 : onLeave : Yes
3 : 3 : onLeave : Yes
4 : 4 : onLeave : Yes

updateMyData() is as follows,

// When our cell renderer calls updateMyData, we'll use
// the rowIndex, columnId and new value to update the
// original data

const updateMyData = (rowIndex, columnId, value) => {


    // We also turn on the flag to not reset the page
    skipResetRef.current = true
    setData(old =>
      old.map((row, index) => {
        if (index === rowIndex) { console.log(index + " : " +  rowIndex + " : " + columnId + " : " + value););
          return {
            ...row,
            [columnId]: value,
          }
        }
        return row
      })
    )

}

Why is the checkbox values not saved in 'data' field? Thanks

1 Answer 1

1

Problem was with using checkbox 'value' attribute. Instead of that, using 'defaultChecked' solved the problem,

Cell: row => {
  return(
    <div style={{'text-align':'center'}}>
      <input type="checkbox" 
        defaultChecked={row.value == "Yes" ? true : false} 
        onBlur={(event) => updateMyData(parseInt(row.row.id), row.column.id, event.target.checked ? "Yes" : "No")}  />
    </div>)}

This question has more details, How to set default Checked in checkbox ReactJS?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.