Javascript 完整日历用户事件
我累坏了。我有基本的用户模式的完整日历。我想添加两个按钮,用户可以将日历分成两个。首先,他只获取自己的事件,然后在几秒钟内获取所有用户的所有事件。我该怎么做?我是rails的新手:) 这是我的js代码Javascript 完整日历用户事件,javascript,jquery,ruby-on-rails,fullcalendar,Javascript,Jquery,Ruby On Rails,Fullcalendar,我累坏了。我有基本的用户模式的完整日历。我想添加两个按钮,用户可以将日历分成两个。首先,他只获取自己的事件,然后在几秒钟内获取所有用户的所有事件。我该怎么做?我是rails的新手:) 这是我的js代码 $(document).ready -> $("#calendar").fullCalendar( events: '/events.json' eventColor: '#378006' eventBackgroundColor: 'red' ) 和我
$(document).ready ->
$("#calendar").fullCalendar(
events: '/events.json'
eventColor: '#378006'
eventBackgroundColor: 'red'
)
和我的json事件文件
json.array!(@events) do |event|
json.extract! event, :id, :title, :description, :user_id
json.start event.start_time
json.end event.end_time
json.url event_url(event, format: :html)
if (event.user_id == current_user.id)
json.color 'green'
end
end
为此,您可以这样做:
$(document).on 'change', 'your_checkobx_id_to_select_user_events ', ->
if @checked
calEvents = 'events/user_events.json'
return
$(document).on 'change', 'your_checkobx_id_to_select_all_events', ->
if @checked
calEvents = 'events.json'
return
1)在视图上添加两个名为“用户事件”和“所有用户事件”的按钮
2)现在在js中设置一个名为calEvents的全局变量
3)现在使用按钮单击事件,这意味着当用户单击“用户事件”按钮时,将calEvents的值设置为等于当前用户事件,如果用户单击“所有用户事件”,则将calEvents的值更改为等于所有用户事件
注意:确保事件的格式为json
要实现这一点,您可以编写如下代码:
calEvents = ''
$(document).on 'click', 'all_user_events', ->
calEvents = '/events.json'
return
$(document).on 'click', 'user_events', ->
calEvents = '/events/user_events.json'
return
$(document).ready ->
calEvents = '/events.json'
$("#calendar").fullCalendar(
events: calEvents
eventColor: '#378006'
eventBackgroundColor: 'red'
)
在您的事件\u控制器中添加新操作
def user_events
@events = current_user.events
render json: @events
end
在您的配置/routs.rb文件中
get 'events/user_events' => 'events#user_events'
编辑:
如果要通过复选框执行此操作,可以执行以下操作:
$(document).on 'change', 'your_checkobx_id_to_select_user_events ', ->
if @checked
calEvents = 'events/user_events.json'
return
$(document).on 'change', 'your_checkobx_id_to_select_all_events', ->
if @checked
calEvents = 'events.json'
return
希望我以正确的方式理解了您的问题,这将对您有所帮助。不使用按钮是否可以解决此问题?我想用复选框来做是的,你可以用复选框来做。你能帮我吗?)我想这并不难