So I have a datapoint object with fields date(string) and count(integer). I'm trying to add them to arrays with some JS inside of my show view. Everything is working well - except that my date string is being converted to a series of JS arithmetic. For example: a value of "2015-05-05" is getting converted to 2005. Code follows:
<h1 id="chart"></h1>
<script language="javascript" type="text/javascript">
var counts = ['Count']
var dates = ['x']
<% #chart.datasource.datapoints.each do |c| %>
dates.push( <%= c.date %> )
counts.push( <%= c.count %> )
<% end %>
chart(counts, dates);
</script>
You're missing quotes:
var counts = ['Count']
var dates = ['x']
<% #chart.datasource.datapoints.each do |c| %>
dates.push( "<%= c.date %>" )
counts.push( <%= c.count %> )
<% end %>
chart(counts, dates);
Wrapping the value in quotes will force JS to consider it as a string primitive, rather than a number and arithmetic operators.
Related
Right now I am trying to alter the body of my ejs file with javascript embedded.
Here is the code :
<body>
<%- include('navbar.ejs'); %>
<div class="novel" id="title">
<p>Novel Title</h3>
</div>
<div id="skip"><button>
First
</button>
<button>
Last
</button></div>
<p>
<% for( let index = 0; index < text.length; index++ ) { %>
<%- text[index] %>
<% } %>
In the for statement, the text variable is a list of a very long string that contains the text of a novel. And every item in the list is separated by the new line so now I want to print it out in the ejs file with each line in a new one.
In this case, it would be like doing
document.write("/n")
If it were to work on an ejs file.
Thank you in advance.
try testing every character against newline regex, and if true, add a <br>, else the character:
<% for( let index = 0; index < text.length; index++ ) { %>
<%- (/\r\n|\n|\r/).test(text[index]) ? '<br>' : text[index] %>
<% } %>
or simply split the string by newline, which creates an array which you can join with <br>:
<%- text.split(/\r\n|\n|\r/).join('<br>') %>
you should escape string by using <%= tags, which will require modifying the code.
One option would be to split the string, and then output escaped string along with your HTML newline:
<% const split = text.split(/\r\n|\n|\r/); %>
<% split.forEach(line => { %>
<%= line %><br>
<% }); %>
I need to use RegExp to replace a string and eval() to execute the object.
In my code below, I'm currently in a forEach of items.
item.url : http://localhost:3000/formation/[[item.id]]/[[item.launch_url]]
My code :
<% _.forEach(items, function(item) { %>
<% _.forEach(tableLineActions, function(tableLineAction) { %>
<% if (tableLineAction.conditionToShow) { %>
<% var label = tableLineAction.label; %>
<% var url = tableLineAction.url.replace(/\[\[item.id\]\]/g, item.id).replace('[[modelName]]', modelName).replace('[[item.seo_url]]', item.seo_url); %> // not reusable code
<% url = eval(url.replace(/\[\[/g, '').replace(/\]\]/g, '')); %> // test of a reusable code
<li><icon class="material-icons"><%=tableLineAction.icon%></icon><%=label%></li>
<% } %>
<% }); %>
<% }); %>
Error message : Invalid regular expression flags
I found a solution but this one is not maintenable if I want to send severals properties :
url = url.replace(/\[\[item.id\]\]/g, item.id).replace(/\[\[item.launch_url\]\]/g, item.launch_url)
With this solution, I need to add a new replace() method when a new property is sending and I want something more reusable without specifying all properties to replace.
I try in ejs to compare two Strings which in the current situation are the same and it runs the else case. Here is the code:
<% var idf = notifications.data[i].from; %>
<% var ids = notifications.users[a].id; %>
<% if(idf == ids){ %>
<% index = a; %>
<script>alert('<%= index %>');</script>
<% break; %>
<% }else{ %>
<script>console.log('<%= idf %> - <%= ids %>');</script>
<% } %>
It logs 5d5ecfd1ad6d193de86c2264 - 5d5ecfd1ad6d193de86c2264
Use === instead of == to see if this fixes the issue, as it will prevent internal type coercion.
It might also be worth trimming the strings to see if there is any white space or padding added to them.
You could use the below code to compare two string values in ejs
<% if("some text" === "some text"){ %>
<h1>Yes the above text are equal</h1>
<% } %>
If you are comparing mongoDB objectId, then use toString().
<% var idf = notifications.data[i].from; %>
<% var ids = notifications.users[a].id; %>
<% if(idf.toString() == ids.toString()){ %>
<% index = a; %>
<script>alert('<%= index %>');</script>
<% break; %>
<% }else{ %>
<script>console.log('<%= idf %> - <%= ids %>');</script>
<% } %>
I want to create a javascript map from a java map to set a dropdown value depending upon the value selected in another dropdown.
Below is the code(not working):
var categoryAndReportsMap = new Object();
<%
Map<String,Set<String>> categoryAndReportsJ = (Map<String,Set<String>>) request.getAttribute("categoryAndReports");
for(Map.Entry<String,Set<String>> e : categoryAndReportsJ.entrySet()){ %>
categoryAndReportsMap[ <% e.getKey(); %> ] = <% e.getValue(); %>;
<% } %>
Please suggest how can I achieve this.
You need quotes around the keys and values :
categoryAndReportsMap["<%= e.getKey() %>"] = "<%= e.getValue() %>";
But this supposes those strings don't contain quotes themselves. The best solution would be to use a JSON serializer like the excellent gson, this would be as simple as
var categoryAndReportsMap = <%= gson.toJson(categoryAndReportsJ) %>;
I have the following in a html.erb file:
<%= #location_list = [['test',2]] %>
<script type="text/javascript">
var test = <%= #location_list.to_json %>
alert(test);
</script>
And the alert is not showing up.
However if I do <%= #location_list = [[3,2]] %> - The alert is showing up.
Why?
<%= %> tag means output something. In your case, you probably dont want to output anything, so I guess you are looking for
<% #location_list = [['test',2]] %>, which means normal statement no output will be involved
Sorry, havn't really answer your question.
var test = <%= #location_list.to_json %>
should be
var test = "<%=j #location_list.to_json %>"