[Vuejs]-Vue.js and Bootstrap Modal – Invoking JS

0πŸ‘

Will that help with what I’m trying to do?

Not exactly, I misunderstood and thought that the datepicker is a bootstrap component. Unfortunately it is not, and so while vue implementation of bootstrap could be helpful, it would leave you still with quite a bit to implement yourself.

Option 1

Use a Vue component that has the functionality out of the box

const app = new Vue({
  el: '#app',
  components: {
  	vuejsDatepicker
  }
})
<div id="app">
  <vuejs-datepicker></vuejs-datepicker>
</div>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vuejs-datepicker"></script>

Demo: https://codesandbox.io/s/mpklq49wp

If that does what you need then vuejs-datepicker component may be better for you to implement.

Option 2

Get jquery and Vue to talk

This is not ideal, because the two libraries will compete for DOM control. Vue mounts and unmounts of DOM elements are data driven. That means if your data changes, your DOM may replace an element that jquery may be relying on. SO you may be able to get it to work, but based on all the other stuff going on in the app, you may find some odd things happening, that said, here is a way you can do it…

<div id="app">
  <div class="container">
      <p>
        TIMEDATE: {{timedate}}
      </p>
      <div class="row">
          <div class='col-sm-6'>
              <input type='text' class="form-control" id='datetimepicker4' />
          </div>
      </div>
  </div>
</div>
const app = new Vue({
  el: '#app',
  data(){
    return{
      timedate: new Date(),
    }
  },
  methods:{
    onUpdate(e){
    this.timedate=e.date;
    }
  },
  mounted() {
    const dp = $('#datetimepicker4')
    dp.datetimepicker({
        defaultDate: this.timedate,
    });
    dp.on('dp.change', this.onUpdate )
  }
})

demo: https://jsfiddle.net/fjk6cLo8/7/

This code will allow you to add the datetime picker with a preset date. Upon change of the date, using the datetime picker, the data will be updated in the vue component (using dp.change listener)

The datetime picker is mounted once right after the component mounts, and the <input type='text' class="form-control" id='datetimepicker4' /> element is available.

Leave a comment