ng2-select2 icon indicating copy to clipboard operation
ng2-select2 copied to clipboard

show placeholder by default

Open masoudline opened this issue 7 years ago • 6 comments

i am using ng2-select2 and set option like below code: this.options = { allowClear: true, placeholder: "select...", } i want show placeholder by default but it dosen't and the first value of list show everywhere! please help me!

masoudline avatar Aug 10 '17 18:08 masoudline

Try this:

placeholder: {
    id: "-1",
   text:  "select..."
  }

Source

superMDguy avatar Aug 10 '17 22:08 superMDguy

your code not working and my issue still exists

masoudline avatar Aug 11 '17 05:08 masoudline

The trick was doing this:

<select2  [data]="countryList" [options]="selectOptions">
	<option></option>
</select2>

ayyash avatar Aug 26 '17 19:08 ayyash

Try this:

this.options = { placeholder: { id: '', text: 'Select an option' } }

this.data = { { id: '', text: 'Empty'}, { id: 1, text: 'Option 1'}, }

werad avatar Oct 31 '17 21:10 werad

In my case I created a pipe to handle the options configuration for me.


export class OptionsPipe implements PipeTransform {
  transform(options: any, placeholder: string, data: any): {} {
    let defaultOptions: Select2Options;
    defaultOptions = {};

    defaultOptions['width'] = '100%';
    defaultOptions['containerCssClass'] = 'someClass';
    defaultOptions['placeholder'] = placeholder; <<< Your default placeholder

    return defaultOptions;
  }
}

and you would use it like so

*   {} | optionsPipe: 'Placeholder'
* Example:
*   [options]="options | optionsPipe: 'Suffix'" <<< here you pass down your `options` and set your `placeholder`. 

Since we have multiple select in our application this was an easy way to extract this functionality. Hope this helps

diegodesouza avatar Jan 11 '18 17:01 diegodesouza

<select2 [cssImport]=true [options]="select2Options" [width]="'50%'"> <option></option> <option *ngFor="let tag of tags"> {{ tag.name }}</option> </select2>

slk500 avatar Oct 08 '18 10:10 slk500