Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Allow us to choose a different optionLabel to display selected items in MultiSelect #451

Closed
robbevwinckel opened this issue Jun 20, 2018 · 0 comments
Assignees
Labels
Type: New Feature Issue contains a new feature or new component request
Milestone

Comments

@robbevwinckel
Copy link

robbevwinckel commented Jun 20, 2018

I'm submitting a ...

[ ] bug report
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://forum.primefaces.org/viewforum.php?f=57

Allow us to choose a different optionLabel to display selected items in MultiSelect (see picture)
image

I have an object:

{
  code: 1,
  description: description of code 1,
  optionLabel: $code - $description
}

I would like to show optionLabel in the dropdown, and code only in the selected items bar

Played around, and found an easy way to get what I want, feel free to copy code:

MultiSelect.d.ts

interface MultiSelectProps {
  ... // code omitted
  optionLabel?: string; // see PS*
  selectedOptionLabel?: string;
  ... // code omitted
}

MultiSelect.js

MultiSelect.defaultProps = {
    ... // code omitted
    selectedOptionLabel: null,
    ... // code omitted
};
MultiSelect.propTypes = {
  ... // code omitted
  selectedOptionLabel: _propTypes2.default.string,
  ... // code omitted
}
{
  key: 'getOptionLabel',
  value: function getOptionLabel(option, useSelectedOptionLabel = false) { // Added support for a selected option label
  // If a selectionOptionLabel is given to MultiSelect element, and we're attaching to the selected label, use selectedOptionLabel
  if (this.props.selectedOptionLabel && useSelectedOptionLabel) 
    return _ObjectUtils2.default.resolveFieldData(option, this.props.selectedOptionLabel)

  return this.props.optionLabel ? _ObjectUtils2.default.resolveFieldData(option, this.props.optionLabel) : option.label;
  }
}
{
  key: 'findLabelByValue',
  value: function findLabelByValue(val) {
    ... // code omitted
    
  if (_ObjectUtils2.default.equals(optionValue, val)) {
    label = this.getOptionLabel(option, true); // <=
    break;
  }
  return label;
};

In own code:

<MultiSelect optionLabel={'optionLabel'} selectedOptionLabel={'code'} />

PS: for this to work, optionLabel has to be added (see issue: https://github.com/primefaces/primereact/issues/450

@robbevwinckel robbevwinckel changed the title Allow us to choose a different optionLabel to display selected items in MultiSelect Feature request: Allow us to choose a different optionLabel to display selected items in MultiSelect Jun 22, 2018
@Merve7 Merve7 self-assigned this Nov 29, 2018
@Merve7 Merve7 added the Type: New Feature Issue contains a new feature or new component request label Nov 29, 2018
@Merve7 Merve7 added this to the 2.0.0 milestone Nov 29, 2018
@Merve7 Merve7 closed this as completed in 9d3dbf0 Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: New Feature Issue contains a new feature or new component request
Projects
None yet
Development

No branches or pull requests

2 participants