简体   繁体   English

全日历:在周或天视图中检测点击

[英]Fullcalendar: detect click in week or day view

I'm currently detecting double click events in month view. 我目前在月份视图中检测到双击事件。 This works well: 这很好用:

dayRender: (date, element) => {
  element.bind('dblclick', ...)
}

What I'm looking for is a way to do the same when the user is in day view or week view. 我正在寻找的是一种在用户处于日视图或周视图时执行相同操作的方法。 I want to be able to detect a click event, and get the date and time they clicked on (note that time is relevant in day/week view, but not month view). 我希望能够检测到单击事件,并获取他们单击的日期和时间(请注意,时间与日/周视图有关,而与月视图无关)。

Any ideas? 有任何想法吗?

For anyone that finds this in the future, here's the hacky solution I came up with: 对于将来会发现此问题的任何人,这是我想出的hacky解决方案:

Because fullcalendar doesn't render a <div> per time slot (eg. a single div representing 1pm-1:30pm on 1/2/2015), and instead visually represents a single time slot as the intersection of a row and a column, we need a way to figure out the time from a given click. 因为fullcalendar不会在每个时隙中呈现<div> (例如,一个div代表在1/2/2015的1 pm-1:30pm),而是直观地将单个时隙表示为行和列的交集,我们需要一种方法来确定给定点击的时间。 Day info is already attached to each column (day) DOM element in the form of a data-date attribute. 天信息已经以data-date属性的形式附加到每个列(天)DOM元素上。 What I did was I forked fullcalendar, and added time info to each row (time slot) with a new data-time attribute . 我所做的是分叉了全日历,并使用新的data-time属性将时间信息添加到每行(时隙)。

Then, I listened on the double click event onViewRender, figured out the elements the user clicked on, and from those was able to derive both the date and the time slot: 然后,我侦听了双击事件onViewRender,找出了用户单击的元素,并从中得出了日期和时间段:

viewRender: (view, element) ->

  if view.type in ['agendaDay', 'agendaWeek']
    element.on 'dblclick', (event) ->

      els = document
        .elementsFromPoint event.pageX, event.pageY
        .filter (e) -> (e.hasAttribute 'data-date') or (e.hasAttribute 'data-time')
      date = (_.find els, (e) -> e.hasAttribute 'data-date').getAttribute 'data-date'
      time = (_.find els, (e) -> e.hasAttribute 'data-time').getAttribute 'data-time'

      return unless date and time

      startTime = moment "#{ date } #{ time }"
      endTime = (moment startTime).add 30, 'minutes' # clone + increment

This is a really hacky solution, and mixes all sorts of concerns. 这是一个非常棘手的解决方案,并且混合了各种各样的问题。 However, until fullcalendar adds support for a timeClick event, this works pretty well. 但是,在fullcalendar添加对timeClick事件的支持之前,这将非常有效。

Hope this helps someone else! 希望这对别人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM