0

This is the data fetched from WebAPI and stored in a state called items

this.setState({
    items: dataSource
  staffId: ''
})

This state of items will be this.

[
    {"departmentName":"KYAUKSE MAINTENANCE","employeeName":"AUNG THU1", "staffId" : "00001"},
    {"departmentName":"KYAUKSE MAINTENANCE","employeeName":"AUNG THU2", "staffId" : "00002"},
    {"departmentName":"KYAUKSE MAINTENANCE","employeeName":"AUNG THU3", "staffId" : "00003"}
]

I would like to create a dropdown in react native with just the emplyeeName

let staff = [{
      value: 'AUNG THU1',
    }, {
      value: 'AUNG THU2',
    }, {
      value: 'AUNG THU3',
    }];
<Dropdown
    label='Staff Name'
    baseColor='#0079b5'
    data={staff}
    onChangeText={(value) => this.onChangeName(value)}
/>

Edit: Thank you for the answers! I would like to know whether it is possible to get the staffId of the selected value in DropDown? So if i were to select AUNG THU 1, the onChange method would trigger and I would like to set the state of staffId to it.

2 Answers 2

1

If you want to change your array, try below approach

let dataSource = [
  { departmentName: "KYAUKSE MAINTENANCE", employeeName: "AUNG THU1" },
  { departmentName: "KYAUKSE MAINTENANCE", employeeName: "AUNG THU2" },
  { departmentName: "KYAUKSE MAINTENANCE", employeeName: "AUNG THU3" }
];

let staff = dataSource.map(item => ({
  value: item.employeeName,
  staffId: item.staffId
}));

console.log(staff);

Hope this helps you. Feel free for doubts.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi! Thank you for the answer,but is there a way I can get the id of the chosen employee? For example, if I were to choose AUNG THU1 in the dropdown, I would like to get the value of his staffId.
@MinUD check your Dropdown there should be props to get the selected value.
It says selectedValue is "Value matching value of one of the items". so i pass selectedValue={value}, whereas my onChangeText={(staffId) => this.onChangeName(staffId)}. Will I be able to get the staffId this way?
@MinUD what is the library which you used for Dropdown?
It is react-native-material-dropdown
0

Double check your Dropdown component. It may be expecting a data array that has both keys and values.

let staff = [{
  key: 'AUNG THU1',
  value: 'AUNG THU1'
}, {
  key: 'AUNG THU2',
  value: 'AUNG THU2'
}, {
  key: 'AUNG THU3',
  value: 'AUNG THU3'
}];

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.