Full context: I'm trying to process multiple files using a grails Application. The code I will display comes from the post-processing page where it gives information about the files processed.
My initial sense was to use code like this:
<table>
<tr>
<th>Parsed from Excel:</th>
<th>Uploaded to DS:</th>
<th>File Name:</th>
<th>Size:</th>
</tr>
<tr>
<g:each in="${fileContents}" var="item">
<td>${item}</td>
</g:each>
<%--
<td>${fileContents.ExcelRows?.encodeAsHTML()}</td>
<td>${fileContents.policies?.encodeAsHTML()}</td>
<td>${fileContents.originalFileName?.encodeAsHTML()}</td>
<td>${fileContents.Size?.encodeAsHTML()}</td>
--%>
</tr>
</table>
Now, what I don't understand is why the contents displayed in the <g:each loop always reports key=value such as ExcelRows=14 as I have received in one output case.
When I switch comments (note the <%-- tag being used) it works exactly as expected. From my "ExcelRows" column, I get just "14." What is wrong with my thinking that the <g:each loop should do the same thing? Intuitively it comes down to For each item in fileContents
display item.
My controller code:
def processFile = {
def uploadedFile = request.getFile('excelFile')
//...snipped
def fileContents = [
ExcelRows:"${ods.numberOfRows}",
policies:"${ods.numberOfPolicies}",
originalFileName: "${ods.originalFilename}",
Size:"${ods.size}"
]
[fileContents:fileContents]
}
When iterating over a map you'll be working with Entrys. Try using:
<g:each in="${fileContents}" var="item">
<td>${item.value?.encodeAsHTML()}</td>
</g:each>
Or
<g:each in="${fileContents.values()}" var="item">
<td>${item?.encodeAsHTML()}</td>
</g:each>
Related
I have my grails view, and using a api rest I Receive an array of items,that I put in a table,and I want to make the pair rows with tr class="alt">
I want for first for second ,and so on.
I was thinking to do that with a or creating a tag,but donĀ“t know really if there is something useful that can use for similar cases.
The tr that I want to change In the code is as
<table id="customers">
<tr>
<th>Foto</th>
<th>Nombre</th>
<th>Vendedor</th>
<th>Precio</th>
<th>Estado</th>
<th>Ciudad</th>
</tr>
<g:each in="${results}">
**<tr >**
<td><img src="${it.thumbnail}"></td>
<td>${it.title}</td>
<td>${it.nombre_vendedor}</td>
<td>${it.price}</td>
<td>${it.condition}</td>
<td>${it.address.state_name}</td>
</tr>
</g:each>
</table>
<g:each in="${results}" status="ix">
<tr class="${ix % 2 ? 'light' : 'dark'}">
<td>...
</tr>
</g:each>
I am working on a Rails 3.2.11 app using angular 1.0.5.
Currently, a user will select a Cycle from a dropdown, and that will return a bunch of JSON from my controller using ng-resource.
Here is the method
$scope.update = function(cycleId) {
Cycle.get({action: cycleId}, function(resource) {
$scope.selectedCycle = resource;
$scope.tasks = resource.tasks;
$scope.newTask = {cycle_id: resource.cycle.id};
});
};
Here is an example of what json my controller is returning, which is 'resource' in above function: https://gist.github.com/anonymous/01ffe5a37e370661f6fb
Basically I am needing to use ng-repeat twice (one of them nested) using angulars ng-repeat, so that I can get the task_type_name in there as a header. I'm getting some weird interesting results. See the shorted code in my view below and the full thing here
<section ng-repeat="(task_type_name,task_type) in tasks ">
<h2>{{task_type_name}}</h2>
<table>
<tr>
<th>Task Name</th>
</tr>
<section id="task-edit">
<tr ng-repeat="task in task_type">
<td>
<%= link_to "{{task.name}}", '', "ng-click"=>"toggleShowHistory(task.id)" %>
</td>
</tr>
</section
</table>
So my is occuring at this part here
<section id="task-edit">
<tr ng-repeat="task in task_type">
If I try to combine that section and tr, OR change tr to ANYTHING but a tr, {{task}} no longer becomes available.
<section class="task-edit" ng-repeat="task in task_type">
{{task}} is available right here
<tr>
{{task}} is not available right here
<td>
{{task}} is not avaiable right here
</td>
</tr>
</section>
I tested the same concept on the first loop, and it seems to be fine on that loop just not the second, nested loop.
I'm assuming it has something to do with the scope. But i'm just not getting it.
Also, if you have any tips, i'm very new to angular and would love them.
I created a demo, and I am not seeing any issue. Please check your data source and make sure you plug in the tasks value of the json.
You need to change the nested section to tbody.
Demo on jsfiddle
I have a list of table names in a request attribute, "BillSummaryTables". I am iterating through the list and I want to use each table name to get a request attribute for that particular table name. Corresponding to each table name I have another list in request attribute and I want to iterate through that.
This is what I am doing.
<s:iterator value='#request.BillSummaryTables' var="tableName" status="itStatus">
<div class="contentbox" role="content">
<table class="rpt">
<s:iterator value="#request.get('%{#tableName}').getData()" var="ocRow" status="itStatus">
<tr style="border:1px solid #CCCCCC">
<s:iterator value='#ocRow' var="cell" status="itStatus2">
<td>
<s:property value="#cell.getValue()"/>
</td>
</s:iterator>
</tr>
</s:iterator>
<tr>
<s:iterator value="#request.get('%{#tableName}').getData()" var="ocTotal">
<td>
<s:property value="#ocTotal"/>
</td>
</s:iterator>
</tr>
</table>
</div>
</s:iterator>
I have also tried
#request[<s:property value="#tableName" />].getData()
and
#request['<s:property value="#tableName" />'].getData()
and
#request.%{#tableName}
But nothing is returned in any case.
However, this code works fine if I hard code the values.
i.e. if i use: #request['other_charges'].getData()
Note: I am able to retrieve the list of tableName (#request.BillSummaryTables).
#1) You are using three nested iterators, but both the first and the second have an instance of IteratorStatus called itStatus; they must have different names to work.
#2) If the Lists corresponding to the Table name is, effectively, a List, then you should iterate the list, not the getData() stuff (what is that ?)
#3) Why using request ? why not simply using an HashMap on the Action (with the getter), adding elements dynamically using table names as key ?
#4) This #request[<s:property value="#tableName" />].getData() will obviously not work if put inside another Struts2 tag, like an Iterator (cannot nest Struts2 tags).
However, try something like this (I stripped the second iterator, make it running before, then add stuff), and see if it works (and what it prints):
<s:iterator value='#request.BillSummaryTables' var="tableName" status="statusAllTables">
<div class="contentbox" role="content">
<br/>==== START DEBUG ====
<br/>Current table name: [<s:property value="#tableName"/>]
<br/>Corresponding request object: [<s:property value="#request['%{#tableName}']"/>]
<br/>getData on that object: [<s:property value="#request['%{#tableName}'].getData()"/>]
<br/>===== END DEBUG =====
<table class="rpt">
<s:iterator value="#request['%{#tableName}'].getData()" var="ocRow" status="statusThisTable">
<tr style="border:1px solid #CCCCCC">
<s:iterator value='#ocRow' var="cell" status="statusThisField">
<td>
<s:property value="#cell.getValue()"/>
</td>
</s:iterator>
</tr>
</s:iterator>
</table>
</div>
</s:iterator>
EDIT
Ok, but then why are you using request.setAttribute ? Actions are created per-request... just use a private List<MyObjects> myObjects with its getter (public List<MyObject> getMyObjects()), and call it from JSP with <s:iterator value="myObjects"> (in your case, <s:iterator value="myObjects.data">.
Please note that .getData() in OGNL should become .data (i didn't noticed it before), removing the get, lowering the first letter of the method, and removing round brackets...
Retry and let us know.
I'm new to grails and I am trying to display all the data of a domain class from the database in a table. When I open the gsp file my table is blank. I'm not sure what I am doing wrong since I have been following every tutorial exactly with the same syntax. My domain class name is "Test" and here is my code for the table:
<g:each var="test" in="${tests}">
<tr>
<td>
${test.testName}
</td>
<td>
${test.numberofQuestions}
</td>
<td>
${test.responseType}
</td>
</tr>
</g:each>
If you are just trying to bring back all of your "Test" domain objects you would do something like this:
<g:each var="test" in="${com.mypackage.Test.list()}">
or
<g:each var="test" in="${com.mypackage.Test.listOrderByTestName()}">
if you wanted them in order by testName (be sure to use the actual package name in place of com.mypackage).
If you are trying to pass a tests variable into the GSP to be used as jonaldomo said in the comment we will need more information.
I have a loop inside a loop and want to substitute the value of first var into second. Below is the code snippet.
<g:each in="${tables}" status="i" var="table">
<div class="tabletitle">
${table.name}
</div>
<table>
<thead>
<tr>
<g:each in="${${table.name}DisplayColumns}" status="k" var="displayColumn">
<td>${displayColumn}</td>
</g:each>
</tr>
</thead>
<tbody>
....
....
</tbody>
</table>
</g:each>
${table.name} substitution in second g:each tag is not working. Any idea to make it work?
Try this:
<g:each in="${evaluate(table.name+'DisplayColumns')}" status="k" var="displayColumn">
Interesting, I've never used evaluate inside a gsp, as Kelly suggests. But may I suggest a less optimal approach?
You can store ${table.name} inside a variable with <g:set> ( http://grails.org/doc/2.0.x/ref/Tags/set.html )
Do you know that you can pass any object to a GSP? Even maps (you're trying to emulate maps, I don't know why), and use it like:
<g:each in="${displayColumns[table.name]}">
where displayColumns is a Map that contains columns for each table.
Btw, more clean way, is to use special object, that includes this data. Something like TableDetails that have List<String> getColumns() method. So you can use
<g:each in="${tables}" var="table">
${table.name}
<g:each in="${table.columns}" var="column">
${column}
</g:each>
</g:each>