@@ -153,6 +153,12 @@ export class SelectEditor implements Editor {
153
153
* The current selected values (multiple select) from the collection
154
154
*/
155
155
get currentValues ( ) {
156
+ // collection of strings, just return the filtered string that are equals
157
+ if ( this . collection . every ( x => typeof x === 'string' ) ) {
158
+ return this . collection . filter ( c => this . $editorElm . val ( ) . indexOf ( c . toString ( ) ) !== - 1 ) ;
159
+ }
160
+
161
+ // collection of label/value pair
156
162
const separatorBetweenLabels = this . collectionOptions && this . collectionOptions . separatorBetweenTextLabels || '' ;
157
163
const isIncludingPrefixSuffix = this . collectionOptions && this . collectionOptions . includePrefixSuffixToSelectedValues || false ;
158
164
@@ -180,6 +186,12 @@ export class SelectEditor implements Editor {
180
186
* The current selected values (single select) from the collection
181
187
*/
182
188
get currentValue ( ) {
189
+ // collection of strings, just return the filtered string that are equals
190
+ if ( this . collection . every ( x => typeof x === 'string' ) ) {
191
+ return findOrDefault ( this . collection , ( c : any ) => c . toString ( ) === this . $editorElm . val ( ) ) ;
192
+ }
193
+
194
+ // collection of label/value pair
183
195
const separatorBetweenLabels = this . collectionOptions && this . collectionOptions . separatorBetweenTextLabels || '' ;
184
196
const isIncludingPrefixSuffix = this . collectionOptions && this . collectionOptions . includePrefixSuffixToSelectedValues || false ;
185
197
const itemFound = findOrDefault ( this . collection , ( c : any ) => c [ this . valueName ] . toString ( ) === this . $editorElm . val ( ) ) ;
@@ -387,37 +399,45 @@ export class SelectEditor implements Editor {
387
399
const isRenderHtmlEnabled = this . columnDef . internalColumnEditor . enableRenderHtml || false ;
388
400
const sanitizedOptions = this . gridOptions && this . gridOptions . sanitizeHtmlOptions || { } ;
389
401
390
- collection . forEach ( ( option : SelectOption ) => {
391
- if ( ! option || ( option [ this . labelName ] === undefined && option . labelKey === undefined ) ) {
392
- throw new Error ( `[select-editor] A collection with value/label (or value/labelKey when using Locale) is required to populate the Select list, for example: { collection: [ { value: '1', label: 'One' } ])` ) ;
393
- }
394
- const labelKey = ( option . labelKey || option [ this . labelName ] ) as string ;
395
- const labelText = ( ( option . labelKey || this . enableTranslateLabel ) && labelKey ) ? this . _translate . instant ( labelKey || ' ' ) : labelKey ;
396
- let prefixText = option [ this . labelPrefixName ] || '' ;
397
- let suffixText = option [ this . labelSuffixName ] || '' ;
398
- let optionLabel = option [ this . optionLabel ] || '' ;
399
- optionLabel = optionLabel . toString ( ) . replace ( / \" / g, '\'' ) ; // replace double quotes by single quotes to avoid interfering with regular html
400
-
401
- // also translate prefix/suffix if enableTranslateLabel is true and text is a string
402
- prefixText = ( this . enableTranslateLabel && prefixText && typeof prefixText === 'string' ) ? this . _translate . instant ( prefixText || ' ' ) : prefixText ;
403
- suffixText = ( this . enableTranslateLabel && suffixText && typeof suffixText === 'string' ) ? this . _translate . instant ( suffixText || ' ' ) : suffixText ;
404
- optionLabel = ( this . enableTranslateLabel && optionLabel && typeof optionLabel === 'string' ) ? this . _translate . instant ( optionLabel || ' ' ) : optionLabel ;
405
-
406
- // add to a temp array for joining purpose and filter out empty text
407
- const tmpOptionArray = [ prefixText , labelText , suffixText ] . filter ( ( text ) => text ) ;
408
- let optionText = tmpOptionArray . join ( separatorBetweenLabels ) ;
409
-
410
- // if user specifically wants to render html text, he needs to opt-in else it will stripped out by default
411
- // also, the 3rd party lib will saninitze any html code unless it's encoded, so we'll do that
412
- if ( isRenderHtmlEnabled ) {
413
- // sanitize any unauthorized html tags like script and others
414
- // for the remaining allowed tags we'll permit all attributes
415
- const sanitizedText = DOMPurify . sanitize ( optionText , sanitizedOptions ) ;
416
- optionText = htmlEncode ( sanitizedText ) ;
417
- }
402
+ // collection could be an Array of Strings OR Objects
403
+ if ( collection . every ( x => typeof x === 'string' ) ) {
404
+ collection . forEach ( ( option : string ) => {
405
+ options += `<option value="${ option } " label="${ option } ">${ option } </option>` ;
406
+ } ) ;
407
+ } else {
408
+ // array of objects will require a label/value pair unless a customStructure is passed
409
+ collection . forEach ( ( option : SelectOption ) => {
410
+ if ( ! option || ( option [ this . labelName ] === undefined && option . labelKey === undefined ) ) {
411
+ throw new Error ( `[select-editor] A collection with value/label (or value/labelKey when using Locale) is required to populate the Select list, for example: { collection: [ { value: '1', label: 'One' } ])` ) ;
412
+ }
413
+ const labelKey = ( option . labelKey || option [ this . labelName ] ) as string ;
414
+ const labelText = ( ( option . labelKey || this . enableTranslateLabel ) && labelKey ) ? this . _translate . instant ( labelKey || ' ' ) : labelKey ;
415
+ let prefixText = option [ this . labelPrefixName ] || '' ;
416
+ let suffixText = option [ this . labelSuffixName ] || '' ;
417
+ let optionLabel = option [ this . optionLabel ] || '' ;
418
+ optionLabel = optionLabel . toString ( ) . replace ( / \" / g, '\'' ) ; // replace double quotes by single quotes to avoid interfering with regular html
418
419
419
- options += `<option value="${ option [ this . valueName ] } " label="${ optionLabel } ">${ optionText } </option>` ;
420
- } ) ;
420
+ // also translate prefix/suffix if enableTranslateLabel is true and text is a string
421
+ prefixText = ( this . enableTranslateLabel && prefixText && typeof prefixText === 'string' ) ? this . _translate . instant ( prefixText || ' ' ) : prefixText ;
422
+ suffixText = ( this . enableTranslateLabel && suffixText && typeof suffixText === 'string' ) ? this . _translate . instant ( suffixText || ' ' ) : suffixText ;
423
+ optionLabel = ( this . enableTranslateLabel && optionLabel && typeof optionLabel === 'string' ) ? this . _translate . instant ( optionLabel || ' ' ) : optionLabel ;
424
+
425
+ // add to a temp array for joining purpose and filter out empty text
426
+ const tmpOptionArray = [ prefixText , labelText , suffixText ] . filter ( ( text ) => text ) ;
427
+ let optionText = tmpOptionArray . join ( separatorBetweenLabels ) ;
428
+
429
+ // if user specifically wants to render html text, he needs to opt-in else it will stripped out by default
430
+ // also, the 3rd party lib will saninitze any html code unless it's encoded, so we'll do that
431
+ if ( isRenderHtmlEnabled ) {
432
+ // sanitize any unauthorized html tags like script and others
433
+ // for the remaining allowed tags we'll permit all attributes
434
+ const sanitizedText = DOMPurify . sanitize ( optionText , sanitizedOptions ) ;
435
+ optionText = htmlEncode ( sanitizedText ) ;
436
+ }
437
+
438
+ options += `<option value="${ option [ this . valueName ] } " label="${ optionLabel } ">${ optionText } </option>` ;
439
+ } ) ;
440
+ }
421
441
422
442
return `<select id="${ this . elementName } " class="ms-filter search-filter" ${ this . isMultipleSelect ? 'multiple="multiple"' : '' } >${ options } </select>` ;
423
443
}
0 commit comments