How to split a string and get first words?
studentController.groovy
def student = {
def ex = new ArrayList()
ex[0]= "Steven | ABCDEF0123456"
ex[1]= "Steven | ABCDEF0123456"
//student's value
[studentlist:ex] //send to gsp
}
student.gsp
<g:each in="${studentlist}" status="i" var="stdnt">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td>${stdnt}</td> //i want this show "Steven"
<td>${stdnt}</td> // and this show ABCDEF0123456
</tr>
</g:each>
How can I get the first word and second word?
You want Java's String.split() method:
List ex = "Steven | ABCDEF0123456".split('\\|')*.trim()
[studentlist:ex] //send to gsp
If you know there are only two items in the list, then you can reference them via studentlist[0] and studentlist[1]. It's a little unclear what you expect from the controller currently (a list of lists?):
<td>${studentlist[0]}</td>
<td>${studentlist[1]}</td>
Related
I have some code in my view which displays an employee's info (name, address, etc), within a grid. I'm having a problem however, when an employee's "Address 2" has data. In this circumstance, I need to place the Address 2 on a new line, within the same grid cell as Address 1.
So it should display as follows:
Name
Address
John Smith
123 My St
Amy Andrews
456 Main StApt B.
Instead however, my Address 2 is displaying the following, which physically displays the "br" tag:
456 Main St<br>Apt B.
Here's my code, which basically says, display "AddressLine1" and IF "AddressLine2" is not null, then include a line break, followed by AddressLine2.
I know I can get this to work by breaking out the AddressLine1 & AddressLine2 segments & I'm not against it, but I'd prefer to keep it similar to the code that already exists, which is written similar to the following, if possible:
#if (member.Employee != null)
{
<td>
#(member.Employee.FullName != null ? member.Employee.FullName : "-" )
</td>
<td>
#(member.Employee.AddressLine1 != null ? member.Employee.AddressLine1 +
(member.Employee.AddressLine2 != null ? "<br/>" +
member.Employee.AddressLine2 : "")
: "-" )
</td>
...
}
Thanks for any help.
The code doesn't emit the tag <br/> but a string that contains "<br/>". Use an #if block to emit the data you want instead, the same way you did for member.Employee :
<td>
</td>
#(member.Employee.FullName ?? "-" )
<td>
#(member.Employee.AddressLine1 ?? "-")
#if(member.Employee.AddressLine2 != null)
{
<br/>
#(member.Employee.AddressLine2)
}
</td>
if array data is
String sampdata[]= new String [22];
sampdata[0] ="a";,sampdata[1] ="b";
sampdata[2] ="c";,sampdata[3] ="d";
sampdata[4] ="e";,sampdata[5] ="f";
sampdata[6] ="g";,sampdata[7] ="h";
sampdata[8] ="i";,sampdata[9] ="j";
sampdata[10] ="k";,sampdata[11] ="l";
sampdata[12] ="m";,sampdata[13] ="n";sampdata[14] ="o";
sampdata[15] ="m";,sampdata[16] ="n";sampdata[174] ="o";
sampdata[18] ="m";,sampdata[19] ="n";sampdata[20] ="o";
sampdata[21] ="m";
and jsp use jstl foreach
<c:forEach var="list" items="${sampdata" begin="0" end="9" step="2" varStatus="status">
<tr>
<td>${list}</td> //a
<td>${list}</td> //td : b
<td></td>
</tr>
</c:forEach>
i mean
check this java array
for (int i=0 ; i<9;i +=2){
System.out.println(i)
System.out.println(i+1) <- i want like this.
}
i want "td:b" view b.
how to fix that?
"foreach" can use like that?
Check documentation on loop scoped variable with nested visibility that
lets authors use the status object to obtain information about the iteration range, step, and current object.
<c:forEach items="${sampdata}" step="2" varStatus="loop">
<c:out value="${sampdata[loop.index]}"/>
<c:out value="${sampdata[loop.index+1]}"/>
</c:forEach>
I am using nuget Package named ExcelDataReader in MVC application to read Excel sheet and convert it to HTML.All sounds goof but i am not able to read special character from excel sheet.For example is cell value is "59%" but its reading 0.59.So how can i get exact same string value from excel.I am using following code in Controller to read and display excel to html.
public ActionResult ViewExcelFileData(long? id, int sheetindex)
{
var excelfile = db.ExcelUpload.FirstOrDefault(x => x.FileID == id);
string filePath= string.Format(Server.MapPath("~/App_Data/ExcelUploads/Labor_Excel/") + excelfile.FileName);
System.IO.File.GetAccessControl(filePath);
FileStream stream = System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read);
IExcelDataReader reader = null;
if (excelfile.FileName.EndsWith(".xls"))
{
reader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else if (excelfile.FileName.EndsWith(".xlsx"))
{
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
reader.IsFirstRowAsColumnNames = true;
DataSet result = reader.AsDataSet();
reader.Close();
return View(result.Tables[sheetindex]);
}
By returning datatable in View side I am using Following Code in Cshtml.
<table class="gridtable" id="table-1">
<thead class="fixed_headers">
<tr>
#foreach (DataColumn col in Model.Columns)
{
<th>#col.ColumnName</th>
}
</tr>
</thead>
<tbody>
#foreach (DataRow row in Model.Rows)
{
<tr>
#foreach (DataColumn col in Model.Columns)
{
<td>#row[col.ColumnName]</td>
}
</tr>
}
</tbody>
</table>
So is it possible in controller side or View side I can get 59% instead of 0.59?
Here is Two images in which one is belongs to excel and second is HTML.
IMAGE :1
IMAGE 2 :
ExcelDataReader currently does not expose any formatting information, it provides you with the raw values except for dates that are converted to DateTime.
There's an open issue about extending it to provide formatting information, https://github.com/ExcelDataReader/ExcelDataReader/issues/215.
Instead of using the AsDataSet() method, you could read the row one by one and read the value as string. Drawback for this is you need to specify the columns one by one which is a maintainability issue.
while (reader.Read())
{
var value = reader.GetString(0); //Get value of first column
}
I suspect that the AsDataSet() converts the data to its respective data type that's why you are seeing the converted decimal value instead of the actual value in the excel.
Basically what I want is:
<g:fancyJoin in="${myList}" var="item" separator=", ">
<g:link controller="foo" action="bar" id="${item.id}">${item.label}</g:link>
</g:fancyJoin>
and for
def mylist = [[id:1, label:"first"], [id:2, label:"second"]]
it should output:
first, second
The key difference between this and the existing join tag is that I need it to basically do a collect and apply tags over the initial list before performing the join operation
You shouldn't do this in a GSP. Cluttering your view with loops and conditionals makes it hard to maintain the code and forces you to test with functional tests which are quite slow. If you do this in a taglib you clean up the view and testing is very easy.
You could define a custom tag, something like:
def eachJoin = {attrs, body ->
def values = attrs.remove('in')
def var = attrs.remove('var')
def status = attrs.remove('status')
def delimiter = attrs.remove('delimiter')
values.eachWithIndex {entry, i ->
out << body([
(var ?: 'it') : entry,
(status ?: 'i') : i
])
if(delimiter && (i < values.size() - 1)) {
out << delimiter
}
}
}
Usage:
<g:eachJoin in="${myList}" var="item" delimiter=", ">
<g:link controller="foo" action="bar" id="${item.id}">${item.label}</g:link>
</g:eachJoin>
In a controller, I have populated a map that has a string as key and a list as value; in the gsp, I try to show them like this:
<g:each in="${sector}" var="entry" >
<br/>${entry.key}<br/>
<g:each in="${entry.value}" var="item" >
${item.name}<br/>
</g:each>
</g:each>
The problem is that item is considered as string, so I get the exception
Error 500: Error evaluating expression [item.name] on line [11]:
groovy.lang.MissingPropertyException: No such property: name for class:
java.lang.String
Any hints on how to fix it other than doing the find for the item explicitly in the gsp ?
I did the poc which works fine for me
class Item{
String name
}
List items = [new Item(name:"laptop" ,
new Item(name:"mouse")]
Map sector =[:]
sector.manager = items
sector.manager.each{item->
println item.name
}
Even If it doesn't work, Try declaring Map sector as
Map < String, List < Item > > sector =[:]