2

I want to get unique data from db in the drop-down menu as i have two drop-down menus and both are depended on each other for example if i select something in one drop-down then it will show so data related to that in the other drop-down. `

<FormDataConsumer>

  {({ formData, dispatch, ...rest }) => (

    <Fragment>

      <ReferenceInput basePath={props.basePath} validate={requiredValidator} source="cc_documents_section_id" reference="documents-section" label="Document Type">
       <SelectInput optionText="cc_document_type" {...rest} />
     </ReferenceInput>

     <ReferenceInput source="cc_documents_section_id" reference="documents-section" validate={requiredValidator} label="Document Name">
        <SelectInput optionText="cc_document_name"
                            onChange={value => dispatch(
                            change(REDUX_FORM_NAME,'cc_document_type', null)
                                )}{...rest}
                         />
          </ReferenceInput>

       </Fragment>
      )}
</FormDataConsumer>`

In db there are fields like this

{ doctype: "A", docname: "X", id: 1 },
{ doctype: "A", docname: "Y", id: 2 },
{ doctype: "B", docname: "Z", id: 3 }

in first drop-down i want to show unique data from doctype and in second it will show docname accordingly

1 Answer 1

1

We used a custom Input Component for that purpose:

interface UniqueSelectInputProps {
  choices?: [];
  source?: string;
}

const UniqueSelectInput = (props: UniqueSelectInputProps) => {
  const { choices, source } = props;

  let choices_unique: Array<{ name: String; id: String }> = [];
  if (choices && source) {
    const choices_unique_list = choices
      .map(item => item[source])
      .filter((value, index, self) => self.indexOf(value) === index);
    choices_unique = choices_unique_list.map(item => {
      return { name: item, id: item };
    });
  }
  return (
    <>
      <SelectInput choices={choices_unique} source={source} />
    </>
  );
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.