Comments (16)
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.
@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.
@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.
@Fkscorpion @bakoserge I recommend you to update to latest version and see the sample code in Form Validation page.
from ngx-materialize.
ok. will try that and let you know .
thanks again for the reply.
from ngx-materialize.
Good, I'm gonna close this issue.
from ngx-materialize.
from ngx-materialize.
Can you add your typescript/javascript code for this example?
from ngx-materialize.
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.
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.
@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.
this seems to select the last entry as the default always for me.
from ngx-materialize.
am already pointing at the master branch. Would I still need to update anything?
from ngx-materialize.
You only have to do a npm install
or yarn install
to fetch the latest version if you are on master.
from ngx-materialize.
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.
@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)
- How to make a fixed navbar ? HOT 3
- Ho to align material-icon and a text-input HOT 1
- Tooltip with max width HOT 1
- Components no implements (click)
- autocomplete not working for input if data is dynamically bind through webservice HOT 3
- MzToastService.show: Materialize is not defined HOT 2
- Autocomplete not working with chip input (does with regular input) HOT 1
- Dropdown click inisde elements closes HOT 1
- Errors after updating to Angular 7 HOT 2
- Pagination component.
- Sidenav - this.collapseButton.sideNav is not a function HOT 5
- "Falling" icon in float+flat button in a mz-navbar HOT 1
- Chip selected when scrolling (swiping) through chip autocomplete list.
- Add own picture in select option with data-icon?
- Date picker and Time picker issue in latest chrome browser HOT 15
- Date picker - How Modify default Language HOT 2
- Text area not expanding on large input size HOT 1
- Getting compilation error when import ngx-materialize. HOT 1
- pagination breaks after totalItems = 0 HOT 1
- @angular/core has no exported member 'Renderer' HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngx-materialize.