I am trying to get a bar chart to show data from a table. The table is called notes and I want to be able to show the total notes created by users on each day over a period of time. A basic chart.
I followed the railscast tutorial on this.
The chart interacts with the data but I get crazy numbers.
<script>
$(function() {
new Highcharts.Chart({
chart: {
renderTo: "notes_chart",
type: "column"
},
title: {
text: "Notes by Day"
},
xAxis: {
type: "datetime"
},
yAxis: {
title: {
text: "Notes",
}
},
tooltip: {
formatter: function() {
return Highcharts.dateFormat("%B %e, %Y", this.x) + ':' +
"Number of Notes:" + Highcharts.numberFormat(this.y, 0);
}
},
series: [{
pointInterval: <%= 1.day * 1000 %>,
pointStart: <%= 9.weeks.ago.to_i * 1000 %>,
data: <%= (9.weeks.ago.to_date..Date.today).map { |date| Note.created_at(date)}.inspect %>
}]
});
});
</script>
Note.rb
def self.created_at(date)
where("date(created_at) = ?",date).sum(:created_at)
end
The y axis show the number of notes in 1,000's (don't know why). When I create a new note it shows I have created 2,000 and something notes. I just want it to show how many notes were created on such a date.
Would appreciate any guidance. Perhaps I am going about it totally the wrong way with the created_at column?
Here is my table
<table class="table table-striped border table-bordered table-hover success panel-body" id="datatable">
<thead>
<tr class="tableheaders danger">
<th>Created at</th>
<th>Updated at</th>
<th>Teacher ID</th>
<th>Name</th>
<th><%= sort_link(#q, :email) %></th>
<th><%= sort_link(#q, :Student_Alias) %></th>
<th><%= sort_link(#q, :subject_type) %></th>
<th>Grievance</th>
<th>Penalty</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #notes.each do |note| %>
<tr>
<td><%= note.created_at %></td>
<td><%= note.updated_at.localtime %></td>
<td><%= note.user_id %></td>
<td><%= note.user.full_name %></td>
<td><%= note.user.email %></td>
<td><%= note.studentname %></td>
<td><%= note.subject_type %></td>
<td><%= note.grievance %></td>
<td><%= note.penalty %></td>
<td><%= link_to 'Show', note, class: 'btn btn-primary btn-xs' %></td>
<% if note.user==current_user %>
<td><%= link_to 'Edit', edit_note_path(note), class: 'btn btn-info btn-xs' %></td>
<% end %>
<% if current_user.admin %>
<td><%= link_to 'Destroy', note, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger btn-xs' %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
Image:
I just want to create a simple bar or line chart that shows the number of notes on each day over a given period. Hope someone can help. Thanks.
Related
I want to Generate Automatic Serial number for my Registered Admins
<tbody>
<% Admins.forEach(function (Admin){ %>
<tr>
<td>//S.No Here Starts from 1 //</td>
<td><%= Admin.First_Name %></td>
<td><%= Admin.Last_Name %></td>
<td><%= Admin.User_Name %></td>
<td><%= Admin.Email %></td>
<td><%= Admin.Password %></td>
<td><%= Admin.Contact %></td>
</tr>
<% }) %>
forEach passes the index as the second argument. It's zero-based so you'll need to add 1:
<% Admins.forEach(function (Admin, index) { %>
<tr>
<td><%= index + 1 %></td>
I have one looping Dates and table content.
I want to display table content based on Dates. How's it possible please help me.
index.html.erb
<% #batches.each do |batch| %>
<tr>
**<td class = "pie" id ="demo"> <i class="fa fa-plus-circle"></i>
<%= batch.date.strftime("%Y-%m-%d") if !batch.date.nil? %></td>**
<td><%= batch.purchase_center.name.to_s if !batch.purchase_center.nil? %></td>
<td><%= batch.plant.name.to_s if !batch.plant.nil?%></td>
<td><%= batch.species.name.to_s if !batch.species.nil? %></td>
<td><%= batch.batch_number %></td>
<td><%= batch.total_quantity %></td>
<td><%= batch.calculated_yeild %></td>
<td><%= link_to '<i class="fa fa-users"></i>'.html_safe, "#", :class => "addsupplier", :id => "#{batch.serial_id}" %></td><td><%= link_to '<i class="fa fa-trash-o"></i>'.html_safe, batch, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<tr class="<%= batch.serial_id %>" style="display:none">
<%= render :partial => "batch_counts/form", locals: {id: "#{batch.serial_id}"} %>
</tr>
**<td id ="two" style="display:">
Microsoft Corporation
Microsoft Corporation
</td>**
<% end %>
<script type="text/javascript">
$('#demo').click(function(){
$('#two').toggle();
});
</script>
I highlighted with star marks i.e. id = demo and id =two.
i have different dates and different message. So i want to display it on which date is having which message.
Based on dates display message.
Give a common class to each of them. Say, date-alert:
<td class="pie date-alert" id="demo"> <i class="fa fa-plus-circle"></i>
<%= batch.date.strftime("%Y-%m-%d") if !batch.date.nil? %></td>
<td id="two" style="display:" class="date-alert">
Microsoft Corporation
Microsoft Corporation
</td>
And then upon clicking it, you can execute like this:
$(".date-alert").click(function () {
// the current td is this
});
I have a Rails 3.2.21 app which I use select2 for my dropdown menus and UI. In this view I'm doing a multi-select to pick different elements/objects in my view/form. When the initial controller view loads it's fine as you can see by the first picture. But when I click to a second page to paginate, the select2 is broken by the Ajax as you can see in the last picture.
Here is what my view looks like:
index.html.erb
<h3 class="offset3">5 most recent pages</h3>
<div class="row">
<div class="span3"><div class="well" id="form"><%= render 'form' %></div></div>
<div class="span9"><div id="pages"><%= render 'pages' %></div></div>
</div>
index.js.erb
$("#form").html("<%= escape_javascript render("form") %>");
$("#pages").html("<%= escape_javascript render("pages") %>");
_form.html.erb
<%= form_for(#page) do |f| %>
<div class="control-group">
<div class="controls">
<%= f.label :message %>
<%= f.text_area(:message, size: "40x2", :maxlength => 254, placeholder: "Enter your message here", required: true) %>
<%= f.label 'Unit'%>
<%= f.select :unit_id, options_for_select(Unit.active.order(:unit_name).map{ |unit| [unit.unit_name, unit.id] } + [["All Units", "all"]]), {:include_blank => true}, {:multiple => 'true', class: 'select'} %>
<%= f.label 'Region' %>
<%= f.collection_select(:region_id, Region.order("area ASC"), :id, :area, {:include_blank => true}, {:multiple => 'true', :class => 'select' } )%>
</div>
</br>
<%= f.button 'Send Page', class: 'btn btn-info', data: {disable_with: "<i class='icon-spinner'></i>Sending..."} %>
</div>
<% end %>
_results.html.erb
<table class="table table-striped">
<thead>
<tr>
<th>Incident Number</th>
<th>Patient Name</th>
<th>Transfer Date</th>
<th>Transferred From</th>
<th>Transferred To</th>
<th>Determinant</th>
<th>Unit</th>
<th>Insurance</th>
<th>Cancelation Reason</th>
<th>Billing Reference</th>
<th>Run Time</th>
<th></th>
</tr>
</thead>
<tbody>
<% #calls.each do |c| %>
<tr>
<td><%= c.incident_number %></td>
<td><%= c.patient_name %></td>
<td><%= c.transfer_date.strftime("%m/%d/%y") %></td>
<td><%= transferred_from(c) %><br/><%= transferred_from_address(c) %></td>
<td><%= transferred_to(c) %><br/><%= transferred_to_address(c) %></td>
<td><%= c.nature.try(:determinant)%></td>
<td><%= c.units.map(&:unit_name).join(", ") %></td>
<td><%= c.insurance.try(:insurance_type)%></td>
<td><%= c.cancel_reason.try(:reason) %></td>
<td><%= c.imx_num %></td>
<td><%= TimeFormatter.format_time(c.run_time) if c.in_service_time? %></td>
<td><%= link_to 'View', c, :class => 'btn btn-info btn-mini'%></td>
</tr>
<% end %>
</tbody>
</table>
<%= will_paginate #calls, :renderer => BootstrapPagination::Rails %>
Here is the jQuery from my application.js file:
$(function (){
$(".select").select2({
placeholder: "Select One",
allowClear: true
});
$(function() {
$("#pages th a, #pages .pagination a").live("click", function() {
$.getScript(this.href);
return false;
});
});
It looks like when I click a pagination link the dom is not getting reloaded and thus breaking the UI with select2. I can still pick objects in the form but UI-wise it's broken.
I was wondering if anyone could help point me in the right direction with this as I'm not very savvy with jQuery or javascript/ajax.
If my question is not clear or you need further examples, please let me know.
working
breaks on ajax pagination
So far this is the only useful answer I've come up with.
In my index.js.erb file I put the following:
$("select").select2({
placeholder: "Select One",
allowClear: true
}); init();
This will reinitialize select2 by destroying the call on an Ajax request then calling select2 again. It behaves as expected and keeps the UI properties while overriding the "turbolinks-like" Ajax behavior.
This is working 100% for me, although it feels a bit hackish that I have to do this in UJS, but hey whatever works right?
I have an array of Pet instances and each of them have a name, type, sex and size, etc... I'd like to iterate through this collection of Pet instances and display the each one by one.
This is what I have so far:
<% #pets.each do |pet| %>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Sex</th>
<th>Age</th>
<th>breed</th>
<th>Size</th>
<th>Picture</th>
<th>Description</th>
<th>Shelter_id</th>
<th>Shelter</th>
<th>Phone</th>
<th>Email</th>
<th>City</th>
<th>Zip</th>
<th>Like</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= pet.name %></td>
<td><%= pet.species %></td>
<td><%= pet.sex %></td>
<td><%= pet.age %></td>
<td><% pet.breeds.each do |breed| %>
<li><%= breed.name %></li>
<%end%>
</td>
<td><%= pet.size %></td>
<td><%= image_tag pet.picture %></td>
<td><%= pet.description %></td>
<td><%= pet.shelter_id %></td>
<% shelter = Shelter.find(pet.shelter_id)%>
<td><%= shelter.name %></td>
<td><%= shelter.phone %></td>
<td><%= shelter.email %></td>
<td><%= shelter.city %></td>
<td><%= shelter.zip %></td>
<td><%= form_for pet, :url => { :controller => "favorite_pets", :action => "create" }, :html => {:method => :post} do |f| %>
<%= f.text_field :id %>
<%= f.submit %>
<% end %>
</td>
</tr>
</tbody>
</table>
<%end%>
So now, I need two thinks.
1st
When a user clicks the submit button (<%= f.submit %>) to send a POST request i do not want the page to reload nor to redirected me to another page. I looked around and it seems like :remote => true included in the form_for would do the trick but i'd need more help with that.
2nd
I'd like to control the iterating through #pets so that only the first one is displayed pet[0] and every time I click on the <%= f.submit %> button not only does it not reload the page but it displays the next index in the array pet[1]
I think I need to set up a sort of counter which would increment the index of the pet array every time a user click on the <%= f.submit %> button.
Any guiding thoughts?
If I were to do this I would use some javascript. So your submit button turns into more of an "Add Button" it doesn't actually submit anything. It would just add that object (in your case the pet, along with its info) to a javascript array as well as display the new object (again the pet along with the info) on the window. Then create another button to actually submit all the pets. (The javascript array)
I'm trying to make my table sortable (drag and drop) using jQuery, the problem I'm having is saving the new sorted table and then publishing it to other users or when the browser is refreshed.
I'm using:
gem 'acts_as_list'
Rails 3.2.17
ruby 1.9.3p484
Is there a better way of doing this?
my sort and show method on my controller
def sort
#episodes = current_user.episodes_for(#show).each do |episode|
episode.position = params['episode'].index(episode.id.to_s) + 1
episode.save
end
render :nothing =>true
end
def show
#episodes = current_user.episodes_for(#show)
#episodes.order('episode.position ASC')
#episode = #episodes.where(id: params[:id]).first
end
My Javascript
<script>
$(window).load(function() {
$("#sortthis").sortable({
axis: 'y',
placeholder: 'ui-state-highlight',
cursor: 'move',
dropOnempty: false,
scroll: true,
opacity: 0.4,
update: function(event, ui){
var itm_arr = $("#sortthis").sortable('toArray');
var pobj = {episodes: itm_arr};
$.post("/episodes/sort", pobj);
}
});
});
</script>
The table I wanna sort in views:
<tbody id='eps'>
<tr>
<th>Title</th>
<th>#</th>
<th>Season</th>
<th>Download?</th>
<th>Shared?</th>
<th>Length</th>
<th>Status</th>
<th></th>
</tr>
<% for episode in #episodes %>
<tr>
<td id="eps_<%= episode.id %>", width="20%"><%=h episode.title %></td>
<td id="eps_<%= episode.id %>"><%=h episode.number %></td>
<td id="eps_<%= episode.id %>"><%=h episode.season %></td>
<td id="eps_<%= episode.id %>"><%=h episode.is_downloadable %></td>
<td id="eps_<%= episode.id %>"><%=h episode.shared_with_dev %></td>
<td id="eps_<%= episode.id %>"><%=h episode.length %></td>
<td>
<% if episode.video %>
<%= link_to 'Replace Video', new_show_episode_video_path(episode.show, episode) %>
<% else %>
<%= link_to 'Upload Video', new_show_episode_video_path(episode.show, episode) %>
<% end %>
</td>
<td>
<%= link_to "View", [episode.show, episode] %>
<%= link_to "Edit", edit_show_episode_path(episode.show, episode) if permitted_to? :update, episode %>
<%= link_to "Delete", show_episode_path(episode.show, episode), :confirm => 'Are you sure?', :method => :delete if permitted_to? :delete, episode %>
</td>
</tr>
<% end %>
any help will be greatly appreciated!
I figured it out, made the following changes:
Views:
<tr id="<%= episode.id %>">
and new controller
def sort
Episode.all.each do |e|
if position = params[:episodes].index(e.id.to_s)
e.update_attribute(:position, position + 1) unless e.position == position + 1
end
end
render :nothing => true, :status => 200
end
finally added this to my model:
acts_as_list
default_scope order('position')
let me know if anyone's having similar issues.