Use Flatpickr in Vue.js

in #utopian-io6 years ago (edited)

flafa.png
image-source
In this tutorial, I will speak terms related to date. certainly in a website, the date is an important thing. where every activity on our website will happen by date. and this time I will mengimplentasikan calender flatpickr. nah usual flatpickr many in use and in mix with jquery, but this time I will use it in komponent vue.js. just start

What Will I Learn?

  • Install Package with NPM
  • Use Flatpickr in Component
  • Customize flapickr

Requirements

  • Install Node.js
  • Install Vue-Cli
  • Basic jQuery and Es6

Difficulty

  • Basic

Install Package with Npm

Before we start installing the vue-select package, make sure you have installed nodejs , so you can use Npm. to check its version npm -v
Screenshot_1.png
and now you can install the vue-select. Open your folder that has installed vue-cli. open command prompt . and then you can install .
npm install vue-flatpickr-component --save, for notes. i added --save: it's means you want to save in your package.json
Screenshot_7.png
Screenshot_6.png

User Flatpickr in Component

I will use flatpickr in vue component, This is the App.vue component structure.

<template>
  <div id="app">
    <h1>Vue Flat-Pickr</h1>
   <flat-pickr v-model="Mydate"></flat-pickr>
 <h3 style="margin-top:10px;">Date : {{Mydate}}</h3>
  </div>
</template>
<script>
import flatPickr from 'vue-flatpickr-component'
import 'flatpickr/dist/flatpickr.css'
export default {
  data(){
    return{
       Mydate: null
    }
  },
  components:{
    flatPickr
  }
}
</script>
<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

import flatPickr from 'vue-flatpickr-component' : This is how to import flatpickr component.

import 'flatpickr/dist/flatpickr.css' : This is how to import Css from flatpickr.

Mydate: null : Local data to accommodate the value of flatpickr.

components:{flatPickr} : after we import flatpickr. we will register it in component { }properties

This is important, once you import and register it in the component. you can use it in template
< flat-pickr v-model="Mydate" >< /flat-pickr >

You can use it with the tag <flat-pickr></flat-pickr> . I can use v-model for bind input with local data. the name of v-model must same with data local.
after we finish importing, registering, and using the flat-pickr component. then next we run our vue-cli, open your folder directory then run npm run dev
Screenshot_9.png
Screenshot_10.png
Now, you can oper your browser and your host in localhost:8080. and this the result of our code
Screenshot_8.png
Screenshot_11.png

Customize Flapickr

When we use flatpickr, we will be given default view from flatpickr but we also can customize the flatpickr. I will change the format calender in flatpickr. We can wrap our customization configuration in the form object in our local data, I will make the object name is MyConfig.


MyConfig: {
       altFormat: "F j, Y",
       dateFormat:"d-M-Y"
      }

altFormat : Exactly the same as date format, but in use in the input field. dateFormat:"d-M-Y" :to set the date display form that we want here I want to view its data like this "Date-Month-Year". after we set up its configuration, do not forget to bind it in our component. We can use like this <flat-pickr v-model="Mydate" :config="MyConfig" >< /flat-pickr >. v-bind:config or :config is a name of props, and the "MyConfig" is a name of object we set in local data. After we finished the configuration, we will see its result like this.

Screenshot_13.png

We can see the date format has changed, it means we managed to customize it. we have used flatpickr and can customize it there are many that we can customize, please have visit flatpickr official website. so many of me hopefully this tutorial useful for you, thank you



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

Hey @alfarisi94 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Congratulations @alfarisi94! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 65306.74
ETH 3488.89
USDT 1.00
SBD 2.51