最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I create an event form to use it with Fullcalendar - Stack Overflow

programmeradmin13浏览0评论

I feel lost. I've been trying to get the title from form and update the calendar. Can someone help me to create an event form? I don't have enough experience with JQuery. First I thought adding eventlistener for button would solve the problem. But it didn't.

HTML script:

<!DOCTYPE html>
<html>
 <head>
  {% from "formhelpers.html" import render_field %}
  <title> Calendar </title>
  <link rel="stylesheet" href=".4.0/fullcalendar.css" />
  <link rel="stylesheet" href=".3.6/css/bootstrap.min.css">
  <script src=".12.4/jquery.min.js"></script>
  <script src=".3.6/js/bootstrap.min.js"></script>

  <script src=".12.1/jquery-ui.min.js"></script>
  <script src=".js/2.18.1/moment.min.js"></script>
  <script src=".4.0/fullcalendar.min.js"></script>
  <script>
  $(document).ready(function() {
   var calendar = $('#calendar').fullCalendar({
    editable:true,
    header:{
     left:'prev,next today',
     center:'title',
     right:'month,agendaWeek,agendaDay'
    },
    events: [{% for row in calendar %}{ id : '{{row.id}}', title : '{{row.title}}', start : '{{row.start_event}}', end : '{{row.end_event}}', }, {% endfor %}],
    selectable:true,
    selectHelper:true,
    select: function(start, end, allDay)
    {
     document.getElementById("eventblock").style.display="block"
     var title = prompt("Enter Event Title");
     if(title)
     {
      var start = $.fullCalendar.formatDate(start, "Y-MM-DD HH:mm:ss");
      var end = $.fullCalendar.formatDate(end, "Y-MM-DD HH:mm:ss");
      $.ajax({
       url:"/insert",
       type:"POST",
       data:{title:title, start:start, end:end},
       success:function(data)
       {
         //alert(data)
        alert("Added Successfully");
        window.location.replace("/");
       }
      })
     }
    },
    editable:true,
    eventResize:function(event)
    {
     var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD HH:mm:ss");
     var end = $.fullCalendar.formatDate(event.end, "Y-MM-DD HH:mm:ss");
     var title = event.title;
     var id = event.id;
     $.ajax({
      url:"/update",
      type:"POST",
      data:{title:title, start:start, end:end, id:id},
      success:function(){
       calendar.fullCalendar('refetchEvents');
       alert('Event Update');
      }
     })
    },

    eventDrop:function(event)
    {
     var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD HH:mm:ss");
     var end = $.fullCalendar.formatDate(event.end, "Y-MM-DD HH:mm:ss");
     var title = event.title;
     var id = event.id;
     $.ajax({
      url:"/update",
      type:"POST",
      data:{title:title, start:start, end:end, id:id},
      success:function()
      {
       calendar.fullCalendar('refetchEvents');
       alert("Event Updated");
      }
     });
    },

    eventClick:function(event)
    {
     if(confirm("Are you sure you want to remove it?"))
     {
      var id = event.id;
      $.ajax({
       url:"/ajax_delete",
       type:"POST",
       data:{id:id},
       success:function()
       {
        calendar.fullCalendar('refetchEvents');
        alert("Event Removed");
       }
      })
     }
    },

   });
  });

  </script>
 </head>
 <body>
  <br />
  <h2 align="center"><a href="#"> Calendar </a></h2>
  <br />

  <div class="row" style="">
   <div class="column" id='calendar' style="float: left; width: 40%; padding: 5px;"></div>
   <div class="column" id='eventblock' style="background-color: yellow; float: right; width: 40; padding: 5px;">
    <div style="margin-left: 60px;" >
    <form method=post>
      <dl>
       <dt id="titleForm">{{ render_field(form.title) }} </dt>
      </dl>
      <button type="submit">Submit</button>
    </form>
</div>
   </div>

 </div>

 </body>
</html>

Flask script:

from flask import Flask, render_template, request, jsonify
from flask_mysqldb import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField, validators

app = Flask(__name__)
app.secret_key = "waytoosecret"

app.config["MYSQL_HOST"] = "localhost"
app.config["MYSQL_USER"] = "root"
app.config["MYSQL_PASSWORD"] = ""
app.config["MYSQL_DB"] = "calendar"
app.config["MYSQL_CURSORCLASS"] = "DictCursor"

db = MySQL(app)

class EventBlockForm(Form):
    title = StringField("Title")
    #description = StringField("")

@app.route('/', methods=["POST", "GET"])
def index():
    form = EventBlockForm(request.form)
    cur = db.connection.cursor()
    cur.execute("SELECT * FROM events ORDER BY id")
    calendar = cur.fetchall()
    return render_template('index.html', calendar=calendar, form = form)


@app.route("/insert", methods=["POST", "GET"])
def insert():
    cur = db.connection.cursor()
    form = EventBlockForm(request.form)

    if request.method == 'POST':

        #title = request.form['title']
        title = form.title.data
        #desc = form.description.data


        start = request.form['start']
        end = request.form['end']
        print(title)
        print(start)
        cur.execute("INSERT INTO events (title,start_event,end_event) VALUES (%s,%s,%s)", [title, start, end])
        db.connectionmit()
        cur.close()
        msg = 'success'
    return jsonify(msg)


@app.route("/update", methods=["POST", "GET"])
def update():
    cur = db.connection.cursor()
    if request.method == 'POST':
        title = request.form['title']
        start = request.form['start']
        end = request.form['end']
        id = request.form['id']
        print(start)
        print(end)
        cur.execute("UPDATE events SET title = %s, start_event = %s, end_event = %s WHERE id = %s ",
                    [title, start, end, id])
        db.connectionmit()
        cur.close()
        msg = 'success'
    return jsonify(msg)


@app.route("/ajax_delete", methods=["POST", "GET"])
def ajax_delete():
    cur = db.connection.cursor()
    if request.method == 'POST':
        getid = request.form['id']
        print(getid)
        cur.execute('DELETE FROM events WHERE id = {0}'.format(getid))
        db.connectionmit()
        cur.close()
        msg = 'Record deleted successfully'
    return jsonify(msg)


if __name__ == "__main__":
    app.run(debug=True)

I've tried to add listener to get title info from form. Then checked a lot of posts on here.

发布评论

评论列表(0)

  1. 暂无评论