Populate values using optionCollection Tag in struts1 - struts-1

I have an arraylist which contains {1,2,3,4,5} as objects. i want to show these values in dropdown select box. how do i achieve it. how to map with value and label property?

<%
for (int j = 0; j < arrayNew.size(); j++) {
%>
<%
if ("dropDown") {
%>
<%
if (counter == 0) {
%>
<select name="serveyTakerValue">
<%
counter++;
}
%>
<option value=<%=arrayNew.get(j)%>><%=arrayNew.get(j)%></option>
<%
}
%>
<%
}
counter = 0;
%>
</select><br><br>
Try this will solve your problem.
NOte: <select name="serveyTakerValue"> and </select> must be called once. That's why counter is being used here in this code. Assume arrayNew be your ArrayList.

Related

can I use partials in a for loop in ejs?

I'm trying to include partials in a for loop in EJS.
<% let index = 1 %>
<% for (let item in db) { %>
<div class="box unfocused box_<%=index %> ">
<% if (db[item].type === "video") { %>
<%- include ('partials/_video.ejs') %>
<% } else if (db[item].type === "collection") { %>
<%- include ('partials/_collection.ejs') %>
<% } else if (db[item].type === "box") { %>
<%- include ('partials/_box.ejs') %>
<% } %>
</div>
<% index ++ %>
<% } %>
For some reason, the variables defined in the for loop are not being passed down into the partial. For example, _video.ejs looks like this:
<video muted=true autoplay=true loop=true width="250">
<source src="<%=db[item].src%>" type="video/mp4">
</video>
When I run the code, the browser tells me that item is not defined in _video.ejs (item is defined in the for loop).
How to pass item down into the partial?
<%for(let post of posts){%>
<%-include('_post',{post: post}); %>
<%}%>

Add a class to the first child in a loop using Middleman

I am creating a slider for my blog and I want to add a collection of featured items to it, The slider requires that the first child load with the class selected.
How can I do something like if first child do this else do that
Here is what I have so far:
<ul class="cd-hero-slider">
<% blog.articles.select {|a| a.data[:featured] }.each do |article| %>
<li class="selected" style="background-image: url('https://images.unsplash.com/photo-1447014421976-7fec21d26d86?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&s=c82e04e1201234889daab5427f481731')">
<div class="cd-full-width">
<h2><%= link_to article.title, article %></h2>
<p><%= article.summary(250) %></p>
<%= link_to 'Read More', article, :class => 'cd-btn' %>
</div>
</li>
<% end %>
</ul>
Use each_with_index instead of each - this will give you the object but also the position into the array, first being 0:
<% blog.articles.select {|a| a.data[:featured] }.each_with_index do |article, index| %>
<% if index == 0 %>
<li>I'm the first!</li>
<% else %>
<li>Not the first</li>
<% end %>
<% end %>

trying to populate dropdown, error in if else condition ruby

This is the below code that I had written for populating a dropdown.
<html>
<select id = 'status_update' >
<% array = [{"status_name"=>"Submitted", "reachable"=>false, "transition_name"=>""},
{"status_name"=>"Replied", "reachable"=>true, "transition_name"=>"Reply"},
{"status_name"=>"Answered", "reachable"=>false, "transition_name"=>""},
{"status_name"=>"Closed", "reachable"=>false, "transition_name"=>""},
{"status_name"=>"Canceled", "reachable"=>true, "transition_name"=>"Cancel"}]
array.each { |x|
x.each do |key, value|
%>
<option value = "<%= #{x['transition_name']} %>"
disabled = "<%= if ((#{x['reachable']}) == 'false')
return 'disabled'
else
return ''
end %>" ><%= "#{x['status_name']}" %></option>
<% end %>
<% } %>
</select>
</html>
In the above code, array is collection of hashmaps and the keys in hashmaps remains the same, 3 keys with different values in each set. Now, I am trying to populate each hashmap, with the values of their respective keys, in the dropdown. When I try so, I am getting error
ERB syntax error:dropdown:23: syntax error, unexpected kELSE
else
Might be simple, but not able to get the correct way of approach to get the hashmaps in the dropdown. Can anyone of you kindly let me know how to proceed please ?
The second loop in not necessary. This should work:
<% array.each do |x| %>
<option value="<%= x['transition_name'] %>" disabled="<%= x['reachable'] ? '' : 'disabled' %>"><%= x['status_name'] %></option>
<% end %>
Rather than setting disabled="" it's better to omit the attribute:
<option value="<%= x['transition_name'] %>" <%= 'disabled="disabled"' unless x['reachable'] %>"><%= x['status_name'] %></option>
Or, if this is too much in one line:
<% if x['reachable'] %>
<option value="<%= x['transition_name'] %>"><%= x['status_name'] %></option>
<% else %>
<option value="<%= x['transition_name'] %>" disabled="disabled"><%= x['status_name'] %></option>
<% end %>

ASP.NET MVC 3 Razor view Pagination

I'm developing a pagination using MVC 3.0 with code below. All good except how do I populate re-generate the pagination numbers when user selects 5 and then clicks on Next button:
1 2 3 4 5 Next
When user clicks on Next the pagination should look like below:
Prev 2 3 4 5 6 Next
The code look like below
<ul class="pagination-clean">
<% if (ViewData.HasPreviousPage)
{ %>
<li class="previous">« Previous</li>
<% }
else
{ %>
<li class="previous-off">« Previous</li>
<% } %>
<%for (**int page = 1; page <= ViewData.TotalPages; page++**)
{
if (page == ViewData.PageIndex)
{ %>
<li class="active"><%=page.ToString()%></li>
<% }
else
{ %>
<li><%=page.ToString()%></li>
<% }
}
if (ViewData.HasNextPage)
{ %>
<li class="next">Next »</li>
<% }
else
{ %>
<li class="next-off">Next »</li>
<% } %>
</ul>
Is this the custom pagination that you have developed? Have you tried using jqGrid? It has direct binding with the controller action to retrieve the data and it's own pager control. You can explore it. Page number and other parameters will be automatically updated

Where can I find the default Object.cshtml Editor template?

I need to modify the default editor template for scaffolding but I havent found the Object.cshtml template, where can I find the default razor Object.cshtml Editor template?
The following blog post describes how to customize the editor templates: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
Basically you have to add a file named Views\Shared\EditorTemplates\Object.cshtml and put all the logic for displaying the object there.
When #marcind says they're compiled in, the templates themselves are not embedded but are rather written in code. For example, EditorFor calls TemplateFor which may call TextAreaExtensions.TextArea or one of many other extensions which generate the code that is ultimately output. This may be because the we have the option of removing the default view engine and using something like nhaml.
The mapping between the template names and the function creating the resulting output can be seen in System.Web.Mvc.Html.TemplateHelpers. See also System.Web.Mvc.Html.DefaultEditorTemplates.
The closest thing that exists right now are the Webforms templates that exist in Mvc3Futures which are available on the aspnet.codeplex.com website. Within it exists an DefaultTemplates\EditorTemplates folder that contains the templates.
Here's the Object.ascx template:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
bool ShouldShow(ModelMetadata metadata) {
return metadata.ShowForEdit
&& metadata.ModelType != typeof(System.Data.EntityState)
&& !metadata.IsComplexType
&& !ViewData.TemplateInfo.Visited(metadata);
}
</script>
<% if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
<% if (Model == null) { %>
<%= ViewData.ModelMetadata.NullDisplayText %>
<% } else { %>
<%= ViewData.ModelMetadata.SimpleDisplayText %>
<% } %>
<% } else { %>
<% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => ShouldShow(pm))) { %>
<% if (prop.HideSurroundingHtml) { %>
<%= Html.Editor(prop.PropertyName) %>
<% } else { %>
<% if (!String.IsNullOrEmpty(Html.Label(prop.PropertyName).ToHtmlString())) { %>
<div class="editor-label"><%= Html.Label(prop.PropertyName) %></div>
<% } %>
<div class="editor-field"><%= Html.Editor(prop.PropertyName) %> <%= Html.ValidationMessage(prop.PropertyName, "*") %></div>
<% } %>
<% } %>
<% } %>

Resources