0

I like to set the IsRowExpandable property to false, if no entries for the ReferenceManyField exists. How can i access the ReferenceManyField count or rows? I found a similar question, but i have no idea how can i access this total prop in this context. (Get ReferenceManyField Count?)

https://marmelab.com/react-admin/List.html#isrowexpandable

  const ExpandCardWithDatagrid = () => {
    const classes = useStyles();
    return (
      <Card className={classes.subcard} variant="outlined">
        <CardContent>
          <ReferenceManyField reference="comments" target="postId">
            <Datagrid rowClick="edit">
              <NumberField source="myField1" />
              <NumberField source="myField2" />
               ...
            </Datagrid>
          </ReferenceManyField>
        </CardContent>
        <CardActions>
          <ViewFilteredContractTypesList />
        </CardActions>
      </Card>
    )
  }

  ...

    <List
      {...props}
      bulkActionButtons={false}/>}
    >
      <Datagrid
        rowClick="edit"
        isRowExpandable={??????????}    
        expand={<ExpandCardWithDatagrid />}>
...
kdev
  • 11
  • 6

1 Answers1

0

You could have access to the total by making your own Datagrid and accessing its list context:

import { useListContext, List, Datagrid  } from 'react-admin';

const MyDatagrid = (props) => {
    const { total } = useListContext();

    return (
        <Datagrid {...props} isRowExpandable={row => total > 0}>
            <NumberField source="myField1" />
            <NumberField source="myField2" />
            //...
        </Datagrid>
    );
};

...

<List
    {...props}
    bulkActionButtons={false}
>
      <MyDatagrid
        rowClick="edit"
        expand={<ExpandCardWithDatagrid />}>
        ...

Even though, if there are no records, I don't know how this is useful, but at least is a simple example on how to access a List context props like total within its children

WiXSL
  • 689
  • 5
  • 16
  • This is not the right context. I don't need the total of the current list. I need the total of the sub-items (ExpandCardWithDatagrid: ReferenceManyField ). I have now changed my backend and add a property to the raw with the required information: e.g. has_comments and use it like in the documentation ({row => row.has_comments}) – kdev Mar 11 '22 at 16:41