Materialize + Framework not updating select value
Model value is not updated when material_select() is applied to the <select>.
I've made a fiddle to show you what's going on: http://jsfiddle.net/maxflex/j8xcm7d2/
There are also other people facing the same problem: one, two
I have the same issue with AngularJS.
I have the same problem with Angular 2
I don't know Vue.js enough but with Aurelia I had to issue a custom event on the jQuery change event to make that work. Maybe it helps.
This seems to work:
new Vue({
el: 'body',
ready: function() {
var suspend = false;
$('#materialize-select').material_select();
$('#materialize-select').on('change', function() {
if (!suspend) {
suspend = true;
var event = new CustomEvent('change', {
detail: 'change',
bubbles: true
});
$(this).get(0).dispatchEvent(event);
}
});
}
})
But only once in this example since the custom change also triggers jQuery's change. You'd have to prevent stack overflow in a cleverer way than me. :smile:
(f.i. use a real callback and reset suspend there)
Updated fiddle: http://jsfiddle.net/jk5800wh/
Yes, with Angular I had to do a manual binding. When change the model, I execute a JQuery function to update de select component.
Well, I guess you could encapsulate that behaviour in a component easily with either Angular, Angular2 or Vue.js :smile:
Can you give the exact code you used @ferchoman09?
of course @MichaelDeBoey
This is my select component:
<div class="input-field col s12 m6">
<select id="usuario" name="Consecutivo" ng-model="idUsuario" my-material-select>
<option value="" disabled selected>Seleccione un usuario</option>
<option ng-repeat="usuario in usuarios" value="{{usuario.idusuario}}">{{usuario.usr_login}}</option>
</select>
<label for="usuario">Usuario</label>
</div>
I use a custom directive:
.directive("myMaterialSelect", [ "$timeout", function($timeout) {
return {
restrict: 'A',
require : 'ngModel',
link : function(scope, element, attrs, ngModelCtrl) {
$(function() {
$(element).material_select();
//Cambia el modelo cuando cambia el elemento seleccionado
$(element).change(function() {
ngModelCtrl.$setViewValue($(element).val());
});
});
}
}
}])
And in the controller I am watching the variable that I have in the ng-model of select component:
$scope.$watch("formCaja.consecutivo.id", function(newValue, oldValue) {
$timeout(function() {
$("#consecutivo").val(newValue);
$("#consecutivo").material_select();
}, 0);
});
So, I am simulating the two ways binding. Note that you must call the function "material_select()" every time that changes the collection of your select component.
$timeout(function() {
$("#consecutivo").material_select();
}, 0);
Not the best, but I had to do so. Hope this can help you
Oh you're still using Angular.js, not Angular 2 @ferchoman09?
@MichaelDeBoey Oh! Sorry, is Angular 1.5. :(
No problem @ferchoman09 :smile:
It's defo a MaterializeCSS problem with the implementation of the select
When using the browser-default class it all works fine so... :worried:
Yes, I hope too that materializecss team improve the select implementation for compatibility with frameworks.
Every two-way binding framework seems to be affected. There are surely ways to get around this problem, I use a custom directive myself – but yeah, we are waiting for some improvements on this one.
I have the same problem with Angular 2, any solution for this problem?
My problem is little bit different: I have an material-select
- I select a value and php write this value to database.
- I populate database as table
- When I click a row modal open and material-select but cant show selected value. If I use browser-default it works. All I want this When I click on green button Select shows 1024 or whatever in value in table (Bandwidth) in fiddle dont know how can I do this
http://jsfiddle.net/tceydeliler/9j8zzfn6
I think Its called as DOM manupulation.
For change the materializecss select I had to do that:
$("#idSelect").val(newValue);
$("#idSelect").material_select();
Where do I use "#idselect" ?
It works. Thnx
@ferchoman09 thanks for the workaround. shame on you authors.
In angular2 you can use it:
For get:
$('select').material_select();
$('select').change((e) => {
this.model[e.currentTarget.name] = e.currentTarget.value;
});
For set:
this.model.gender = 'male'; // or this.model = newValues;
$('select').material_select();
@Thanood is on right track just add else.
new Vue({
el: 'body',
ready: function() {
var suspend = false;
$('#materialize-select').material_select();
$('#materialize-select').on('change', function() {
if (!suspend) {
suspend = true;
var event = new CustomEvent('change', {
detail: 'change',
bubbles: true
});
$(this).get(0).dispatchEvent(event);
} else {
suspend = false;
}
});
}
})
http://jsfiddle.net/GreyZact/qukwodpv/
It even works if there are multiple select objects.