sherweb / ngx-materialize

Angular wrap around Materialize library
https://sherweb.github.io/ngx-materialize/
Apache License 2.0
314 stars 75 forks source link

Select default value #165

Closed bakoserge closed 7 years ago

bakoserge commented 7 years ago

Having some trouble setting the default value of the dropdown select. here is my sample code using angular 4.

<mz-select-container>
  <select id="options-select" required name="hour" mz-select [label]="'Hour'"
 [(ngModel)]="appointment.startHour">
    <option *ngFor="let hour of hours" [ngValue]="hour">{{hour}}</option>
  </select>
</mz-select-container>
scote commented 7 years ago

Maybe you should try to use [value] instead of [ngValue] if hour is a number. The [ngValue] is used when the select options are complex object (class).

bakoserge commented 7 years ago

I did use value, ngvalue. attr.value none of them work.

On Wed, Jul 19, 2017 at 8:43 AM Sébastien Côté notifications@github.com wrote:

Maybe you should try to use [value] instead of [ngValue] if hour is a number. The [ngValue] is used when the select options are complex object (class).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/sherweb/ng2-materialize/issues/165#issuecomment-316373614, or mute the thread https://github.com/notifications/unsubscribe-auth/ABGZ1XgXEkNyO-kqAqhN52wRYZ5N3K_Xks5sPfnWgaJpZM4Ob-PK .

scote commented 7 years ago

Can you add your typescript/javascript code for this example?

bakoserge commented 7 years ago

Here is the typescript variable set in the contructor

private appointment: Appointment = new Appointment();

constructor(){
this.hours = Array(23).fill(23).map((x,i)=>(i+1));
}

export class Appointment{
    startHour: number = 1; //default value
}

html:

<mz-select-container>
  <select id="options-select" required name="hour" mz-select [label]="'Hour'"
 [(ngModel)]="appointment.startHour">
    <option *ngFor="let hour of hours" [ngValue]="hour">{{hour}}</option>
  </select>
</mz-select-container>
Fkscorpion commented 7 years ago

I'm having a similar issue with the mz-select directive. And I try to use a multiple select.

Template code:

<div *ngFor="let user of userList; let i=index">
    <mz-select-container>
        <select mz-select
                [placeholder]="'Select a permission'"
                [filledIn]="true"
                [id]="perm-select + i"
                multiple
                [(ngModel)]="user.permissions"
        >
            <option *ngFor="let perm of availablePermissions" class="left circle"
                    [ngValue]="perm"
                    [selected]="user.permissions.indexOf(perm) > -1 ? true : null"
            >{{ perm }}</option>
        </select>
    </mz-select-container>
</div>

Component:

export class UserAdministrationComponent {

  public userList: Array<Object>;
  public availablePermissions: Array<string>;

 //... code receiving the data.

}

Those 2 arrays then get filled with data from the backend.

the userList is filled with objects that look like this:

userList = [
  {
    name: "shepard",
    permissions: ['login', 'likeStoresOnTheCitadel']
  }
];

While the availablePermissions array looks something like this:

availablePermissions = ['login', 'killReapers', 'likeStoresOnTheCitadel'];

The ngModel two-way binding works in this case and the user is updated correctly, when selecting multiple permissions. However, the "preselect" does not work at all. It either just selects the last element or none - when it should add the "select" attribute to all permissions, the user already has.

Any ideas?

scote commented 7 years ago

@Fkscorpion Here a workaround for your case : Replace [selected] attribute for

[attr.selected]="user.permissions.indexOf(perm) > -1 ? '' : null"

And for @bakoserge

[attr.selected]="hour === appointment.startHour"

We are currently investigating this bug to make the select component options working with [value] and [ngValue].

Fkscorpion commented 7 years ago

@scote Thank you very much for your quick answer. Unfortunately I've already tried that and it didn't work.

When I change it to this:

<mz-select-container>
    <select mz-select
            [placeholder]="'Select a permission'"
            [filledIn]="true"
            [id]="perm-select + i"
            multiple
            [(ngModel)]="user.permissions"
    >
        <option *ngFor="let perm of permissions" class="left circle"
                [ngValue]="perm"
                [attr.selected]="user.permissions.indexOf(perm) > -1 ? '' : null"
        >{{ perm }}</option>
    </select>
</mz-select-container>

then none of the option fields is pre selected.

I've also tried it with

[attr.selected]="true"

to make sure, that the condition isn't the problem, but still the same result :/

bakoserge commented 7 years ago

this seems to select the last entry as the default always for me.

scote commented 7 years ago

@Fkscorpion you could try to use includes function :

[selected]="user.permissions.includes(perm)"

In my snippet I have removed the [ngValue] attribute.

scote commented 7 years ago

@Fkscorpion @bakoserge I recommend you to update to latest version and see the sample code in Form Validation page.

bakoserge commented 7 years ago

am already pointing at the master branch. Would I still need to update anything?

scote commented 7 years ago

You only have to do a npm install or yarn install to fetch the latest version if you are on master.

bakoserge commented 7 years ago

ok. will try that and let you know . thanks again for the reply.

bakoserge commented 7 years ago

just did a test and seems to be working fine.. SAMPLE CODE; <mz-select-container> <select mz-select id="options-select" [label]="'category'" [placeholder]="'Placeholder'" [(ngModel)]="_dataService.loggedInUser.category"> <option *ngFor="let category of _dataService.categories" [value]="category.$key">{{category.$value}}</option> </select> </mz-select-container>

scote commented 7 years ago

Good, I'm gonna close this issue.

Fkscorpion commented 7 years ago

@scote Thank you very much. Just wanted to inform you, that after an update to the latest version (1.5.1.) it works, without altering my code :)