I am using jquery-datatables. I want that if the value is numeric its alignment should be right and if it is string then the alignment should be left. Is it possible with datatables or is there any method in it?
<% #tasks.each do |task| -%>
<tr>
<% col_order.each do |key| %>
<td>
<% value = task[key.column_name.split(" as ")[1]] || task[key.column_name.split(".")[1]] -%>
<% if key["drilldown_reportid"].present? %>
<%= link_to value, project_report_path(#current_project,key["drilldown_reportid"], :column=>"#{key.column_name}", :value=>"#{value}") %>
<% else %>
<%= value -%> </td>
<% end %>
<% end %>
</tr>
<% end -%>
I solved this problems by changing the client side code (not changing any server-side code).
in file css/jquery.dataTables.css,
I added a class
.alignRight { text-align: right; }
And in my javascript file for processing the data I changed "aoColumnDefs"...
///...
"aoColumnDefs" : [
//...col 1 -6
// col_07
{
"aTargets" : [ 7 ],
"fnRender" : function(oObj) {
return Math.round(oObj.aData["endingDepth"] * 100) / 100;
},
"sTitle" : "Ending Depth [m]",
"sWidth" : "5em",
"sClass" : "alignRight"
},
//... more columns
Another solution: add the following line and no need the update the CSS
///...
"aoColumnDefs" : [
//...col 1 -6
// col_07
{
"aTargets" : [ 7 ],
"fnRender" : function(oObj) {
return Math.round(oObj.aData["endingDepth"] * 100) / 100;
},
"sTitle" : "Ending Depth [m]",
"sWidth" : "5em",
"sClass" : "right"
},
//... more columns
Related
I use Rails 4.
I use structure
#users = ActiveRecord::Base.connection.execute("SELECT * FROM users")
#users.each do |row| %>
puts user[0] # id--> 1,2,3,4,5,6
end
#users.fields do |field| %>
puts field.name # --> id, login, password.....
end
how to display the data by column name?
for example
#users.first.field['id']
ActiveRecord::Base.connection.execute( will run the sql but not return anything (or return nil, more accurately). To load the records into memory do
#users = User.all
You don't describe what you want very well. If you literally want "1,2,3,4,5,6" you could do
#users.map(&:id).join(",")
if you wanted, for example, to do a table showing all the user data you could do (in your view)
<% cols = User.column_names %>
<table>
<thead>
<tr>
<% cols.each do |col| %>
<th><%= col %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<% cols.each do |col| %>
<td><%= user.send(col) %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
Like i say i don't know if this is what you actually want to do. Your requirements are a very messy and very vague.
EDIT
If you want to load data out of the database without using an AR model then you can do
#users = ActiveRecord::Base.connection.select_all("select * from users")
this will give you an array of hashes like this
[{"id" => "1", "name" => "foo", "bar" => "baz"}, {"id" => "2", "name" => "chunky", "bar" => "bacon"}, .... ]
which you could then access like
user = #users.first #you've got a hash now
aname = user["name"]
I am stuck with what I think is a simple problem. I am creating json and need to have the format be:
[{ "source" : "google / organic", "visits" : 20 }]
And here is what I get:
[{"source"=>"google / organic", "visits"=>20}]
Here is the model (campaign_results.rb)
def as_json(options = {})
{ "source" => source,
"visits" => visits,
}
end
In the controller:
def show
#campaign_summary = CampaignResults.all
end
In the view:
<%= raw #campaign_summary.as_json %>
Any suggestions on what I should do to replace the "=>" with ":"?
Try calling #to_json:
<%= raw #campaign_summary.as_json.to_json %>
This is a quick one:
In my rails view I have:
<% h = { "a" => 100, "b" => 200 } %>
<%= h.each_key { |key| puts key } %>
This is returning the following in the view:
{"a"=>100, "b"=>200}
However, according to the ruby api doc (http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-each_key), it should just return the following:
a
b
Why is the whole hash being listed in the view instead of just the key output? I know that this is probably I stupid question, but I have been stuck on this for awhile.
Thanks in advance for your help.
Write this code for correct result:
<% { "a" => 100, "b" => 200 }.each_key do |key| %>
<%= key %>
<br/>
<% end %>
When you write pusts key key is wrote to log (and console). When you write <%= key %> key is wrote to your page.
I am trying the following jsp code.
switch(ch)
{
case 1 :
ResultSet rsid=stmt.executeQuery("select ID from BcTwo");
while(rsid.next())
{
%>
<tr><td>
<%out.println(rsid.getString(1)); %>
</td></tr>
<%
}
rsid.close();
break;
case 2 :
ResultSet rs=stmt.executeQuery("select SERIES from BcTwo");
while(rs.next())
{
%>
<tr><td>
<%out.println(rs.getString("SERIES")); %>
</td></tr>
<%
}
rs.close();
break;
}
Using this code I am able to print the data.But I want the data to be printed in following format :
ID Series
1 BE
2 EQ
3 BE
4 BE
5 EQ
6 EQ
And using the above code data is getting printed as :
ID
1
2
3
4
5
6
Series
BE
EQ
BE
BE
EQ
EQ
What changes I should make to get the required output?
I got the answer.
switch(ch)
{
case 1 :
String ID=(String)session.getAttribute("ID");
session.setAttribute("ID", ID);
if(null == session.getAttribute("ID"))
{
out.println(rsid.getString("ID"));
}
break;
case 2 :
String SERIES=(String)session.getAttribute("SERIES");
session.setAttribute("SERIES", SERIES);
if(null == session.getAttribute("SERIES"))
{
out.println(rsid.getString("SERIES"));
}
break;
}
I am not sure whether this is the best solution but it just worked for me.
switch(ch)
{
%>
<tr>
<%
case 1 :
ResultSet rsid=stmt.executeQuery("select ID from BcTwo");
while(rsid.next())
{
%>
<td>
<%out.print(rsid.getString(1)); %>
</td>
<%
}
rsid.close();
break;
case 2 :
ResultSet rs=stmt.executeQuery("select SERIES from BcTwo");
while(rs.next())
{
%>
<td>
<%out.print(rs.getString("SERIES")); %>
</td>
<%
}
rs.close();
break;
}
%>
</tr>
first of all ,having java code inside scriptlets is a bad practice. You should avoid it. DO the necessary java code in a servlet and use EL or JSTL to display the returned data in the JSP
as for your question, each result will have a TR where the ID and SERIES will be printed inside that TR in 2 TDs
<%
ResultSet rsid=stmt.executeQuery("select ID,SERIES from BcTwo");
while(rsid.next())
{
%>
<tr>
<td>
<%out.print(rsid.getString("ID")); %>
</td>
<td>
<%out.print(rsid.getString("SERIES")); %>
</td>
</tr>
<%
}
rsid.close();
%>
hope this helps..
I need to create some type of helper for a form where it will create a select for each day of the week.
Below, creates the 5 instances, but only submits the last one.
def basic_question(q)
a = ""
5.times do
a << select("question[question_id_#{q.id}]", :response, (0..30).to_a) + " for #{q.survey.publish_on.strftime('%A')} #{q.survey.publish_on.strftime('%D')} <br />"
end
return a.html_safe
end
EDIT
Here is the view to take the survey
<%= form_for store_survey_path(#survey) do |f| %>
<% hidden_field.user_id, :value => current_user.id %>
<% #survey.questions.each do |q| %>
<li><%= q.content %></li>
<%= question_helper(q) %>
<% end %>
<p><%= f.submit %></p>
<% end %>
And the helper that checks for the type of question it is.
def question_helper(question)
case question.question_type
when 'basic'
return basic_question(question)
when 'fill_in'
return fill_in_question(question)
when 'scale_5'
return "should return a scale of 5"
when 'scale_10'
return "should return a scale of 10"
when 'must_choose_answer'
return question.answers.to_s
when 'just_label'
return " I will be a label"
else
return "Couldn't find in helper"
end
end
You could either go with giving the separate selects different names so that they are submitted separately, or you could submit them all as an array. I would go with the last option.
To submit them as an array, you have to add [] to the end of the select name like this:
question[question_id_#{q.id}][]
Which would end up looking like this:
a << select("question[question_id_#{q.id}][]", :response, (0..30).to_a) + " for #{q.survey.publish_on.strftime('%A')} #{q.survey.publish_on.strftime('%D')} <br />"
I hope that helps.