How to set checked on item in DetailsList control
Asked Answered
B

4

6

I use DetailsList component from office-ui-fabric-react library:

   import {DetailsList} from 'office-ui-fabric-react/lib/DetailsList';
   render () {
      const item = [
        {value: 'one'},
        {value: 'two'}
      ]
      return (
        <DetailsList
            checkboxVisibility={CheckboxVisibility.always}
            items={items}
            selection={selection}
        />
    }

How to set checked for item with value `two?

Bladderwort answered 7/4, 2017 at 14:44 Comment(3)
is this resolved now? I need this too...and I found a useful link github.com/OfficeDev/office-ui-fabric-react/issues/1741 but I still didn't solved my problem.Betteanne
Look at an example codepen.io/jasongore/pen/mdbPExxJeminah
@AndrejKirejeŭ the example above works thanks, but somehow despite no trace of setting of setSelection and setSelectionDetails hooks (in class components?) [scratching my head]Adverbial
D
4

Noticed you passed a selection to DetailsList. There's a few methods in selection to do that, including:

  • setAllSelected(isAllSelected: boolean)
  • setKeySelected(key: string, isSelected: boolean, shouldAnchor: boolean)
  • setIndexSelected(index: number, isSelected: boolean, shouldAnchor: boolean)

In your case, you can give each value a key. And then call setKeySelected somewhere (for example, componentDidMount) to tell DetailsList to select specific items.

Deist answered 20/5, 2017 at 12:28 Comment(2)
what's key? is it a json object {value: 'one'} or as you said, we should assign a key somewhere meaning items should be a dict instead of list?Pompadour
You need to give each value a key, for example. { key: 'some_key', value: 'some_value' }. You can also customize the way to get the key with IDetailsListProps.getKey.Deist
H
2

I looked everywhere and could never find the answer. To set selected items basically on page load do the following:

<DetailsList
    columns={columns}
    items={getItems()}
    selection={getSelectedItems()}
/>

const getItems = () => {
     const items: any[] = [];

     itemList.map((item: any, i) => {
         items.push({
             key: i.toString(),
             Name: item.Name,
             Description: item.Description
         });
     });

     return [];
};

const getSelectedItems = (): ISelection => {
    const selection: Selection = new Selection();
    const items = getItems();

    selection.setItems(items);
    selectedItemsList.map((selectedItem: 
    any, s) => { 
         selection.setKeySelected(s.toString(), true, false);
    });

    return selection;
};
Hostelry answered 22/4, 2020 at 21:50 Comment(1)
FYI - selection.setItems(items); needs to be called BEFORE the selection.setKeySelected( ). This resolved this issue for me, which i discovered by accident / trial-and-error.Amerigo
Z
1

Simply call selection.setAllSelected(true) inside useEffect if you want all the list items to be selected by default.

const selection = new Selection();
useEffect(() => {
   selection.setAllSelected(true)
}, [selection]);
Zollverein answered 1/9, 2021 at 6:44 Comment(0)
D
0

React Typescript (tsx) Sample.

import { DetailsList, ISelection, Selection } from '@fluentui/react/lib/DetailsList';

// ...

export const Component = () => {

    const getSelectionDetails = (): string => {
      const selectionCount = selection.getSelection().length;
      return selectionCount
        ? `${selectionCount} items selected: ${selection.getSelection().map( (i: any) => i.key ).join('; ')}`
        : 'No items selected';
    }
    
    const createSelection = () => {
      return new Selection({
        onSelectionChanged: () => setSelectionDetails(getSelectionDetails()),
        getKey: (item: any) => item.key
      });
    };
      
    const [selection, setSelection] = React.useState(createSelection());
    const [selectionDetails, setSelectionDetails] = React.useState('');
    
    return <>
        <DetailsList
            // ...
            selection={selection}
            selectionMode={SelectionMode.multiple}
            // ...
        />
    </>;
}
Dedication answered 30/12, 2021 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.