I have my calendar being drawn out for the current month now i want to add Previous Month and Next Month this is how im making the Calendar in the view
<table border="1" width="70%">
<th colspan=7 align=center><%= @month %> <%= @year %></th>
<tr width ="10%">
<% Date::DAYNAMES.each do |days_of_week| %>
<td align=center><%= days_of_week %></td>
<% end %>
</tr>
<% @tasks.in_groups_of(7, ' ') do |row_tasks| %>
<tr>
<% for task in row_tasks %>
<!-- if Staament here -->
<td><%= task %></td>
<!-- Else Staament here -->
<% end %>
</tr>
<% end %>
</tr>
</table>
this is the Controller
def calendar_of_events
@year = Date.today.year
month = Date.today.month
@month = Date::MONTHNAMES[month]
begin_day = Date.today.beginning_of_month
@begin = begin_day
@last = Date.civil(@year, month, -1)
@blank_day= Date.today.beginning_of_month开发者_运维问答.wday
if @blank_day >= 0
@day = @blank_day - 1
else
@day = 0
end
@last_day_of_month = @last.mday
@first_day_of_month = @begin.mday
@blank_days_of_month = ('').to_a
for i in 0..@day
@blank_days_of_month2 = (' ').to_a
@blank_days_of_month1 = @blank_days_of_month
@blank_days_of_month = @blank_days_of_month2 + @blank_days_of_month1
end
@days_of_month = (@first_day_of_month..@last_day_of_month).to_a
@tasks = @blank_days_of_month + @days_of_month
end
I know there's alot of extra stuff i don't need i was using some of that to make sure i was getting the correct info. What i need is to click next and get next month and previous for the last month. All i need for now is getting the command to the controller i can worry about year + and year - after i get this working any help would be useful!
You can do that using Ajax if you want using any Javascript library out there. Have two links in your view and attach an function to the click event to those two. For example,
<a href="javascript:void(0);" class="left" id="prev"> </a>
<a href="javascript:void(0);" class="right" id="next"> </a>
If you are using Prototype JS for Javascript library:
document.observe("dom:loaded", function() {
$('prev').observe('click', function(event){
url = '/fetch?m=prev&d=' + d
new Ajax.Request(url, { method: 'get' });
...
});
$('next').observe('click', function(event){
url = '/fetch?m=next&d=' + d;
new Ajax.Request(url, { method: 'get' });
...
});
});
In the url we are building, d
can be the dates we want to pass to fetch the tasks for. You can have a fetch
action in your controller to serve this request. Add that to the routes to work.
Hope this helps.
精彩评论