Dynamically toggle resource column visibility

最后都变了- 提交于 2019-12-11 06:36:49

问题


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?


回答1:


There is an easy way to do this:

  1. Store resources in an array variable resourceData.
  2. Create another array called visibleResourceIds to store the ids of any resources you want to show.
  3. In the resources callback function, filter resourceData to only contain the resources where the resource id exists in visibleResourceIds. Return the filtered array and fullcalendar will only add the desired resources for you.

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);
  }
});



回答2:


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




回答3:


You can use the resourceColumns option for this. In the column objects you can set the width property to a number of pixels or a percentage. If you pass a function here you can easily handle the width property someplace else. Your hide/show function can then set the width to 0 to hide the column. After that you can trigger reinitView to update the view: $('#calendar').fullCalendar("reinitView");



来源:https://stackoverflow.com/questions/48427602/dynamically-toggle-resource-column-visibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!