Skip to content

How can I select all the items in the table by different button? #638

Answered by connorlanigan
hiimnhan asked this question in Q&A
Discussion options

You must be logged in to vote

Hello!

Note that the Table already provides a checkbox in the top left that allows your users to select and unselect all items in the table.

The list of selected items is controlled by the selectedItems property, which will be controlled by your application's state. You can select all items by passing all items into that property.

For example:

const [selectedItems, setSelectedItems] = useState([]);

return <>
    <Button onClick={() => setSelectedItems(allItems)}>
          Check all items in the table
    </Button>

    <Table
         items={allItems}
         selectedItems={selectedItems}

         ...
    />
</>

You can find a running example of this in CodeSandbox.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@hiimnhan
Comment options

Answer selected by hiimnhan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants