Autodesk / Autodesk/react-base-table
selections not working if columns pulled from state?
- Vorherrschende Sprache
- TypeScript
- Sterne
- 1.5k
- Forks
- 170
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
In my application, I need to dynamically create the columns and then have the pull the columns from the state. However, when I do this, the code to update the row state does not work. Below is a simplified version, showing the issue. If you run this code, when you select the checkbox, the state does not get updated correctly.
If, however, I change BaseTable and set the columns= the actual array, then the selection process works.
Code that does not work
```
/* eslint-disable */
import React, { useState,useEffect } from 'react';
import BaseTable, { Column } from 'react-base-table';
import 'react-base-table/styles.css';
const MultiSelectTable = () => {
const [selectedRows, setSelectedRows] = useState([]);
const [columns, setColumns] = useState([])
useEffect(()=>{
setColumns(
[
{
key:"checkbox",
title:"",
width:50,
selectedRowKeys:{selectedRows},
cellRenderer:({ rowData }) => (
handleRowSelection({ rowData })}
/>
)}
,
{key:"name", dataKey:"name", title:"Name", width:200},
{key:"age", dataKey:"age", title:"Age", width:100}
]
)
},[])
const handleRowSelection = ({ rowData }) => {
const selectedRowKeys = [...selectedRows];
const rowIndex = selectedRowKeys.indexOf(rowData.id);
if (rowIndex !== -1) {
selectedRowKeys.splice(rowIndex, 1);
} else {
selectedRowKeys.push(rowData.id);
}
setSelectedRows(selectedRowKeys);
console.log(selectedRowKeys)
};
const data = [
{ id: 1, name: 'John Doe', age: 25 },
{ id: 2, name: 'Jane Smith', age: 30 },
{ id: 3, name: 'Bob Johnson', age: 35 },
// Add more data as needed
];
return (
);
};
export default MultiSelectTable;
```
Code that works:
```
/* eslint-disable */
import React, { useState,useEffect } from 'react';
import BaseTable, { Column } from 'react-base-table';
import 'react-base-table/styles.css';
const MultiSelectTable = () => {
const [selectedRows, setSelectedRows] = useState([]);
const handleRowSelection = ({ rowData }) => {
const selectedRowKeys = [...selectedRows];
const rowIndex = selectedRowKeys.indexOf(rowData.id);
if (rowIndex !== -1) {
selectedRowKeys.splice(rowIndex, 1);
} else {
selectedRowKeys.push(rowData.id);
}
setSelectedRows(selectedRowKeys);
console.log(selectedRowKeys)
};
const columns= [
{
key:"checkbox",
title:"",
width:50,
selectedRowKeys:{selectedRows},
cellRenderer:({ rowData }) => (
handleRowSelection({ rowData })}
/>
)}
,
{key:"name", dataKey:"name", title:"Name", width:200},
{key:"age", dataKey:"age", title:"Age", width:100}
]
const data = [
{ id: 1, name: 'John Doe', age: 25 },
{ id: 2, name: 'Jane Smith', age: 30 },
{ id: 3, name: 'Bob Johnson', age: 35 },
// Add more data as needed
];
return (
);
};
export default MultiSelectTable;
```
How can I load the columsn dynamically into state but still have the selection of rows work?
Beitragsleitfaden
Rechercherichtung
Das bereitgestellte MultiSelectTable-Beispiel reproduzieren und dabei die in useEffect erstellten columns mit dem inline an BaseTable übergebenen columns-Array vergleichen. Nachverfolgen, wie die zustandsbasierten columns und cellRenderer selectedRows beobachten, und anschließend überprüfen, dass die Auswahl eines Kontrollkästchens sowohl selectedRows als auch den gerenderten Zustand des Kontrollkästchens aktualisiert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, react
- Bereich
- frontend
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100