materialize icon indicating copy to clipboard operation
materialize copied to clipboard

[1.0.0-alpha.3] Datepicker: When you select a date, the value is not inserted in the input tag

Open andrey-helldar opened this issue 8 years ago • 12 comments

Expected Behavior

When selecting a date, the value attribute of the input tag should be filled.

Current Behavior

The value attribute of the input tag does not fill out.

Steps to Reproduce (for bugs)

I initialize several components on the page, and none of them returns a value. I also tried to leave only one component on the page. Exactly the same behavior is observed on the page http://next.materializecss.com/pickers.html.

My code:

let elem = document.querySelector('.datepicker');
M.Datepicker.init(elem, options);

Your Environment

  • Version used: 1.0.0-alpha.3
  • Framework: VueJS framework 2.5.13
  • Browser Name and version: Irrelevant
  • Operating System and version (desktop or mobile): Irrelevant

andrey-helldar avatar Feb 14 '18 09:02 andrey-helldar

The value attribute is for default initial value

Dogfalo avatar Feb 14 '18 16:02 Dogfalo

@Dogfalo The form sends data to the onsubmit event. How do I get the value of a form if the component does not return a value automatically?

Part of my code in VueJS component:

<template>
  <form class="row" @submit="store">
    <div class="input-field col s6">
      <input placeholder="Placeholder" id="first_name" type="text" class="validate" v-model="formData.firstName">
      <label for="first_name">First Name</label>
    </div>
    <div class="col s6">
      <input type="text" id="start_date" class="datepicker" v-model="formData.startDate">
      <label for="start_date">Start Date</label>
    </div>
    <div class="col s6">
      <button class="btn waves-effect waves-light" type="submit">send</button>
    </div>
  </form>
</template>
<script type="text/javascript">
  data() {
    return {
      url: {
        store: '/orders/store'
      },
      formData: {}
    }
  },
  mounted() {
    let elem = document.querySelector('.datepicker');
    M.Datepicker.init(elem);
  },
  methods: {
    store(e) {
      e.preventDefault();

      axios.post(this.url.store, this.formData)
        .then(response => {
          // ...
        })
        .catch(error => {
          // ...
        })
    }
    // ...
  }
</script>

Is it really necessary to store the instance of the datepicker component and before accessing the form, contact it?

andrey-helldar avatar Feb 14 '18 16:02 andrey-helldar

Your inputs are missing the name attribute

Dogfalo avatar Feb 14 '18 17:02 Dogfalo

I'm using the VueJS framework. If you use the v-model method, you do not need to transfer a name: https://vuejs.org/v2/api/#v-model and https://vuejs.org/v2/guide/forms.html

andrey-helldar avatar Feb 14 '18 19:02 andrey-helldar

v-model will only update on the input event so the 2 way binding will not work in the picker. You can listen to the change event instead and set the value on the model to fix this

Dogfalo avatar Feb 14 '18 20:02 Dogfalo

In general, when the value of the value attribute changes, v-model updates the value of the corresponding variable inside the framework.

Datepicker does not change the value of value for the element, hence, VueJS can not track this.

It seems that the solution to the problem will not be easy, since there can be several datepicker elements on one page.

If you do not take into account the JS-framework, the same behavior of the element is observed on your site: http://next.materializecss.com/pickers.html

2018-02-15 00-35-22 developer tools - http next materializecss com pickers html

It turns out that when you submit the form, you will need to catch additional fields with a date and get their values, substituting in the sent data, as you said.

andrey-helldar avatar Feb 14 '18 21:02 andrey-helldar

I don't see vue's example exhibiting that behavior either: https://vuejs.org/v2/guide/forms.html

Dogfalo avatar Feb 14 '18 22:02 Dogfalo

This is it: https://vuejs.org/v2/guide/forms.html#Text

andrey-helldar avatar Feb 15 '18 06:02 andrey-helldar

The input there doesn't have its value attribute changed either

Dogfalo avatar Feb 15 '18 06:02 Dogfalo

just used v-model.lazy to get datepicker value. Cheers! :)

aungmyohlaing avatar Jul 18 '18 10:07 aungmyohlaing

@aungmyohlaing v-model.lazy works. Do you know the reason behind this behaviour? Thanks for the solution though.

Ravin001 avatar Aug 08 '18 18:08 Ravin001

@aungmyohlaing v-model.lazy works. Do you know the reason behind this behaviour? Thanks for the solution though.

"By default, v-model syncs the input with the data after each input event ... You can add the lazy modifier to instead sync after change events"

Thank you for the tip. Vue Docs on Forms

ToniMaunde avatar Oct 12 '18 00:10 ToniMaunde