1

I have a table "fos_user" and "version", I would like to display "9.0.1.A" instead of "/ version / 1".

=>fos_user https://i.stack.imgur.com/8z9LG.png

=>version https://i.stack.imgur.com/vCu9v.png

Is it possible to concatenate the fields of the "version" table to display it in the source, like this? https://i.stack.imgur.com/iKsWQ.png

Because it will prevent me from creating a new field in the "version" table.

Best regards,

Peacefull
  • 546
  • 6
  • 24

3 Answers3

3

React-admin | How to concat source inside ReferenceField ?

Answer:

<ReferenceField label="Version" source="version" reference="versions"> <FunctionField render={version => `${version.major}.${version.minor}.${version.patch}.${version.letter}`} /> </ReferenceField>

Peacefull
  • 546
  • 6
  • 24
1

I needed a prefix for images. Unfortunatelly the provided solution didn't work for me... At first I tried

<ImageField source="prefix + contentUrl"/>

and the solution above...

Eventually I found following solution:

const MyImageField = ({source, record = {}, ...rest}) => {
    let clonedRecord = JSON.parse(JSON.stringify(record));
    clonedRecord[source] = process.env.REACT_APP_BASE_PATH + clonedRecord[source];
    return <ImageField source={source} record={clonedRecord} {...rest}/>;
};

export const ImageList = props => (
    <List {...props}>
        <Datagrid rowClick="edit">
            <MyImageField source="contentUrl"></MyImageField>
        </Datagrid>
    </List>
);

const dataProvider = baseHydraDataProvider(entrypoint, fetchHydra, apiDocumentationParser);

export default () => (
    <HydraAdmin dataProvider={dataProvider} authProvider={authProvider} entrypoint={entrypoint}>   
        <ResourceGuesser name={"media_objects"} list={ImageList}/>
    </HydraAdmin>
);

Somehow i needed a clone, because the function was called multiple times (-> prefix was added x times).

I use this in combination with the API Platform.

Also see:

https://marmelab.com/react-admin/Fields.html#writing-your-own-field-component

Lord_Fry
  • 83
  • 1
  • 5
0

From https://marmelab.com/react-admin/Fields.html#reference-fields

Tip: If you want to display data from more than one field, check out the <FunctionField>, which accepts a render function:

import { FunctionField } from 'react-admin';

<FunctionField
    label="Name"
    render={record => `${record.first_name} ${record.last_name}`}
/>;

So for your case, it would be:

import { FunctionField } from 'react-admin';

<FunctionField
    label="Name"
    render={version => `${version.major}.${version.minor}.${version.patch}.${version.letter}`}
/>;
luoenzhen
  • 29
  • 1