Dynamically toggle resource column visibility


Dynamically toggle resource column visibility



I have a FullCalendar scheduler on a webapp which has 2 way databinding for resources and events, all working great. I want to be able to present the user with a dropdown that enables them to toggle the visibility of a column, ideally completely client side.



I have tried a combination of addResource / removeResource however my issue here is that a rerender of the calendar (e.g. when a new event is added) then displays the previously removed resource. I can work around this however would prefer a really simple approach using JS / CSS. I currently cannot find a way to set a resource to not be visible, or to have zero width - is this possible?





"a rerender of the calendar (e.g. when a new event is added) then displays the previously removed resource". Can you show code which reproduces this? I can't make it do that - see demo at jsfiddle.net/toytd26b/10 - click the "remove resource" button to delete a resource using fullcalendar's removeResource, then drag on the calendar to add an event. The removed resource stays removed. Is your page posting back when creating an event, or something?
– ADyson
Jan 26 at 7:32





2 Answers
2



There is an easy way to do this:



To remove a resource from view, simply remove the resource id from visibleResourceIds and refetchResources. To add the resource back in, add the id to visibleResourceIds and refetchResources. DONE.



JSFiddle


var resourceData = [
{id: "1", title: "R1"},
{id: "2", title: "R2"},
{id: "3", title: "R3"}
];

var visibleResourceIds = ["1", "2", "3"];

// Your button/dropdown will trigger this function. Feed it resourceId.
function toggleResource(resourceId) {
var index = visibleResourceIds.indexOf(resourceId);
if (index !== -1) {
visibleResourceIds.splice(index, 1);
} else {
visibleResourceIds.push(resourceId);
}
$('#calendar').fullCalendar('refetchResources');
}

$('#calendar').fullCalendar({
defaultView: 'agendaDay',

resources: function(callback) {
// Filter resources by whether their id is in visibleResourceIds.
var filteredResources = ;
filteredResources = resourceData.filter(function(x) {
return visibleResourceIds.indexOf(x.id) !== -1;
});
callback(filteredResources);
}
});



I had the same challenge. Instead of a dropdown, I use checkboxes, but the workings will be the same.



My resources are stored in a variable, when I uncheck a box, the resource is removed and the resource's object is added to another array with the resourceId as key, and the index added to the object to restore the object in the same column as it originally was. When re-checking the box, the object is added to the resources array and the resources refetched.


/*retireve the resources from the server */
var planningResources;
var removedResource = ;

$.ajax({
url: '/planning/resources/',
method: 'get',
success: function (response) {
planningResources = response;
showCalendar();
}
, error: function () {
if (typeof console == "object") {
console.log(xhr.status + "," + xhr.responseText + "," + textStatus + "," + error);
}
}
});

/* create the calendar */
showCalendar = function () {
$('#calendar').fullCalendar({
...
});
}

/* checkbox on click */
$('.resource').click(function() {
var resourceId = $(this).val();
var hideResource = !$(this)[0].checked;
$('.status:checkbox:checked').each(function () {
});
if(hideResource) {
$.each(planningResources, function(index, value){
if( value && value.id == resourceId ) {
value.ndx = index;
removedResource[resourceId] = value;
planningResources.splice(index,1);
return false;
}
});
$('#planningoverview').fullCalendar(
'removeResource',
resourceId
);
}
else {
planningResources.splice(removedResource[resourceId].ndx, 0, removedResource[resourceId]);
$('#planningoverview').fullCalendar('refetchResources');
}
});

showCalendar();



It probably doesn't get first price in a beauty contest, but it works for me ...



Cheers






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter