Skip to content

Interactive Primereact Datatable #2729

Answered by qwadrox
Amit-ga asked this question in PrimeReact
Discussion options

You must be logged in to vote

@Amit-ga
Hi. The ability to add and remove rows interactively isn't explicitly mentioned in the documentation because it would be only basic React state management.

const [data, setData] = useState([
        { id: 1, name: 'Product A', price: 100 },
        { id: 2, name: 'Product B', price: 200 },
    ]);

const addRow = () => {
    const newId = data.length > 0 ? Math.max(...data.map(d => d.id)) + 1 : 1;
    // The DataTable would re-render after this and display the new data
    setData([...data, { id: newId, name: `New Product ${newId}`, price: 0 }]);
};

const removeRow = (rowData) => {
    setData(data.filter(d => d.id !== rowData.id));
};

// You can customize this further based on…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by melloware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
2 participants