Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to set date to datepicker ? #4

Closed
mohammadfiroozi opened this issue May 15, 2021 · 7 comments
Closed

how to set date to datepicker ? #4

mohammadfiroozi opened this issue May 15, 2021 · 7 comments

Comments

@mohammadfiroozi
Copy link

how to set date from data model to datepicker and show it in datepicker?

@alireza-ab
Copy link
Owner

alireza-ab commented May 15, 2021

Hi @mohammadfiroozi

In single mode use string and in range mode use array. (date must be gregorian)

<date-picker v-model="date" mode="single"></date-picker>
export default {
  data() {
    return {
      date: "2021/05/12"
    }
  }
}

<date-picker v-model="date" mode="range"></date-picker>
export default {
  data() {
    return {
      date: ["2021/05/12", "2021/05/15"]
    }
  }
}

@mohammadfiroozi
Copy link
Author

this not working when i set it in created life cycle or other method,

<date-picker v-model="formValues.birthDate" mode="single"></date-picker>

`
created() {

this.formValues= {
nationalCode: this.nationalCode,
birthDate: "2021/05/12"
};

}
`

@alireza-ab
Copy link
Owner

The data model must be filled before the component is mounted.
I suggest using beforeMount to set the data model.

If you can not set the data model before mount, use this way:

<date-picker v-model="date" mode="single" ref="dp"></date-picker>
import { PersianDate } from "@alireza-ab/vue-persian-datepicker"
export default {
  data() {
    return {
      date: null
    }
  },
  created(){
    this.$refs.dp.selectDate(new PersianDate("2021/5/12"), "date");
  }
}

In the next version, I add a better way to set the data model.

@alireza-ab
Copy link
Owner

alireza-ab commented May 25, 2021

Hi @mohammadfiroozi
the new version is published and I add the setDate method for your problem.

<date-picker v-model="date" mode="single" ref="dp"></date-picker>
export default {
  data() {
    return {
      date: null
    }
  },
  mounted(){
    this.$refs.dp.setDate("2021/5/12");
  }
}

I suggest setting the data model before the component is mounted. use this way, after the component is mounted.

@honarmanly
Copy link

honarmanly commented Feb 22, 2022

$ref is not defined in created phase, but I used it in mounted and it works

@alireza-ab
Copy link
Owner

@honarmanly
Yes. it's right. I fix it.

@honarmanly
Copy link

@honarmanly Yes. it's right. I fix it.

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants