What is the strut 2 equivalent of strut 1 logic:iterate tag property attribute - struts2

In strut 1 we use logic:iterate tag to iterate over the list. While defining this tag we use property attribute and strut 1 call getter method of this attribute to get Iterable Object. What is the equivalent in strut 2 for same thing ?
As show in below code in strut 1 we are using logic:iterate tag to iterate over iterable object. And Strut 1 get this itearble object by calling getter method of property attribute(records in this example) which is defined by name attribute(DataForm in this example).
i.e. in below example, strut 1 internally calls getRecords() method which is present in DataForm class to get the iterable object.
How can we achive same thing in strut 2 ?
<div STYLE=" height: 300px; width: 860;font-size:12px; overflow: auto;">
<html:form action="/discardHorisBulk.do" target="content">
<table width="840">
**<logic:iterate id="horisList" name="DataForm" property="records" indexId="indexId" type="com.waghtech.client.isTech.model.Horis" >**
<tr>
<%
String bgColor="#fffafa";
int size = indexId.intValue();
if (indexId.intValue() %2 == 0)
{
bgColor="#dcdcdc";
}
java.util.HashMap params = new java.util.HashMap();
params.put("key", horisList.getKey() );
params.put("clientName", horisList.getClientName() );
pageContext.setAttribute("paramsName", params);
%>
<td bgcolor=<%=bgColor%> width="200">
<html:link page="/mapClient.do" name="paramsName" scope="page" >
<bean:write name="horisList" property="clientName"/>
</html:link>
</td>
<td bgcolor=<%=bgColor%> width="60">
<bean:write name="horisList" property="startDate"/>
</td>
<td bgcolor=<%=bgColor%> width="60">
<bean:write name="horisList" property="endDate"/>
</td>
<!--
<td bgcolor=<%=bgColor%> width="200">
<bean:write name="horisList" property="displayFd"/>
</td>
-->
<td bgcolor=<%=bgColor%> width="200">
<bean:write name="horisList" property="userfileName"/>
</td>
<td bgcolor=<%=bgColor%> width="40">
<html:multibox name="dataForm" property="markedRecords" value="<%=horisList.getClientName()%>">
<bean:write name="horisList" property="key"/>
</html:multibox>
</td>
</tr>
</logic:iterate>
</table>
</div>

You can use value attribute for it. something like this :
<ol>
<s:iterator value="records">
<li><s:property value="userfileName"/></li>
</s:iterator>
</ol>
This will call the getter method of this list and within those when you use value attribute it will call getter for those fields in records class.

Related

MVC- drop down values not binding for all the rows in a table

The model:
public class EdituserModel : IEnumerable<EdituserModel>
{
public int UserID { get; set; }
public string Status { get; set; }
public IEnumerator<EdituserModel> GetEnumerator()
{
return ((IEnumerable<EdituserModel>)editUser).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<EdituserModel>)editUser).GetEnumerator();
}
}
The View:
<table id="tbltbl_usertable" class="tableasd" cellpadding="0" cellspacing="0">
<tr>
<th style="width:115px;text-align: center; ">User Id</th>
<th style="width:100px;text-align: center;">Status</th>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
#foreach (var user in Model)
{
<tr>
<td class="UserID" style="text-align: center; ">
<span style="text-align: center;">#user.UserID</span>
</td>
<td class="Status" style="text-align: center;">
<select id="SelectedStatusId" name="SelectedStatusId" value="#user.Status" >
<option value="A" selected="selected">Admin</option>
<option value="U">Dashboard user</option>
</select>
</td>
</tr>
}
</table>
From the above code, the UserID is being displayed properly (8 rows for 8 users). But only one drop down is being displayed for the first row and not the subsequent rows. Is there something I'm missing?
The way you're binding to the select is wrong. You can't set an option in the dropdown by setting the value attribute of select (try it). Besides, you're setting selected="selected" on the first option. I'm guessing that is selected in all the dropdowns.
"Only one drop down is being displayed for the first row and not the subsequent rows":
I don't know how this is possible. I have tested this in my system and it wasn't reproducible.
So, how to bind your value to the dropdown? You should use MVC's DropdownListFor HTML Helper.
#for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td class="UserID" style="text-align: center; ">
<span style="text-align: center;">Model.editUser[i].UserID</span>
</td>
<td class="Status" style="text-align: center;">
#Html.DropDownListFor(m => Model.editUser[i].Status, new SelectList(new[] {
new { Text = "Admin", Value = "A" },
new { Text = "Dashboard user", Value = "U" },
}, "Value", "Text", Model.editUser[i].Status))
</td>
</tr>
}
You should use a for loop instead of foreach whenever you're looping and creating form elements in MVC (Why though?). This generates appropriate name attribute which are important when submitting forms.
(We usually get the SelectListItems in a ViewBag and bind. But there is an issue in MVC with DropDownList helper when we are looping)
"BUT I DONT CARE ABOUT GIVING PROPER NAMES TO FORM ELEMENTS
OR DROPDOWNLISTFOR OR ANY OF THAT STUFF. I JUST WANT TO BIND MY DROPDOWN OKAY?":
Then:
#for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td class="UserID" style="text-align: center; ">
<span style="text-align: center;">#Model.editUser[i].UserID</span>
</td>
<td class="Status" style="text-align: center;">
<select id="SelectedStatusId" name="SelectedStatusId">
<option value="A" #(Model.editUser[i].Status == "A" ? "selected" : "" )>Admin</option>
<option value="U" #(Model.editUser[i].Status =="U" ? "selected" : "" )>Dashboard user</option>
</select>
</td>
</tr>
}
UPDATE: I saw your update now reagarding the Enumerator. See, this is why I asked you post all the relevant code. You said your model for the page was IEnumerable<EdituserModel>. My asnwer would still work. Replace all the Model[i] with Model.editUser[i] (otherwise you'd have implement IList as well)

How to Use/ Display a Asp.net DataList in MVC Razor View

I have this list below, which I am generating on the fly using Razor view:
<table id="contractCoverablesGrid">
<tbody>
#foreach (var coverableItem in Model.ContractCoverablesList)
{
<tr>
<td>
#Html.Hidden("coverID",coverableItem.CoverID)
</td>
<td>
#Html.Label(coverableItem.Name)
</td>
<td>
<input type='checkbox' class='chkboxActive' checked='checked' />
</td>
</tr>
}
</tbody>
</table>
As you can imagine the outcome of this is a vertical list, which keeps extending downwards.
I would like to have this list 'wrapped' into three adjacent columns instead of one single long column; just as you would do this in ASP.net Datalist server control (in Webforms).
My initial thoughts were to limit the width & height of the table to set values and then keep floating the td(s) left. But how do I float a td. I cannot turn this into div.
Any thoughts?
Please let me know.
You might try a for instead of foreach. You would loop through the list divided by 3 and start a new td each time.
This is how I resolved it:
<table id="contractCoverablesGrid" width="600" align="center">
<tbody>
<tr>
#foreach (var coverableItem in Model.ContractCoverablesList)
{
<td>
<div id="dataListItem">
#Html.Hidden("coverableID", coverableItem.CoverID)
#Html.Label(coverableItem.Name)
<input type='checkbox' name="coverableItemCheckBox" id="coverableItemCheckBox" />
</div>
</td>
}
</tr>
</tbody>
</table>
<style>
#dataListItem {
float: left;
font-weight: normal;
}
</style>

Grails Pagination

Hello im back with another tedious question!
Trying to get my table to paginate. There are 12 users in the table. Here is my controller function
def listDuplicates(params) {
def result = User.getAllWithDuplicateIDs()
def totalDupCount = result.size()
/*sout for troubleshooting */
System.out.println("Duplicate:" + result.ID + " " + result.username)
params.max = Math.min(params.max ? params.int('max') : 10, 100)
return [resultList: result, totalDupCount: totalDupCount, params:params ]
}
Here is my view
<div>
<fieldset class="warningFieldSet">
<h1 style="color: red" align="center">
<g:message code="Duplicate IDs" />
</h1>
<p style="color: red; margin-left: 20px;">Duplicate IDs Found!</p>
<table>
<thead>
<tr>
<g:sortableColumn property="Username" title="Username" />
<g:sortableColumn property="ID" title="ID" />
<g:sortableColumn property="Status" title="Status" />
</tr>
</thead>
<tbody>
<g:each in="${resultList}" status="i" var="resultDuplicate">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td>
${resultDuplicate.username}
</td>
<td style="color: red; font-weight: bold">
${resultDuplicate.id}
</td>
<td>
${resultDuplicate.accountStatus }
</tr>
</g:each>
</tbody>
<tfoot>
<g:if test="${totalDupCount >10 }">
<div class="paginateButtons">
<g:paginate action= "listDuplicates" total="${totalDupCount}" />
</div>
</g:if>
</tfoot>
</table>
</fieldset>
</div>
Domain function for finding the duplicate IDs
static List<User> getAllWithDuplicateIDs() {
findAll("FROM User WHERE id IN (SELECT id FROM User group by id having count(*) > 1) AND id != '' ", [])
}
The buttons show up. And in the URL the offset and max is displayed. The table just puts all 12 displayed instead of 10 on one page and 2 on the other. 2 Page numbers show up so It knows that it is only suppose to display only 10 per page. It just isn't doing it in the table itself. Im assuming its some kind of issue with passing params and such.
Any Suggestions/Opinions/Help are/is greatly appreciated!
Grails pagination is based on two parameters: max and offset. max determines the page size, and offset determines where the current page starts. The controller receives these parameters and generally passes them to a database query. The list method added to domain objects by grails handles these parameters, and the finder methods take a queryParams. The usual pattern is to pass the params object directly to list or as the queryParams parameter to the finders. This returns a result set starting at the given offset, with one page length.
In your example, you're calling getAllWithDuplicateIDs without making use of these parameters. Update your query to take them, like this:
static List<User> getAllWithDuplicateIDs(params) {
findAll("FROM User WHERE id IN (SELECT id FROM User group by id having count(*) > 1) AND id != '' ", [], params)
}
Alternatively, page it in memory with something like
results = results.drop(params.offset).take(params.max)
Paging directly in the query is preferable, since it will perform better handle cases where the entire list doesn't fit in memory.
Provide max and offset function params this:
def result = User.getAllWithDuplicateIDs([max:params.max, offset:params.offset])
And use them in in query to database.
Or check the answer how to get results from list with max and offset in answer here
Look at this example .
Domain class ..
class Job {
static belongsTo = [company:Company]
String jobtitle
String jobdescription
String jobskills
String joblocation
String experience
String jobtype
String salary
}
Controller Code..
def uijobs () {
[res:Job.list(params),jobcount:Job.count()]
}
and view is here.
<div class="container" id="main">
<div class="row">
<g:each in="${res}">
<div class="col-sm-4">
<div class="panel panel-warning">
<div class="panel-heading">
<h4 class="panel-title"><g:link action="infopagejob" controller="Job" id="${it.id}">${it.jobtitle}</g:link></h4>
</div>
<div class="panel-body">
<table class="table">
<tr class="info" >
<td > Job Location</td>
<td >${it.joblocation}</td>
</tr>
<tr class="info">
<td>Description</td>
<td>${it.jobdescription}</td>
</tr>
</table>
</div>
</div>
</div>
</g:each>
</div>
<g:paginate next="Forward" prev="Back" maxsteps="10" controller="Job" action="uijobs" total="${jobcount}" params="${params}"/>
</div></div>

Struts 2.0 pass selected checkbox mapping value to JavaScript function

I am using struts 2 framework and iterating 4 fields (checkbox,rollnumber,name,location) in Jsp. It is working fine. Now i need to delete the selected checkbox record, for that i require rollnumber(which is primary key in table) object to be pass to javascript function. How can I pass the rollnumber object to javascript function. I have already pass the checkbox object(document.myForm.subCheckBox) to java script function, i need to pass one more rollnumber object.
<table border="1">
<tr>
<s:if test="%{mode != 'view'}">
<td><input type="checkbox" id="mainCheckBox" onclick="return checkAll(document.myForm.subCheckBox)"/></td>
</s:if>
<th>Roll Number</th>
<td>Name</td>
<td>Location</td>
</tr>
<s:iterator value="beanList" >
<tr>
<s:if test="%{mode != 'view'}">
<td>
<input type="checkbox" name="subCheckBox"/>
</td>
</s:if>
<td>
<s:property value="rollnumber" />
</td>
<td>
<s:property value="name"/>
</td>
<td>
<s:property value="location"/>
</td>
</tr>
</s:iterator>
</table>
<table>
<s:if test="%{mode != 'view'}">
<tr>
<input type="button" value="Delete" onclick="return deleteRecord(document.myForm.subCheckBox) "/>
</tr>
</s:if>
</table>

Getting Posted Data from a Dynamic Form with MVC

I have a page that contains a form, where part of it is dynamically generated based off of what SKU's are on an order.
<% for each i in ViewData.Model %>
<script type="text/javascript">
$(function () {
$('#return_<%=i.SKUN%>').change(function () {
if ($('#return_<%=i.SKUN%>').val() > $('#qty_<%=i.SKUN%>').val()) {
$('#Discrepancy').val("Yes");
} else {
$('#Discrepancy').val("");
}
});
});
</script>
<tr>
<td style="text-align: left"><%= i.SKUN%></td>
<td style="text-align: left; width: 360px"><%= i.DESCR%></td>
<td style="text-align: left">£<%= i.PRIC%></td>
<td style="text-align: left"><%= i.QUAN%></td>
<td style="text-align: left">£<%= i.EXTP%></td>
<td style="text-align: left"><input type="hidden" name="qty_<%=i.SKUN%>" id="qty_<%=i.SKUN%>" value="<%= i.QUAN%>"/><input type="text" name="return_<%=i.SKUN%>" id="return_<%=i.SKUN%>" style="width:50px;" class="required"/>
<% If i.FLAG3 = "T" Then
%> <img src="../../Content/images/icons/error.png" alt="This SKU is subject to a <%=Html.ViewData("RestockFee") %>% restocking fee" title="This SKU is subject to a <%=Html.ViewData("RestockFee") %>% restocking fee"/><%
End If%>
</td>
</tr>
<% Next%>
It's by no means perfect, but it gets the job done at the moment.
The part I'm struggling with is as return_<%=i.SKUN%> is a series of dynamically generated text boxes that change for each order, though they remain with the naming convention of return_<%=i.SKUN%>, how do I get the values for them in my controller that handles the form post?
EDIT: It's also important to note that none of these fields are required fields and that the number of text boxes varies per order.
Can't you change the naming convention to:
<input
type="text"
name="skuns[<%= index %>]"
id="return_<%= i.SKUN %>"
style="width:50px;"
class="required"
value="<%= i.SKUN %>"
/>
where index would be an incrementing variable from 0 to n. This way your controller action could look like this:
Public Function Result(skuns As String()) As ActionResult
And leave the default model binder do the job.

Resources