diff --git a/src/components/multiselect/MultiSelect.d.ts b/src/components/multiselect/MultiSelect.d.ts
index 9e9fcb816f..82c57e913b 100755
--- a/src/components/multiselect/MultiSelect.d.ts
+++ b/src/components/multiselect/MultiSelect.d.ts
@@ -17,6 +17,7 @@ interface MultiSelectProps {
tooltip?: any;
tooltipOptions?: TooltipOptions;
itemTemplate?(item: any): JSX.Element | undefined;
+ selectedOptionTemplate?(item: any): JSX.Element | undefined;
onChange?(e: {originalEvent: Event, value: any}): void;
}
diff --git a/src/components/multiselect/MultiSelect.js b/src/components/multiselect/MultiSelect.js
index e42766673c..1783e24abe 100644
--- a/src/components/multiselect/MultiSelect.js
+++ b/src/components/multiselect/MultiSelect.js
@@ -26,6 +26,7 @@ export class MultiSelect extends Component {
tooltip: null,
tooltipOptions: null,
itemTemplate: null,
+ selectedOptionTemplate: null,
onChange: null
};
@@ -45,6 +46,7 @@ export class MultiSelect extends Component {
tooltip: PropTypes.string,
tooltipOptions: PropTypes.object,
itemTemplate: PropTypes.func,
+ selectedOptionTemplate: PropTypes.func,
onChange: PropTypes.func,
};
@@ -204,7 +206,7 @@ export class MultiSelect extends Component {
getLabel() {
let label;
-
+
if(this.props.value && this.props.value.length) {
label = '';
for(let i = 0; i < this.props.value.length; i++) {
@@ -223,35 +225,51 @@ export class MultiSelect extends Component {
findLabelByValue(val) {
let label = null;
-
+
for(let i = 0; i < this.props.options.length; i++) {
let option = this.props.options[i];
let optionValue = this.getOptionValue(option);
-
+
if(ObjectUtils.equals(optionValue, val)) {
label = this.getOptionLabel(option);
- break;
+ break;
}
}
-
+
return label;
}
+ findOptionByValue(val) {
+ let option = null;
+
+ for(let i = 0; i < this.props.options.length; i++) {
+ let opt = this.props.options[i];
+ let optionValue = this.getOptionValue(opt);
+
+ if(ObjectUtils.equals(optionValue, val)) {
+ option = opt;
+ break;
+ }
+ }
+
+ return option;
+ }
+
onFocus() {
DomHandler.addClass(this.container, 'p-focus');
}
-
+
onBlur() {
DomHandler.removeClass(this.container, 'p-focus');
}
-
+
bindDocumentClickListener() {
if(!this.documentClickListener) {
this.documentClickListener = this.onDocumentClick.bind(this);
document.addEventListener('click', this.documentClickListener);
}
}
-
+
unbindDocumentClickListener() {
if(this.documentClickListener) {
document.removeEventListener('click', this.documentClickListener);
@@ -287,43 +305,43 @@ export class MultiSelect extends Component {
if(!this.selfClick && !this.panelClick && this.panel.element.offsetParent) {
this.hide();
}
-
+
this.clearClickState();
}
-
+
clearClickState() {
this.selfClick = false;
this.panelClick = false;
}
-
+
filterOption(option) {
let filterValue = this.state.filter.trim().toLowerCase();
let optionLabel = this.getOptionLabel(option);
-
+
return optionLabel.toLowerCase().indexOf(filterValue.toLowerCase()) > -1;
}
-
+
hasFilter() {
return this.state.filter && this.state.filter.trim().length > 0;
}
-
+
isAllChecked(visibleOptions) {
if(this.hasFilter())
return this.props.value && visibleOptions && visibleOptions.length&&(this.props.value.length === visibleOptions.length);
else
return this.props.value && this.props.options && (this.props.value.length === this.props.options.length);
- }
-
+ }
+
filterOptions(options) {
return options.filter((option) => {
return this.filterOption(option);
});
}
-
+
getOptionValue(option) {
return this.props.optionLabel ? option : option.value;
}
-
+
getOptionLabel(option) {
return this.props.optionLabel ? ObjectUtils.resolveFieldData(option, this.props.optionLabel) : option.label;
}
@@ -343,11 +361,29 @@ export class MultiSelect extends Component {
);
}
+ renderLabel() {
+ if(this.props.selectedOptionTemplate) {
+ if(this.props.value && this.props.value.length) {
+ return this.props.value.map((val, index) => {
+ return
In addition selectedOptionTemplate can be used to customize the selected values display instead of the default comma separated list.
+ +Filtering allows searching items in the list using an input field at the header. In order to use filtering, enable filter property.
@@ -245,6 +309,12 @@ carTemplate(option) {