【问题标题】:How to update vForm input value如何更新 vForm 输入值
【发布时间】:2020-05-18 07:46:28
【问题描述】:

我正在使用 vFormFullCalendar,并且我正在使用引导模式来编辑日历上的事件。当我单击日历上的事件时,它会打开带有表单的模式并且它是输入的,并且我从 Fullcalendar eventclick() 方法将数据发送到 vForm 但它没有显示在表单中并且表单输入是空的。这是我迄今为止尝试过的,但它们都不起作用,我添加了 cmets 以使事情更清楚:

失败 1:

<script>
import "fullcalendar/dist/fullcalendar.min.js";
import "fullcalendar/dist/fullcalendar.min.css";
import { Form, HasError, AlertError } from "vform";

Vue.component(HasError.name, HasError);
Vue.component(AlertError.name, AlertError);

var editTitle = "";
var editStart = "";
var editEnd = "";
var editdesc = "";

export default {
  props: ["workingHours"],
  mounted() {
    const date = new Date();
    const d = date.getDate();
    const m = date.getMonth();
    const y = date.getFullYear();

    const events = this.workingHours;

    $("#full-calendar").fullCalendar({
      events,
      height: 800,
      header: {
        left: "month,agendaWeek,agendaDay",
        center: "title",
        right: "today prev,next"
      },
      eventClick: function(calEvent, jsEvent, view) {
        editStart = calEvent.start.format("MM/DD/YYYY, h:mm a");
        editEnd = calEvent.end.format("MM/DD/YYYY, h:mm a");
        editTitle = calEvent.title;
        editdesc = calEvent.desc;
        var xpos = jsEvent.pageX;
        var ypos = jsEvent.pageY;
        $("#calendar-edit").modal("show");
        console.log(editTitle); //at this stage the editTitle variable is updated with the new value but down in the form the title is not updated with the new value

        return false;
      }
    });
  },
  data() {
    return {
      // Create a new form instance
      form: new Form({
        title: editTitle, //here the value is not updated with the new value so the input in the form is empty
        start: editStart,
        end: editEnd,
        desc: editdesc
      })
    };
  },

  methods: {
    updateEvent() {
      // Submit the form via a PUT request
      this.form.put("/api/workinghours/update/" + 1).then(({ data }) => { //don't worry about the plus one i'm using it for testing purpose
        console.log(data);
      });
    }
  }
};
</script>

失败 2:

<script>
import "fullcalendar/dist/fullcalendar.min.js";
import "fullcalendar/dist/fullcalendar.min.css";
import { Form, HasError, AlertError } from "vform";

Vue.component(HasError.name, HasError);
Vue.component(AlertError.name, AlertError);

export default {
  props: ["workingHours"],
  mounted() {
    const date = new Date();
    const d = date.getDate();
    const m = date.getMonth();
    const y = date.getFullYear();

    const events = this.workingHours;

    $("#full-calendar").fullCalendar({
      events,
      height: 800,
      header: {
        left: "month,agendaWeek,agendaDay",
        center: "title",
        right: "today prev,next"
      },
      eventClick: function(calEvent, jsEvent, view) {
        this.form.start = calEvent.start.format("MM/DD/YYYY, h:mm a");
        this.form.end = calEvent.end.format("MM/DD/YYYY, h:mm a");
        this.form.title = calEvent.title; //So for this I'm using the Vform's API to update the values but it gives error that the title is not defined
        this.form.desc = calEvent.desc;
        var xpos = jsEvent.pageX;
        var ypos = jsEvent.pageY;
        $("#calendar-edit").modal("show");
        return false;
      }
    });
  },
  data() {
    return {
      // Create a new form instance
      form: new Form({
        title: "", //here the value  still is not updated with the new value so the input in the form is empty
        start: "",
        end: "",
        desc: ""
      })
    };
  },

  methods: {
    updateEvent() {
      // Submit the form via a PUT request
      this.form.put("/api/workinghours/update/" + 1).then(({ data }) => { //don't worry about the plus one i'm using it for testing purpose
        console.log(data);
      });
    }
  }
};
</script>

【问题讨论】:

  • 您使用旧版本的fullCalendar,并且没有使用fullCalendar现成的Vue组件的任何具体原因?见fullcalendar.io/docs/vue。我不是 vue 专家,但我不禁想到它可能会使您的集成更容易。
  • 是的,它是旧版本,它是第 3 版。第 4 版是最新的,第 5 版即将推出。据我所知,不会再发布 3 的更新或修复。所以现在不是开始使用它的好时机。由于它只是在您的页面中定义为脚本,因此我认为您可以轻松地将其替换为较新的版本。
  • 是的,但如果你还记得的话,我的想法是使用 vue 组件来表示日历可能有助于集成。我不知道 vue,但您似乎在变量范围或时间如何/何时将值收集到表单对象中存在问题。
  • 您确实可以访问所有内容。没有缺少的功能。 dateClick 仅作为示例给出。但是您可以以同样的方式使用 eventClick。 (注意 eventClick 不是 eventObject!)。
  • 你检查v4 documentation for eventClick了吗?还是v3-v4 upgrade guide?函数签名已更改。现在函数中只有一个传入对象,其中包含所有不同的项目作为属性。 (即使它没有改变,将整个事件对象分配给您的标题字段也是没有意义的,就像您的代码试图做的那样。)。

标签: javascript jquery vue.js fullcalendar fullcalendar-3


【解决方案1】:

所以在@ADyson 的帮助下,感谢他,我一直在使用旧版本并更新到最新版本,并将 Fullcalendar 库更改为 VUE 版本。所以现在触发的事件点击正在改变 vForm 的输入值,我将在下面添加整个代码,尝试添加 cmets。现在唯一的问题是除了使用 vForm 和 Fullcalendar 库我正在使用vue-datetime 并且当将值发送到表单时,组件输入值为空我认为这个问题与javascript中的日期时间格式更相关,所以我欺骗了在日期时间组件上添加徽章以显示旧值。我知道这不是正确的做法,但现在它很好

<script>
import FullCalendar from "@fullcalendar/vue";
import dayGridPlugin from "@fullcalendar/daygrid";
import timeGridPlugin from "@fullcalendar/timegrid";
import interactionPlugin from "@fullcalendar/interaction";

var eventID = 0;
export default {
  props: ["workingHours"],
  components: {
    FullCalendar // make the <FullCalendar> tag available
  },
  data: function() {
    return {
      calendarPlugins: [
        // plugins must be defined in the JS
        dayGridPlugin,
        timeGridPlugin,
        interactionPlugin // needed for dateClick
      ],
      calendarWeekends: true,
      form: new Form({
        title: "",
        start: "",
        end: "",
        desc: ""
      })
    };
  },
  methods: {
    handleDateClick(info) {
        eventID = info.event.id;
        const startTime = moment(info.event.start).format('yyyy-MM-DD hh:MM:SS');
        const endTime = moment(info.event.end).format('yyyy-MM-DD hh:MM:SS');
        this.form.title = info.event.title;
        this.form.start = startTime;
        this.form.end = endTime;
        this.form.desc = info.event.extendedProps.desc;
        $("#disabledStart").html(startTime);
        $("#disabledEnd").html(endTime);
        $("#calendar-edit").modal("show");
        console.log(info.event.start);
        console.log(startTime);
        console.log(info.event.id);
    },
    updateEvent() {
      // Submit the form via a PUT request
      this.form.put("/api/workinghours/update/" + eventID).then(({ data }) => {
        console.log(data);
        $("#calendar-edit").modal("hide");
      });
    }
  }
};
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2017-01-30
    • 1970-01-01
    • 2023-03-15
    • 2020-04-07
    • 2013-12-29
    相关资源
    最近更新 更多