"calendar.refetchEvents();" not refreshing events - php

I am using fullcalendar v4 (https://fullcalendar.io/) and I am trying to delete events when the event is clicked.
When I click on an event, I have this code to delete the event:
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
locale: 'pt',
plugins: [ 'interaction', 'dayGrid', 'timeGrid', 'list' ],
editable: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
},
// CUSTOM CODE DATABASE
events: load.php,
eventClick:function(clickedInfo)
{
if(confirm("Are you sure you want to delete this event?"))
{
var id = clickedInfo.event.id;
$.ajax({
url:"delete.php",
type:"POST",
data:{id:id},
success: function() {
alert('Deleted!');
calendar.refetchEvents();
}
})
}
},
});
calendar.render();
});
The event is getting deleted in the database, so the function works fine, but the events don't refresh and the deleted event is still showing in the calendar. It only disappears when I fully refresh the page, which is not ideal.
Any idea why?

It doesn't look like the calendar object is ever being initialized.
I would try changing your event listener as described in the documentation:
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
plugins: [ 'dayGrid' ]
});
calendar.render();
});
Ref. https://fullcalendar.io/docs/initialize-globals
I am assuming you are using script tags, but if you're using a build system (i.e. webpack), then you can try this: https://fullcalendar.io/docs/initialize-es6
For removing an event, I would just try this:
eventClick:function(clickedInfo)
{
if(confirm("Are you sure you want to delete this event?"))
{
var id = clickedInfo.event.id;
$.ajax({
url:"delete.php",
type:"POST",
data:{id:id},
success: function() {
alert('Deleted!');
//calendar.refetchEvents(); // remove this
clickedInfo.event.remove(); // try this instead
}
})
}
},
Ref. https://fullcalendar.io/docs/Event-remove

Related

Change HTML element into text from database using AJAX / JQuery according to ID

I'm new to Jquery and ran into a problem in web development. I want to convert html element to paragraph from database based on id from url.
Here my code for now,
<script>
var url = window.location.pathname;
var id = url.substring(url.lastIndexOf('/') + 1);
$(document).ready(function() {
$("#desc").click(function() {
$("#content").html(id);
$(this).addClass('active');
$("#feature").removeClass('active');
$("#spec").removeClass('active');
});
$("#feature").click(function() {
$("#content").html(id);
$(this).addClass('active');
$("#desc").removeClass('active');
$("#spec").removeClass('active');
});
$("#spec").click(function() {
$("#content").html(id);
$(this).addClass('active');
$("#desc").removeClass('active');
$("#feature").removeClass('active');
});
});
</script>
Here is the display that i wish
I've got the id, but don't know how to display data from the database (tbl_product) based on that id using AJAX / Jquery. I can only change the active class for now, all your advice and suggestions are will be very usefull.
you can use ajax like this
$("#desc").click(function() {
$(this).addClass('active');
$("#feature").removeClass('active');
$("#spec").removeClass('active');
$.ajax({
url: 'product.php?id=' + id
dataType: 'html',
contentType: false,
processData: false,
beforeSend: function() {
$('#content').empty();
},
error: function() {
$('#content').append('error');
},
success: function(data) {
$('#content').append(data);
}
})
});

how to display data from database on full calendar?

i have the following data
then i want to display in full calendar
<script>
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
headerToolbar: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek'
},
events: function(start, end, timezone, callback) {
$.ajax({
url: '<?php echo site_url("absentMonthYearController/absentYearController/calendarShow/" . $nik) ?>',
type: 'GET',
dataType: 'JSON',
success: function(data) {
var events = [];
if (data != null) {
$.each(data, function(i, item) {
events.push({
start: item.date,
title: 'Present',
display: 'background'
})
})
}
console.log('events', events);
}
})
}
});
calendar.render();
});
the data doesn't appear in the calendar, but it does appear in console.log
There are two issues here:
events: function(start, end, timezone, callback) { is wrong for fullCalendar 5. That looks like the syntax from an earlier version. https://fullcalendar.io/docs/v5/events-function shows the function's signature in v5, which is
function( fetchInfo, successCallback, failureCallback ) {
You forgot to use the provided callback to return the events to fullCalendar. So your function is downloading the events and logging them, but they never make it to the calendar.
Therefore this:
events: function( fetchInfo, successCallback, failureCallback ) {
$.ajax({
url: '<?php echo site_url("absentMonthYearController/absentYearController/calendarShow/" . $nik) ?>',
type: 'GET',
dataType: 'JSON',
success: function(data) {
var events = [];
if (data != null) {
$.each(data, function(i, item) {
events.push({
start: item.date,
title: 'Present',
display: 'background'
})
})
}
console.log('events', events);
successCallback(events);
}
})
}
should fix it.
P.S. Really you should also be using the start and end dates provided inside the fetchInfo object to send to your server, and the server should be using them to restrict the events it returns to only those which occur (or overlap) within those dates. That way you don't return a large number of events which mostly never get viewed, instead you only return what's relevant to the date range bein displayed.

Query String Value Is Not Working Inside The Javascript

Hello stackoverflow community, I am facing a problem where my query string isn't working and it's inside the javascript, I use isset and empty to test out results and it keeps saying failed. Here is the code:
events:'load.php?loadfor="$id"',
The code is inside a fullcalendar script, I want to load an event for specific person.Full code :
$(document).on('click', '.click_hire', function(){
var to_user_id = $(this).data('touserid');
$id = $(this).data('touserid');
var calendar = $('#calendar').fullCalendar({
nextDayThreshold: '23:59:00',
forceRerenderToDisplay: true,
disableDragging: true,
header:{
left:'prev,next today',
right:'title',
center:''
},
events:'load.php?loadfor="$id"',
selectable:true,
selectHelper:true,
select: function(start, end, allDay)
{
$("#popup").show();
var start = $.fullCalendar.formatDate(start, "YY-MM-DD HH:mm:ss");
var end = $.fullCalendar.formatDate(end, "YY-MM-DD HH:mm:ss");
$.ajax({
url:"get.popup.php",
method:"POST",
data:{start:start, end:end, to_user_id:to_user_id},
success:function(data){
$('#popup').html(data);
}
})
}
});
});
For the testing i put it on my get.popout.php.
if(isset($_GET['loadfor']))
{
echo "<h1>Success</h1>";
}
else if(empty($_GET['loadfor']))
{
echo "<h1>Failed</h1>";
}

Load calendar data to FullCalendar

I'm using FullCalendar in my Symfony project and I want to load events from database and show them in calendar for user.
According to documentation I have create a AJAX request to server on my calendar.html.twig page and store response in localStorage. This is what I done:
$(document).ready(function () {
var events = [];
$.ajax({
url: '{{ path('property.get.reservations') }}',
method: 'post',
data: {
'propertyId': {{ propertyId }}
}, success: function (data) {
console.log('running script within calendar.html.twig')
$.each(data, function (key, value) {
var event = JSON.stringify(value);
events.push(event);
});
localStorage.setItem('events', events);
}
});
});
Next I'm trying to use this storaged data in my calnedarService.js in this way:
$('#calendar').fullCalendar({
selectable: false,
header: {
left: 'prev,next',
center: 'title',
right: ''
},
editable: true,
events: localStorage.getItem('events'),
/* another code */
I get an error in the console:
No route found for "GET /admin/property/edit/1/%7B%22id%22:1,%22start%22:%222016-12-04T00:00:00.000Z%22,%22end%22:%222016-12-07T00:00:00.000Z%22%7D" (from "my_host")
So, for the reasons which I don't know,fullCalendar try to generate route, using parameters, getting from AJAX.
Any help will appreciated
Worked for me, just put this for url :
'admin/property/edit/'
with the id var at the end of the url

Ajax call error after opening infowindow

I'm using gmap3 for google map on my site.
I locate a marker after click on a button. Everyclick will give me different location depend on what a user chooses.
It goes smooth, however, when the user click on the marker to open the info window, and close it, the ajax call doesn't work. It gives me this error on chrome console, "Uncaught TypeError: Cannot call method 'lat' of undefined" in main.js.
EDIT: it seems I forgot to put autofit{} from my code, and it seems this was the problem. Anyone know how to user autofit in this case?
Here is my code
// create a map
$('#map_canvas').gmap3({
map: {
options: {
center: [-2.899153, 117.722626],
zoom: 4,
mapTypeId: google.maps.MapTypeId.SATELLITE
}
}
});
$('.locate-btn').on('click', function() {
var place_id = $('.place').val();
var address = $('.address').val();
$.ajax({
url: 'ajax.php',
type: 'post',
dataType: 'json',
data: 'place_id=' + place_id + '&address=' + address,
beforeSend: function() {
$('.loading').append("<img src='/images/loading.gif' id='loader' />");
},
success: function(place) {
$('#map_canvas').gmap3({
marker: {
latLng: [place.lat, place.lng],
data: 'nearest hotels<a class="btn btn-danger" href="#">about</a>',
events: {
click: function(marker, event, context) {
var map = $(this).gmap3('get'),
infowindow = $(this).gmap3({
get: {
name: 'infowindow'
}
});
if (infowindow) {
infowindow.close();
infowindow.open(map, marker);
infowindow.setContent(context.data);
} else {
$(this).gmap3({
infowindow: {
anchor: marker,
options: {
content: context.data
}
}
});
}
}
}
},
autofit: {}
});
}
});
});
});
Would anyone tell what's wrong?
Thank you.

Categories