gpt4 book ai didi

css - 在 Rails 中计算日历一个月中的周数

转载 作者:行者123 更新时间:2023-11-28 08:14:14 25 4
gpt4 key购买 nike

我有一个显示当月公共(public)事件的日历。这是一个响应式布局,所以我需要能够计算 week_row 的高度。因此,我需要能够计算该月的周数,包括部分周(包含当月和上月/下月天数的周)。

以下方法计算一个月中的周数:

def weeks_in_month(date)
@week_count = (0.5 + (date.end_of_month.day + date.beginning_of_month.wday).to_f / 7.0).round
end

以及将适当的类添加到 <div> 的代码是:

def week_rows
weeks.map {|week|
content_tag :div, class: week_classes(week) do
week.map { |day| day_cell(day) }.join.html_safe
end
}.join.html_safe
end

def week_classes(date)
classes = ['week']
classes << "wk_rows_4" if weeks_in_month(date) == 4
classes << "wk_rows_5" if weeks_in_month(date) == 5
classes << "wk_rows_6" if weeks_in_month(date) == 6
classes.empty? ? nil : classes.join(" ")
end

但是,我错过了一些东西。除了默认的 'week' 之外,week_classes 没有填充任何东西。类。

这是基于 RailsCasts #213,根据 this tutorial 修改的关于创建没有表格的日历,这也大量借鉴了 RC#231。

知道为什么它没有将正确的类分配给 <div>

日历中的其他一切正常。完整代码如下:

module CalendarHelper
def calendar(date = Date.today, &block)
Calendar.new(self, date, block).calendar_div
end

class Calendar < Struct.new(:view, :date, :callback)
HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
START_DAY = :sunday

delegate :content_tag, to: :view

def calendar_div
content_tag 'div', id: "calendar" do
header + week_rows
end
end

def header
content_tag 'div', id: 'weekdays' do
HEADER.map { |day| content_tag :div, class: 'weekday' do
day
end }.join.html_safe
end
end

def week_rows
weeks.map {|week|
content_tag :div, class: week_classes(week) do
week.map { |day| day_cell(day) }.join.html_safe
end
}.join.html_safe
end

def week_classes(date)
classes = ['week']
classes << "wk_rows_4" if weeks_in_month(date) == 4
classes << "wk_rows_5" if weeks_in_month(date) == 5
classes << "wk_rows_6" if weeks_in_month(date) == 6
classes.empty? ? nil : classes.join(" ")
end

def day_cell(day)
content_tag :div, view.capture(day, &callback), class: day_classes(day)
end

def day_classes(day)
classes = ['day']
classes << "today" if day == Date.today
classes << "notmonth" if day.month != date.month
classes << "month" if day.month == date.month
classes.empty? ? nil : classes.join(" ")
end

def weeks
first = date.beginning_of_month.beginning_of_week(START_DAY)
last = date.end_of_month.end_of_week(START_DAY)
(first..last).to_a.in_groups_of(7)
end

def weeks_in_month(date)
@week_count = (0.5 + (date.end_of_month.day + date.beginning_of_month.wday).to_f / 7.0).round
end
end

最佳答案

好的,所以一些摆弄引导我找到这个解决方案:

  def week_rows
weeks.map {|week|
content_tag :div, class: week_classes do
week.map { |day| day_cell(day) }.join.html_safe
end
}.join.html_safe
end

def week_classes
classes = ['week']
classes << "wk_rows_4" if weeks_in_month == 4
classes << "wk_rows_5" if weeks_in_month == 5
classes << "wk_rows_6" if weeks_in_month == 6
classes.empty? ? nil : classes.join(" ")
end

def weeks_in_month
(0.5 + (date.end_of_month.day + date.at_beginning_of_month.wday).to_f / 7.0).round
end

没有必要将 (date) 作为参数传入。

关于css - 在 Rails 中计算日历一个月中的周数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29088565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com