materialize icon indicating copy to clipboard operation
materialize copied to clipboard

floating labels are not initialized when html is loaded dynamically

Open ancientdemon opened this issue 10 years ago • 11 comments

The floating labels such as these:

<input id="password_input" type="password" name="password" class="validate" > <label class="active" for="password_input">Password</label>

Are not getting initialized and remain on the top (without floating in to the input field) without any styles applied, until I do a click within the field. (After user clicks input field, it becomes ok). This is fine and I am kind of expecting this problem as these html portions are getting loaded dynamically through angular template. So they were not there in the initial page load and missed the automatic initialization at page load. This is not an issue by itself, but my issue is that I don't know what JavaScript method to call to get them initialized. It is not mentioned in the documentation. Can someone please mention the method such that I can call it pragmatically in my code after I load the template?

If I am to give an example of what kind of help I am looking for here, your documentation explains what to with dynamically generated 'select' controls and mentions the method that I should be calling to initialize them. I don't have a problem with 'select' as I know what method to call. I am looking for a similar (see the screenshot below) method for materialize floating labels.

image

ancientdemon avatar Mar 17 '16 16:03 ancientdemon

Try Materialize.updateTextFields();

Thanood avatar Mar 17 '16 20:03 Thanood

Thanks @Thanood but that did not help. I tried applying a delay (just to make sure everything is loaded) before calling Materialize.updateTextFields(); but it did not make any difference. What does it supposed to do btw?

ancientdemon avatar Mar 17 '16 20:03 ancientdemon

Sorry, I'm at the mobile atm and cannot have a deeper look. Also I don't know how angular works. :smile: I'm using Aurelia and there is a thing called taskQueue. Essentially a delay at a certain point. I've used it here and it works: https://github.com/aurelia-ui-toolkits/aurelia-materialize-bridge/blob/master/src/input/input-update-service.js

If not, maybe I can have another look later.

Thanood avatar Mar 17 '16 21:03 Thanood

Have a look here for that Materialize function: https://github.com/Dogfalo/materialize/blob/master/js/forms.js

Thanood avatar Mar 17 '16 21:03 Thanood

Thanks again @Thanood, I had a look at the Materialize.updateTextFields() function and it seems all it does is to add the 'active' class to labels that are associated with input fields. In my case, the 'active' class is already there. My problem is that the label does not float in to the empty input fields at the startup. This works fine if my input and label fields were part of the initial html that is getting loaded. However, when they are dynamically loaded, the label remain on the top of input fields and does not float into the input field.

What I really want is to call what method that actual does the animation. Materialize.updateTextFields(); seem to prepare the labels for animation but does not do the animation itself.

Having said that it is possible that I am missing something and doing something wrong. :)

ancientdemon avatar Mar 17 '16 22:03 ancientdemon

That's strange because it's exactly what happens on document.ready:

// Add active if input element has been pre-populated on document ready
    $(document).ready(function() {
      Materialize.updateTextFields();
    });

https://github.com/Dogfalo/materialize/blob/master/js/forms.js#L28-L31

Can you reproduce that in a plunk or codepen or whatever you use?

Thanood avatar Mar 17 '16 22:03 Thanood

Check your HTML structure, it's possible that something is wrong

Dogfalo avatar Mar 18 '16 04:03 Dogfalo

@Dogfalo and @Thanood

Thanks a lot guys, while trying to setting up the plunker for this, I realized that I have done a mistake. Once I corrected that, Materialize.updateTextFields(); works like a charm!

Now, lets update the http://materializecss.com/forms.html and mention this function Materialize.updateTextFields();

Good stuff and please concider this issue closed

ancientdemon avatar Mar 18 '16 17:03 ancientdemon

Can we add Materialize.updateTextFields(); to the forms docs?

0o-de-lally avatar Apr 13 '16 14:04 0o-de-lally

Added into docs in 17d59100c517ec324e186e8b207dc5815d34229b

acburst avatar Apr 24 '16 20:04 acburst

@ancientdemon can you post the plunker link? my problem was the opposite of yours. It did not floating up when the ng-model was filled. It was required twice click to make the label floats up. I've checked the updateTextFields, at line if ($(element).val().length > 0, it shows that the val was empty. Here is my controller.js:

The "$scope.data" and the "org" are objects.

$scope.orgDetail = function(org){ $scope.data = org; Materialize.updateTextFields(); $('#modal1').openModal(); };

And this is my html: <div class="row"> <div class="input-field col s12 m12 l12"> <input id="name" type="text" class="validate" data-ng-model="data.name"> <label for="name">Organization Name</label> </div> </div>

Regards, Aria.

ariaenggar avatar Apr 29 '16 15:04 ariaenggar