【问题标题】:Jquery FullCalendar- Change event color according to even_type in ruby appJquery FullCalendar-根据 ruby​​ 应用程序中的 even_type 更改事件颜色
【发布时间】:2012-08-03 06:52:07
【问题描述】:

我在我的 ruby​​ 应用程序中使用来自 http://arshaw.com/fullcalendar 的 Jquery 完整日历。我想为 4 种不同类型的事件(例如假期、学校、工作、娱乐)显示不同的颜色,这些事件在我的事件表中保存为 type_of_event

现在我只使用以下代码获取带有 start_at 和 end_at 的事件:

scope :before, lambda {|end_time| {:conditions => ["ends_at < ?", Event.format_date(end_time)] }}
  scope :after, lambda {|start_time| {:conditions => ["starts_at > ?", Event.format_date(start_time)] }}


  # need to override the json view to return what full_calendar is expecting.
  # http://arshaw.com/fullcalendar/docs/event_data/Event_Object/
  def as_json(options = {})
    {
      :id => self.id,
      :title => self.title,
      :description => self.description || "",
      :start => starts_at.rfc822,
      :end => ends_at.rfc822,
      :allDay => self.all_day,
      :recurring => false,
      :url => Rails.application.routes.url_helpers.event_path(id),
      #:color => "red"
    }

  end

  def self.format_date(date_time)
    Time.at(date_time.to_i).to_formatted_s(:db)
  end

是否有任何特定的方法可以根据 event_type 显示事件颜色意味着如果事件类型是学校,它将显示红色

【问题讨论】:

    标签: jquery ruby-on-rails fullcalendar


    【解决方案1】:

    您会在模型之外的其他地方使用这些颜色吗?

    很可能,您不需要使其全局可用,因此只需向模型添加一个常量:

    scope :before, ...
    scope :after, ...
    
    EVENT_COLORS = { "School" => "#ff0000", "Holidays" => "#00ff00", ... }
    EVENT_COLORS.default = "#0000ff" #optional step, set default color for events
    
    ...
    :url => Rails.application.routes.url_helpers.event_path(id),
    :color => EVENT_COLORS[self.event_type]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多