Change :value in vc-date-picker > v-text-field using v-for

左心房为你撑大大i 提交于 2021-01-05 07:05:25

问题


I want :value to change from inputValue.start to inputValue.end automatically. So when I click on the end date it should be updated in the second text field. Likewise, if I select a date range in the second text field, the first text field should also be updated again. At the end of this question you can see the result of both cases.

P.S.: I use the V-Calendar plugin for Vue.js. The vc-date-picker element is from this plugin.

  • HTML:

<v-col v-for="(dateF, date_id) in datesF" :key="date_id + 'A'" cols="6" sm="3">
  <vc-date-picker v-model="range" color="red" is-range :input-debounce="500" :min-date="new Date()">
    <template v-slot="{ inputValue, inputEvents }">
      <v-text-field 
        :value="inputValue.start" 
        v-on="inputEvents.start" 
        :label="dateF.label"
      />                          
    </template>
  </vc-date-picker>
</v-col>
  • Script:

<script>
  export default {
    name: "Home",
    data: () => ({
      datesF: [
        { label: "Start Date", val: "start" },
        { label: "End Date", val: "end" },
      ],
      range: {
        start: new Date(),
        end: new Date(),
      },
    }),
  }; 
</script>

Result 1 Result 2

  • What I tried: :value="inputValue + '.' + dateF.val" but that wasn't the solution.

  • EDIT: Without v-for and twice v-text-field it works, but I want to use v-for. The reason is that otherwise I can't put the text fields in one line. No matter what I did, it didn't work. It only works with v-for, but for that I have to solve the main problem.

Temporary Solution (with v-for)

See answer of @Gurkan Ugurlu.

  • The problem of this temporary solution: No Live Update. See GIF.

For Live Update my initial code:

I just output the data for testing and it worked. Every time you select a date range, console.log is executed with the current dates.

watch: {
    range: {
      handler: function () {
        console.log(this.range.start.toLocaleDateString());
        console.log(this.range.end.toLocaleDateString());
      },
    },
},

回答1:


Can you try this? Hope it helps :)

<v-col v-for="(dateF, date_id) in datesF" :key="date_id + 'A'" cols="6" sm="3">
  <vc-date-picker v-model="range" color="red" is-range :input-debounce="500" :min-date="new Date()">
    <template v-slot="{ inputValue, inputEvents }">
      <v-text-field 
        :value="inputValue[dateF.val]" 
        v-on="inputEvents[dateF.val]" 
        :label="dateF.label"
      />                          
    </template>
  </vc-date-picker>
</v-col>


来源:https://stackoverflow.com/questions/65294969/change-value-in-vc-date-picker-v-text-field-using-v-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!