Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

javascript - 我如何使用node在我的postgresql中的数据填充全日历; 表达框架?(How can I populate my fullcalendar with the data in my postgresql using node; express framework?)

I want to load the data from my database into my full calendar, but I don't know to do it in JavaScript.

(我想将数据库中的数据加载到我的完整日历中,但是我不知道要用JavaScript来完成。)

Every code I find is for PHP, and since I'm trying to apply it to Node Express framework I don't get to display the data from the database into my full calendar.

(我发现的每个代码都是针对PHP的,并且由于我试图将其应用于Node Express框架,因此无法将数据库中的数据显示到完整的日历中。)

This is my script for my full calendar:

(这是我完整日历的脚本:)

<script type="text/javascript">

    var Fullcalendar = (function() {

      var $calendar = $('[data-toggle="calendar"]');

      function init($this) {

        var events = [

        {
         id: 1,
         title: 'Call with Dave',
         start: '2019-05-18',
         allDay: true,
         className: 'bg-red',
         description: 'Nullam id dolor id nibh ultricies vehicula ut id elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.'
       },

       ],

      // Full calendar options
    // For more options read the official docs: https://fullcalendar.io/docs


    options = {
      header: {
        right: '',
        center: '',
        left: 'title, today'
      },
      buttonIcons: {
        prev: 'calendar--prev',
        next: 'calendar--next'
      },
      defaultView: 'month',
      theme: false,
      selectable: true,
      selectHelper: true,
      editable: true,
      events: events,

      dayClick: function(date) {
        var isoDate = moment(date).toISOString();
        $('#new-event').modal('show');
        $('.new-event--title').val('');
        $('.new-event--start').val(isoDate);
        $('.new-event--end').val(isoDate);
      },

      viewRender: function(view) {
        var calendarDate = $this.fullCalendar('getDate');
        var calendarMonth = calendarDate.month();

        //Set data attribute for header. This is used to switch header images using css
        // $this.find('.fc-toolbar').attr('data-calendar-month', calendarMonth);

        //Set title in page header
        $('.fullcalendar-title').html(view.title);
      },

      // Edit calendar event action

      eventClick: function(event, element) {
        $('#edit-event input[value=' + event.className + ']').prop('checked', true);
        $('#edit-event').modal('show');
        $('.edit-event--id').val(event.id);
        $('.edit-event--title').val(event.title);
        $('.edit-event--description').val(event.description);
      }
    };

    // Initalize the calendar plugin
    $this.fullCalendar(options);


    //
    // Calendar actions
    //


    //Add new Event

    $('body').on('click', '.new-event--add', function() {
      var eventTitle = $('.new-event--title').val();

      // Generate ID
      var GenRandom = {
        Stored: [],
        Job: function() {
          var newId = Date.now().toString().substr(6); // or use any method that you want to achieve this string

          if (!this.Check(newId)) {
            this.Stored.push(newId);
            return newId;
          }
          return this.Job();
        },
        Check: function(id) {
          for (var i = 0; i < this.Stored.length; i++) {
            if (this.Stored[i] == id) return true;
          }
          return false;
        }
      };

      if (eventTitle != '') {
        $this.fullCalendar('renderEvent', {
          id: GenRandom.Job(),
          title: eventTitle,
          start: $('.new-event--start').val(),
          end: $('.new-event--end').val(),
          allDay: true,
          className: $('.event-tag input:checked').val()
        }, true);

        $('.new-event--form')[0].reset();
        $('.new-event--title').closest('.form-group').removeClass('has-danger');
        $('#new-event').modal('hide');
      } else {
        $('.new-event--title').closest('.form-group').addClass('has-danger');
        $('.new-event--title').focus();
      }
    });


    //Update/Delete an Event
    $('body').on('click', '[data-calendar]', function() {
      var calendarAction = $(this).data('calendar');
      var currentId = $('.edit-event--id').val();
      var currentTitle = $('.edit-event--title').val();
      var currentDesc = $('.edit-event--description').val();
      var currentClass = $('#edit-event .event-tag input:checked').val();
      var currentEvent = $this.fullCalendar('clientEvents', currentId);

      //Update
      if (calendarAction === 'update') {
        if (currentTitle != '') {
          currentEvent[0].title = currentTitle;
          currentEvent[0].description = currentDesc;
          currentEvent[0].className = [currentClass];

          console.log(currentClass);
          $this.fullCalendar('updateEvent', currentEvent[0]);
          $('#edit-event').modal('hide');
        } else {
          $('.edit-event--title').closest('.form-group').addClass('has-error');
          $('.edit-event--title').focus();
        }
      }

      //Delete
      if (calendarAction === 'delete') {
        $('#edit-event').modal('hide');

        // Show confirm dialog
        setTimeout(function() {
          swal({
            title: 'Are you sure?',
            text: "You won't be able to revert this!",
            type: 'warning',
            showCancelButton: true,
            buttonsStyling: false,
            confirmButtonClass: 'btn btn-danger',
            confirmButtonText: 'Yes, delete it!',
            cancelButtonClass: 'btn btn-secondary'
          }).then((result) => {
            if (result.value) {
              // Delete event
              $this.fullCalendar('removeEvents', currentId);

              // Show confirmation
              swal({
                title: 'Deleted!',
                text: 'The event has been deleted.',
                type: 'success',
                buttonsStyling: false,
                confirmButtonClass: 'btn btn-primary'
              });
            }
          })
        }, 200);
      }
    });


    //Calendar views switch
    $('body').on('click', '[data-calendar-view]', function(e) {
      e.preventDefault();

      $('[data-calendar-view]').removeClass('active');
      $(this).addClass('active');

      var calendarView = $(this).attr('data-calendar-view');
      $this.fullCalendar('changeView', calendarView);
    });


    //Calendar Next
    $('body').on('click', '.fullcalendar-btn-next', function(e) {
      e.preventDefault();
      $this.fullCalendar('next');
    });


    //Calendar Prev
    $('body').on('click', '.fullcalendar-btn-prev', function(e) {
      e.preventDefault();
      $this.fullCalendar('prev');
    });
  }


  //
  // Events
  //

  // Init
  if ($calendar.length) {
    init($calendar);
  }

})();

</script>

So I want to create a for loop to get the data values and replace the values of events from the one in the database

(所以我想创建一个for循环来获取数据值并替换数据库中事件的值)

var events = [

        {
         id: 1,
         title: 'Call with Dave',
         start: '2019-05-18',
         allDay: true,
         className: 'bg-red',
         description: 'Nullam id dolor id nibh ultricies vehicula ut id elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.'
       },

       ],

  ask by Gin Terez translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

have you tried

(你有没有尝试过)

<% if (event.length > 0){%>  <%event.forEach(function(item, index) { %>    events:   [
                    { // this object will be "parsed" into an Event Object
                    title: '<%= item.eventName%>',
                    groupId: 'blueEvents',
                    daysOfWeek: [ '5' ],
                    startTime: '10:45:00',
                    endTime: '12:45:00'


                }
            ]
 <% }); %>
<% }else {%> <p><strong>No event Found</strong><p>
    <% } %>

when using ejs and on node ..........your other code

(当使用ejs并在节点上时........您的其他代码)

res.render('calendar', {
            pageTitle: 'Calendar',
            event: event  //the result of the db query,

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...