简体   繁体   English

FullCalendar v4 - 如何在日历中动态添加非标准字段?

[英]FullCalendar v4 - How to dynamically add a non-standard field in the calendar?

I added a non-standard field in the modal / form (Bootstrap) to add it to the calendar event.我在模态/表单(Bootstrap)中添加了一个非标准字段以将其添加到日历事件中。 I take the content of the textarea and save it in a variable (description) but I can't print it in the calendar.我获取文本区域的内容并将其保存在变量(描述)中,但我无法在日历中打印它。 I was researching and testing with eventRender but it didn't work.我正在使用eventRender进行研究和测试,但它没有用。 What do you suggest?你有什么建议?

      // FULLCALENDAR
      document.addEventListener('DOMContentLoaded', function () {
        // Submit formulario
        let enviar = document.querySelector("#guardar"); 
        let calendarEl = document.getElementById('calendar');

        let calendar = new FullCalendar.Calendar(calendarEl, {
          locale: "es",
          plugins: ["interaction", "dayGrid", "timeGrid"],
          selectable: true,
          defaultView: "timeGridWeek",
          header: {
            left: "prev,next today",
            center: "title",
            right: "timeGridDay,timeGridWeek,dayGridMonth",
          },

          // Selecciona con click en una fecha/hora determinada
          dateClick: function (info) {
            let infoDate = info.dateStr.substring(0,10)
            let infoTime = info.dateStr.substring(11,19)

            // Toma el valor donde clikea y lo agrega al value de HTML
            document.querySelector("#fecha").setAttribute("value", infoDate)
            document.querySelector("#hora").setAttribute("value", infoTime)

            // Acciona el modal
            $("#modalEventos").modal();
          },
        });

        calendar.render();

        // EVENT LISTENER
        enviar.addEventListener("submit", enviarEvento)

        // FUNCTIONS
        // Toma los datos del evento en el modal 
        function enviarEvento() {
          let titulo = document.querySelector("#titulo").value;
          let descripcion = document.querySelector("#descripcion").value;
          let fecha = document.querySelector("#fecha").value;
          let hora = document.querySelector("#hora").value;
          let color = document.querySelector("#color").value;

          // Imprime los datos en el calendario
          calendar.addEvent({    
            title: titulo, 
            description: descripcion,
            start: fecha + "T" + hora,
            color: color
          });

          //Cierra el modal
          $('#modalEventos').modal('toggle'); 
        }
      });

Full calendar < 4.x完整日历 < 4.x

According to docs , addEvent was removed in v1.3 .根据文档addEventv1.3中被删除。

The alternative syntax is:替代语法是:

calendar.fullCalendar('renderEvent', {    
  // event props here...
}, true);

If you need more help, please create a minimal reproducible example to allow testing potential solutions.如果您需要更多帮助,请创建一个最小的可重现示例以允许测试潜在的解决方案。

Here's a codepen where you can see the above syntax working (line 16 in JS).这是一个代码笔,您可以在其中看到上述语法(JS 中的第16行)。
To test: click any day, add details, "Save".要测试:单击任何一天,添加详细信息,“保存”。


Full calendar 4.x and above:完整日历 4.x 及更高版本:

According to documentation , every non-standard property passed to the event object and any property passed to event.extendedProps will all be merged inside the same object, called extendedProps which means both department and foo in this example will be found in event.extendedProps after parsing:根据文档,传递给event object 的每个非标准属性和传递给event.extendedProps的任何属性都将合并到同一个 object 中,称为extendedProps ,这意味着本例中的departmentfoo都将在之后的event.extendedProps中找到解析:

calendar.addEvent({
  title: 'BCH237',
  start: '2019-08-12T10:30:00',
  end: '2019-08-12T11:30:00',
  department: 'BioChemistry',
  extendedProps: {
    foo: 'bar'
  },
  description: 'Lecture'
})
... 
eventRender(info) {
  console.log(info.event.extendedProps.department) // 'BioChemistry'
  console.log(info.event.extendedProps.foo) // 'bar'
  console.log(info.event.description) // 'Lecture'
}

See it working here .看到它在这里工作

Thanks for the reply @tao.感谢@tao 的回复。 renderEvent is only available until version 3 and I'm using v 4. So I use calendar.addEvent which is available in version 4 and 5 (beta). renderEvent 仅在版本 3 之前可用,并且我使用的是 v 4。所以我使用在版本 4 和 5(测试版)中可用的calendar.addEvent (I think it is not the same as addEvent, to which you refer) I am a newbie to javascrip so I keep trying a solution and watch for suggestions, Seeks to fix it in JavaScript. (我认为它与您所指的 addEvent 不同)我是 javascrip 的新手,所以我一直在尝试解决方案并观察建议,寻求在 JavaScript 中修复它。 avoiding jQuery.避免使用 jQuery。

FullCalendar gives me this reference but when I try to do it dynamically I can't FullCalendar 给了我这个参考但是当我尝试动态地做它时我不能

                    var calendar = new Calendar(calendarEl, {
          events: [
            {
              title: 'BCH237',
              start: '2019-08-12T10:30:00',
              end: '2019-08-12T11:30:00',
              extendedProps: {
                department: 'BioChemistry'
              },
              description: 'Lecture'
            }
            // more events ...
          ],
          eventRender: function (info) {
            console.log(info.event.extendedProps);
            // {description: "Lecture", department: "BioChemistry"}
          }

        });

For a version 5 try this.对于版本 5,试试这个。

           calendar.addEvent({
                title: $('#txtTituloEvento').val(),
                start: $('#txtInicio').val(),
                end: $('#txtFin').val(),
                extendedProps: {
                    descripcion: $('#txtDescripcion').val()
                },
                allDay: false,
            })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM