【问题标题】:Multiple checkbox select for Fullcalendar eventRenderFullcalendar eventRender 的多个复选框选择
【发布时间】:2015-11-09 15:38:44
【问题描述】:

在我的完整日历中,我有几个复选框,我想像过滤器一样做出反应。 简而言之:我要过滤一、全部、三、二(多)……等等。

我走了这么远,我可以选择一个复选框,它会返回正确的事件,但它当然不适用于两个或更多复选框。 但我不知道,如何做到这一点!我的Javascript技能不太好。

我使用模型中的 Ruby on Rails 4 创建的 Json 文件获取事件。但我不想拆分这个 Json。所以我在寻找一个完整的 Javascript 版本。

我认为,代码会更好地显示我的意思。

HTML:

<input id="hamburg" class="event_filter_box" name="event_filter_select" type="checkbox" value="Hamburg">Hamburg</input>
<input id="schleswig-holstein" class="event_filter_box" name="event_filter_select" type="checkbox" value="Schleswig-Holstein">Schleswig-Holstein</input>

<input id="party" class="event_filter_box" name="event_filter_select"  type="checkbox" value="Party">Party</input>
<input id="concert" class="event_filter_box" name="event_filter_select" type="checkbox" value="Concert">Concert</input>
<input id="festival" class="event_filter_box" name="event_filter_select" type="checkbox" value="Festival">Festival</input>

<div id="calendar"></div>

Javascript:

$(document).ready(function() {
  $('input[class=event_filter_box]').change(function() {
    $('#calendar').fullCalendar('rerenderEvents');
  });
});


$("#calendar").fullCalendar({
  events: [
    {
      title: 'Party Event in Hamburg',
      start: '2015-11-11',
      address_city: 'Hamburg',
      kind: 'Party',
    }, 
    {
      title: 'Conzert Event in Hamburg',
      start: '2015-11-10',
      address_city: 'Hamburg',
      kind: 'Concert',
    }, 
    {
      title: 'Festival Event in Schleswig-Holstein',
      start: '2015-11-08',
      end: '2015-11-12',
      address_city: 'Schleswig-Holstein',
      kind: 'Festival',
    }, 
  ],
  defaultView: "basicWeek",
  eventRender: function eventRender( event, element, view ) {
    if ($('input[id=hamburg]').is(':checked')) {
      return ['all', event.address_city].indexOf($('#hamburg').val()) >= 0
    } else if ($('input[id=schleswig-holstein]').is(':checked')) {
      return ['all', event.address_city].indexOf($('#schleswig-holstein').val()) >= 0
    } else if ($('input[id=party]').is(':checked')) {
      return ['all', event.kind].indexOf($('#party').val()) >= 0
    } else if ($('input[id=concert]').is(':checked')) {
      return ['all', event.kind].indexOf($('#concert').val()) >= 0
    } else if ($('input[id=festival]').is(':checked')) {
      return ['all', event.kind].indexOf($('#festival').val()) >= 0
    }
  },
});

codepen

对于多重选择,Javascript 应该如何?谢谢之前=)

【问题讨论】:

    标签: javascript json ruby-on-rails-4 fullcalendar


    【解决方案1】:

    小提琴演示:http://jsfiddle.net/gohrbk41/1/

    我稍微修改了 HTML,添加了数据类型标签以区分城市和种类

    <!-- I added data-type to the checkboxes -->
    <input id="hamburg" class="event_filter_box" name="event_filter_select" type="checkbox" value="Hamburg" data-type="address_city">Hamburg</input>
    <input id="schleswig-holstein" class="event_filter_box" name="event_filter_select" type="checkbox" value="Schleswig-Holstein" data-type="address_city">Schleswig-Holstein</input>
    
    <input id="party" class="event_filter_box" name="event_filter_select"  type="checkbox" value="Party" data-type="kind">Party</input>
    <input id="concert" class="event_filter_box" name="event_filter_select" type="checkbox" value="Concert" data-type="kind">Concert</input>
    <input id="festival" class="event_filter_box" name="event_filter_select" type="checkbox" value="Festival" data-type="kind">Festival</input>
    
    <div id="calendar"></div>
    

    对于 javascript

    $(document).ready(function() {
      $('input[class=event_filter_box]').change(function() {
        $('#calendar').fullCalendar('rerenderEvents');
      });
    });
    
    
    $("#calendar").fullCalendar({
      events: [
        {
          title: 'Party Event in Hamburg',
          start: '2015-11-11',
          address_city: 'Hamburg',
          kind: 'Party',
        }, 
        {
          title: 'Conzert Event in Hamburg',
          start: '2015-11-10',
          address_city: 'Hamburg',
          kind: 'Concert',
        }, 
        {
          title: 'Festival Event in Schleswig-Holstein',
          start: '2015-11-08',
          end: '2015-11-12',
          address_city: 'Schleswig-Holstein',
          kind: 'Festival',
        }, 
      ],
      defaultView: "basicWeek",
      eventRender: function eventRender( event, element, view ) {
          var display = true;
          var addresses = [];
          var kinds = [];
          // Find all checkbox that are event filters that are enabled
          // and save the values.
          $("input[name='event_filter_select']:checked").each(function () {
              // I specified data-type attribute in above HTML to differentiate
              // between locations and kinds of events.
    
              // Saving each type separately
              if ($(this).data('type') == 'address_city') {
                  addresses.push($(this).val());
              }
              else if ($(this).data('type') == 'kind') {
                  kinds.push($(this).val());
              }
          });
    
          // If there are locations to check
          if (addresses.length) {
              display = display && addresses.indexOf(event.address_city) >= 0;
          }
    
          // If there are specific types of events
          if (kinds.length) {
              display = display && kinds.indexOf(event.kind) >= 0;
          }
    
          return display;
      },
    });
    

    【讨论】:

    • 代码完美运行,谢谢 =) 我也理解代码。使用您的 Html,我可以轻松添加更多内容。
    猜你喜欢
    • 2014-10-07
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多