materialize icon indicating copy to clipboard operation
materialize copied to clipboard

Materialize + Framework not updating select value

Open maxflex opened this issue 10 years ago • 42 comments

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

maxflex avatar Feb 24 '16 20:02 maxflex

I have the same issue with AngularJS.

ferchoman09 avatar Feb 24 '16 21:02 ferchoman09

I have the same problem with Angular 2

MichaelDeBoey avatar Feb 26 '16 16:02 MichaelDeBoey

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/

Thanood avatar Feb 26 '16 18:02 Thanood

Yes, with Angular I had to do a manual binding. When change the model, I execute a JQuery function to update de select component.

ferchoman09 avatar Feb 26 '16 18:02 ferchoman09

Well, I guess you could encapsulate that behaviour in a component easily with either Angular, Angular2 or Vue.js :smile:

Thanood avatar Feb 26 '16 18:02 Thanood

Can you give the exact code you used @ferchoman09?

MichaelDeBoey avatar Feb 26 '16 18:02 MichaelDeBoey

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

ferchoman09 avatar Feb 26 '16 19:02 ferchoman09

Oh you're still using Angular.js, not Angular 2 @ferchoman09?

MichaelDeBoey avatar Feb 26 '16 19:02 MichaelDeBoey

@MichaelDeBoey Oh! Sorry, is Angular 1.5. :(

ferchoman09 avatar Feb 26 '16 19:02 ferchoman09

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:

MichaelDeBoey avatar Feb 26 '16 19:02 MichaelDeBoey

Yes, I hope too that materializecss team improve the select implementation for compatibility with frameworks.

ferchoman09 avatar Feb 26 '16 20:02 ferchoman09

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.

maxflex avatar Feb 26 '16 20:02 maxflex

I have the same problem with Angular 2, any solution for this problem?

Sophia-nguyen avatar Apr 04 '16 10:04 Sophia-nguyen

My problem is little bit different: I have an material-select

  1. I select a value and php write this value to database.
  2. I populate database as table
  3. 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.

tceydeliler avatar Apr 27 '16 11:04 tceydeliler

For change the materializecss select I had to do that:

    $("#idSelect").val(newValue);
    $("#idSelect").material_select();

ferchoman09 avatar Apr 27 '16 13:04 ferchoman09

Where do I use "#idselect" ?

tceydeliler avatar Apr 27 '16 13:04 tceydeliler

It works. Thnx

tceydeliler avatar Apr 27 '16 14:04 tceydeliler

@ferchoman09 thanks for the workaround. shame on you authors.

virtualvoid avatar May 25 '16 14:05 virtualvoid

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();

jmherazo avatar Jul 19 '16 20:07 jmherazo

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

GreyZact avatar Sep 13 '16 09:09 GreyZact