Git Product home page Git Product logo

Comments (16)

scote avatar scote commented on June 4, 2024 1

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).

from ngx-materialize.

scote avatar scote commented on June 4, 2024 1

@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].

from ngx-materialize.

scote avatar scote commented on June 4, 2024 1

@Fkscorpion you could try to use includes function :

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

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

from ngx-materialize.

scote avatar scote commented on June 4, 2024 1

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

from ngx-materialize.

bakoserge avatar bakoserge commented on June 4, 2024 1

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

from ngx-materialize.

scote avatar scote commented on June 4, 2024 1

Good, I'm gonna close this issue.

from ngx-materialize.

bakoserge avatar bakoserge commented on June 4, 2024

from ngx-materialize.

scote avatar scote commented on June 4, 2024

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

from ngx-materialize.

bakoserge avatar bakoserge commented on June 4, 2024

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>

from ngx-materialize.

Fkscorpion avatar Fkscorpion commented on June 4, 2024

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?

from ngx-materialize.

Fkscorpion avatar Fkscorpion commented on June 4, 2024

@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 :/

from ngx-materialize.

bakoserge avatar bakoserge commented on June 4, 2024

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

from ngx-materialize.

bakoserge avatar bakoserge commented on June 4, 2024

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

from ngx-materialize.

scote avatar scote commented on June 4, 2024

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

from ngx-materialize.

bakoserge avatar bakoserge commented on June 4, 2024

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>

from ngx-materialize.

Fkscorpion avatar Fkscorpion commented on June 4, 2024

@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 :)

from ngx-materialize.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.