using if statement in each-loop - javascript

I have a loop
<% _.each(kw, function (x) { %>
now I want to use a if statement which checks a string
<% if ( <%=x%> == "condition") { %>
<div>..</div>
<% } %>
but doesn't work :( any ideas?
another question:
<script>
var maxKW = $("[id^='kalenderwoche']:first").attr("id");
</script>
is declared above and what would be the correct if statement when I want to use maxKW
<% if ( <%=x%> == maxKW) { %>
<div>..</div>
<% } %>
would be great if you could help me :)

<% if (x === "condition") { %>
<div>..</div>
<% } %>
Pass maxKW as attribute to the compiled template:
var maxKW = $("[id^='kalenderwoche']:first").attr("id");
var html = _.template('your html')({maxKW:maxKW});

Related

Ejs double ejs tag in one line erorr

<% if ( <%= id %> = 0 ) { %>
erorr :
Error: Could not find matching close tag for "<%".
can anyone please tell me how can add my passed id value in if ( after searching some while i have guessed the erorr that you cant 2 ejs tags in one line )
any solutions ?
you don't need to use <%= %> this tag to access id value inside <% %> .
Just simply change this
<% if ( <%= id %> = 0 ) { %>
to this:
<% if(id == 0 ){ %>
//your code
<% } %>

EJS comparison between two same strings

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>
<% } %>

How to add in else statement in underscore template

I have the following code:
var c = 'Credits: <% if (credits) { %> <%= credits %> <% } %> <% else { %> N/A <% } %>'
However I get Unexpected token else. Is the else statement different than how the if statement is added? What should the above correctly be?
Just get rid of the %> <% between } and else. Like this:
var c = 'Credits: <% if (credits) { %> <%= credits %> <% } else { %> N/A <% } %>';
Alternatively, the ternary operator is one of my faves:
var c = 'Credits: <%= credits ? credits : "N/A" %>';
In case it's unclear, the ternary is basically a reduced if/else statement. The part before the ? is the expression being evaluated for truthiness. If it's truthy, the middle part between ? and : is executed, but if it's falsey, the last part after : is executed instead.
A better way to do this would be with the OR operator:
var c = 'Credits:</b> <%= credits || "No credits" %></div>

Changing inline javascript with Perl variables into a function

I currently have a select tag with an inline onChange property like this:
<select id="owner_sel" name="owner"
<% print ' style="display: none"' if(!$user->has_permission($task->cid, $PERMISSION_CHANGE_OWNER)); %>
onChange="if(!this.selectedIndex == 0) {
if(this.form.state_id.value == <%= $STATE_NEW %>) {
this.form.state_id.value = <%= $STATE_ASSIGNED %>
}
} else {
this.form.state_id.value = <%= $STATE_NEW %>
}">
<option></option>
<%
foreach my $h (#sorted_users){
foreach my $uid (keys %{$h}) {
my $selected = ($uid == ($form->{owner} || $task->owner)) ? 'selected="selected"' : '';
print qq{<option value="$uid" $selected>$h->{$uid}</option>};
}
}
%>
</select>
and I wanted to move it into a function like this:
<script>
// code...
</script>
Im pretty newbie with javascript and I also don't know if Perl variables can be called the same way in javascript functions?
edit
<script>
$(document).on('change', '#owner_sel', function() {
if(!this.form.owner_sel.selectedIndex == 0) {
if(this.form.state_id.value == <%= $STATE_NEW %>) {
this.form.state_id.value = <%= $STATE_ASSIGNED %>
}
} else {
this.form.state_id.value = <%= $STATE_NEW %>
}
});
$(document).on('change', '#state_id', function() {
if (this.form.state_id.value = <%= $STATE_NEW %> ){
this.form.owner_sel.selectedIndex = 0;
}
});
</script>
----------Question-----------
I redid my code to something like that. When it is just one function, everything works but when I have both scripts included like the snippet above, there is an error.
I was wondering, in my second function if the state_id = 'NEW' and the owner_sel changes to index '0' does that re-trigger the first function?
<script>
$(document).on('change', '#owner_sel', function() {
if(!this.form.owner_sel.selectedIndex == 0) {
if(this.form.state_id.value == <%= $STATE_NEW %>) {
this.form.state_id.value = <%= $STATE_ASSIGNED %>
}
} else {
this.form.state_id.value = <%= $STATE_NEW %>
}
});
$(document).on('change', '#state_id', function() {
if (this.form.state_id.value == <%= $STATE_NEW %> ){
this.form.owner_sel.selectedIndex = 0;
}
});
</script>
The way you translated it was correct. And yes, Perl variables can be called into the javascript function the same way you called on them inline.
The reason your code is conflicting is because your second line of code in the .on('change', '#state_id' code is using a single '=' and I think you meant to use '==' to check if the state is 'NEW'. The way you have it now would just change the owner_sel.selectedIndex to '0' everytime the state_id is changed.
if (this.form.state_id.value == <%= $STATE_NEW %> ){

jQuery if/else condition doesn't work fine in js.erb files

I'm running into problem with jQuery if/else condition here.
I perform a render partial in both the conditions, when I check by placing a debugger both these conditions get executed irrespective of if condition.
And every-time the partial from else condition is updated even though if condition is true.
<% if #associated_goal.present? %>
<% goal_status = #associated_goal.completion_percentage(#required_tasks) %>
var progress_bar_selector = "#cjs_show_goal_progress_bar_<%=#associated_goal.id.to_s%>";
var progress_from_goals = "#cjs_show_goal_progress_from_goals_<%=#associated_goal.id.to_s%>";
var goal_progress_display = ".cjs-mentoring-model-goal-progress-<%=#associated_goal.id.to_s%>";
if(progress_bar_selector != "")
{
$(progress_bar_selector).html("<%= escape_javascript(render :partial => "mentoring_model/goals/display_goal_progress", locals: { goal: #associated_goal, goal_status: goal_status, connection_and_reports_page: true }) %>");
var percentage_content = <% content_tag(:span, goal_status.to_s+"%", :class => "pull-right cjs-mentoring-model-goal-progress-#{#associated_goal.id}") %>;
jQuery(goal_progress_display).replaceWith(percentage_content);
}
else
{
$(progress_from_goals).html("<%= escape_javascript(render :partial => "mentoring_model/goals/display_goal_progress.html.erb", locals: {goal: #associated_goal, goal_status: goal_status}) %>");
}
<% end %>
This is the final solution which helped me to resolve my issue.
<% if #associated_goal.present? %>
<% goal_status = #associated_goal.completion_percentage(#required_tasks) %>
var progress_bar_selector = jQuery("#cjs_show_goal_progress_bar_<%=#associated_goal.id.to_s%>");
var progress_from_goals = jQuery("#cjs_show_goal_progress_from_goals_<%=#associated_goal.id.to_s%>");
var goal_progress_display = jQuery(".cjs-mentoring-model-goal-progress-<%=#associated_goal.id.to_s%>");
if(progress_bar_selector.length > 0){
progress_bar_selector.replaceWith("<%= j(render :partial => "mentoring_model/goals/display_goal_progress", locals: { goal: #associated_goal, goal_status: goal_status, connection_and_reports_page: true }) %>");
<% percentage_content = content_tag(:span, goal_status.to_s+"%", :class => "pull-right cjs-mentoring-model-goal-progress-#{#associated_goal.id}") %>
goal_progress_display.replaceWith("<%= j(percentage_content) %>");
}
else{
progress_from_goals.replaceWith("<%= j(render :partial => "mentoring_model/goals/display_goal_progress.html.erb", locals: {goal: #associated_goal, goal_status: goal_status}) %>")
}
<% end %>

Categories