1

I am using Date Range Picker to select to dates now when I select the dates, I update the inputs with dates value respectively.

The inputs I have binded with v-model and created a function in watch attribute of component to observe the change in model.

But when the inputs are updated with the javascript function no change can be observed in the model but the value of my input fields are updated.

// My Input Fields

<input type="text" name="updateStartDate" v-model="updateDateRange.start">
 <input type="text" name="updateEndDate" v-model="updateDateRange.end">



//My javascript Function
$('input[rel=dateRangePickerX]').daterangepicker({
                        'autoApply': true,
                        'drops': 'up',
                        'startDate': moment().add(90, 'days').calendar(),
                        'endDate': moment().add(97, 'days').calendar(),
                        locale: { cancelLabel: 'Clear' }
                    },
                    function (start, end, label) {
                        $('input[name="updateStartDate"]').val(start.format('MM/DD/YYYY'));
                        $('input[name="updateEndDate"]').val(end.format('MM/DD/YYYY'));
                    });

// My watch attribute in Component
watch : {
        'updateDateRange.end' : function (val) {
            console.log('In Watch Function');
            console.log(this.dateRanges);
            if(val != '' && this.updateDateRange.start != '' && this.updateDateRangeIndex != ''){
                console.log(val);
                console.log(this.updateDateRange.start);
                console.log(this.updateDateRangeIndex);
                this.dateRanges[this.updateDateRangeIndex] = this.updateDateRange;
                this.updateDateRangeIndex = '';
                this.updateDateRange.start = '';
                this.updateDateRange.end = '';
                console.log(this.dateRanges);
            }
        }
    }
2
  • You may need to force a change event: $('input[name="updateStartDate"]').val(start.format('MM/DD/YYYY')).change() Commented Oct 5, 2016 at 14:23
  • @RoyJ I tried to force but it didn't help. actually the model is updated when we touch the input field. Commented Oct 6, 2016 at 4:30

2 Answers 2

5

I don't like to mix jQuery and Vue because jQuery messes up the DOM. Even more, I find it completely unnecessary.

Simple only with native Vue you can do it like this:

<input type="text" name="updateStartDate" v-model="startDate" @input="onInput()">
<input type="text" name="updateStartDate" v-model="endDate" @input="onInput()">

methods: {
   onInput(e): function () {
      // this will be called on change of value
   }
}

Further to set the value and update the DOM simply update startDate and/or endDate variables and DOM will update accordingly.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to work with your model and not fiddle with the bound DOM element. You have bound the elements to viewmodel items:

<input type="text" name="updateStartDate" v-model="updateDateRange.start">
<input type="text" name="updateEndDate" v-model="updateDateRange.end">

then you use jQuery to set the field values

$('input[name="updateStartDate"]').val(start.format('MM/DD/YYYY'));
$('input[name="updateEndDate"]').val(end.format('MM/DD/YYYY'));

but you should be setting the bound values instead:

updateDateRange.start = start.format('MM/DD/YYYY');
updateDateRange.end = end.format('MM/DD/YYYY');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.