thymeleaf +spring date converting - thymeleaf

This is my data model. I want to use date from here.
I do this in my html:
<table th:if="${!commentsInTask.empty}">
<tbody>
<tr th:each="Comments : ${commentsInTask}">
<tr th:each="comment : ${Comments}">
<td th:text="${comment.user}">user ...</td>
<td th:text="${comment.createdAt}">date ...</td>
</tr>
</tr>
</tbody>
</table>
but it brings:
<table>
<tbody>
<td>JACK</td>
<td>1.476787930289E9</td>
</tr>
</tr>
</tbody>
</table>
This part is unix timedate:
1.476787930289E9
but in the picture at beginning i posted, you saw. Timme is not that.
This is in domain
public String getCreatedAtString() {
return createdAtString;
}
public TaskComment setCreatedAtString(String createdAtString) {
this.createdAtString = createdAtString;
return this;
}
private ZonedDateTime createdAt = ZonedDateTime.now();
Why cantt i see in date format which in picture at the beginning?

Use Thymeleaf formatting:
<td th:text="${#dates.format(comment.createdAt, 'dd-MM-yyyy HH:mm:ss')}">date</td>
You'll get an output in the following format: 18-Oct-2016 14:44:05.
#dates: methods for java.util.Date objects: formatting, component extraction, etc.
To convert your createdAt field to java.util.Date type use:
Date date = Date.from(java.time.ZonedDateTime.now().toInstant());
Or just use java.util.Date type:
private Date createdAt = new Date();
this will set cheatedAt to current date.
Also you can add thymeleaf-extras-java8time dependency to your project to work with your ZonedDateTime type.
This module adds a #temporals object similar to the #dates or #calendars ones in the Standard Dialect, allowing the formatting and creation of temporal objects from Thymeleaf templates:
Then you can use ZoneDateTime with the specified pattern:
${#temporals.format(temporal, 'dd/MM/yyyy HH:mm')}
See more at Thymeleaf - Module for Java 8 Time API compatibility.

Related

How to obtain a space between two elements in DataTable?

I am printing the data from ViewModel into DataTable in the following way, but how to obtain a space between #item.FirstName and #item.LastName so that the output can be of the form FirstName LastName? At present, I am getting the Name as FirstNameLastName. Ex: MarkStevens. I want to print it as Mark Stevens.
#if(Model.Count() != 0)
{
<thead>
<tr>
<th data-column-id="Name">Name</th>
...
</tr>
</thead>
}
else
{
<h3 style="text-align:center">No Records Found</h3>
}
<tbody>
#foreach(var item in Model)
{
<tr>
<td>
#(if (item.UserId != null)
{
#item.FirstName #item.LastName
}
else
{
#item.OtherUser
}
</td>
</tr>
}
</tbody>
You can use #Html.Raw helper which means, you render whatever inside without any html encoding like space or string:
#item.FirstName #Html.Raw(" ") #item.LastName
If you want html, you can also use a simple non-breaking spaces:
#item.FirstName #item.LastName
How about string format the output: $"{#item.FirstName} {#item.LastName}"
use string.Format. i feel this is very clean approach.
string.Format("{0} {1}", #item.FirstName,#item.LastName)

polymer expressions access property by its name

trying to render a table as a polymer element
<table>
<thead>
<tr>
<th template repeat='{{ column in columns}}'>
{{column.displayName}}
</th>
</tr>
</thead>
<tbody>
<tr template repeat='{{ row in data}}'>
<td template repeat='{{ column in columns}}'>
{{row[column.name]}}
</td>
</tr>
</tbody>
</table>
the following expression doesn't seem to work as i expected
{{row[column.name]}}
column.name is the property name that i want to access in the row object but it gives the following exception for all properties
NoSuchMethodError: method not found: '[]'
Receiver: Instance of 'Product'
Arguments: ["id"]
is this the right way to access a property by name?
my row model looks like this
class Product extends Observable{
int id;
String name;
String category;
}
One work around which i don't like is overloading the [] in the row class
operator [](String fieldName){
var im = reflect(this);
return im.getField(new Symbol(fieldName)).reflectee;
}
This should work if 'row' is a map.

Kendo grid MVC number link column sorting

I have a Kendo grid on a MVC .cshtml view page:
#model IEnumerable<Models.GetItems>
<script>
$(document).ready(function () {
$("#grid").kendoGrid({
sortable: true
});
});
</script>
<table class="table" id="grid">
<thead>
<tr>
<th data-field="Quantity">
Qty
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.ActionLink(item.Quantity.ToString(), "kendo", "Groups", new { ID = item.ID }, null)
</td>
</tr>
}
</tbody>
</table>
It displays the way I want; as a number with a link to a drill down page, but the sorting doesn't work at all.
How can I tell the grid I want the data type to be a number so it can sort it like a number?
(item.Quantity is Int16 from the model, but had to make it a string for the ActionLink to work)
(I'm open to binding the grid differently if I have to (bind to json output from controller and/or use rowTemplate and/or bind to empty div and define columns in JS possibly with template), but not sure at this point if that will matter, seems like a data type issue regardless of binding method???)

Showing a dynamic table in GSP where column & data comes at run time

getting two arrays from controller and code is --
Sql db = new Sql(dataSource_wldb1) // Create a new instance of groovy.sql.Sql with the DB of the Grails app
def ivrColumns = []
db.eachRow(ivrColumnsQuery) {
rsRow ->
ivrColumns.add(rsRow.name) }
def ivrResults = []
db.eachRow(mssqlQuery) {rows ->
//print rows
ivrResults.add(rows)
}
one has all column names & other has all row data.as below-
return render(view:'xref',model:[ivrcolumns:ivrColumns,ivrresults:ivrResults] )
getting data in below format-
Columns
[ClientKey, Abbr, ConfigKey, Federal, State, DMA, Internal, Wireless, CRssing, CurfewExemption, CampaignID]
Data
[groovy.sql.GroovyResultSetExtension#12f8d75, groovy.sql.GroovyResultSetE
oovy.sql.GroovyResultSetExtension#12f8d75, groovy.sql.GroovyResultSetExtension#1
roovyResultSetExtension#12f8d75, groovy.sql.GroovyResultSetExtension#12f8d75, gr
tSetExtension#12f8d75, groovy.sql.GroovyResultSetExtension#12f8d75, groovy.sql.G
ion#12f8d75, groovy.sql.GroovyResultSetExtension#12f8d75]
view code is---
<g:if test="${ivrcolumns != null }">
<center>Database Location - WLDB1 <br>DB Name - IVR_GUARDIAN </center><br><br>
<table class="table loadTable" >
<thead>
<tr bgcolor="#f0f0f0" >
<g:each in="${ivrcolumns}" status="ii" var="columnivr">
<td nowrap>${columnivr}</td>
</g:each>
</tr>
</thead>
<tbody>
<g:each in="${ivrresults}" status="jj" var="hed">
<tr>
<g:each in="${ivrcolumns}" status="kk" var="col">
<td nowrap>${hed.col}</td> ///please suggest how to do it.
</g:each>
</tr>
</g:each>
</tbody>
</table>
now want to show in GSP page .i am able to display the column but having hard time to display data.not getting how the dot will be used to get correct data to each column.
Will appreciate any help.
thanks
Assuming that's just a sql result, you can just call ${ hed[ col ] } or ${ hed."$col" }

How to search JodaDate And JodaTime using criteria?

I'm new to grails.I'm using joda plugin for date and time.So i'm displaying date and time in separate column.Now i want to search date using criteria.
I used below code in gsp page to search date.
<tr class="prop">
<td valign="top" class="name">
<label for="createdOn"><g:message code="asset.createdOn.label" default="Created On" /></label>
</td>
<td valign="top" class="value ${hasErrors(bean: assetInstance, field: 'createdOn', 'errors')}">
<joda:datePicker name="createdOn" precision="date" value="${assetInstance?.createdOn}" />
</td>
</tr>
i used below code in controller by using criteria.
def search= {
if (request.method == 'POST'){
def criteria = Asset.createCriteria()
def results = criteria {
or {
gt('createdOn','%' + params.createdOn + '%').toDate()
}
}
render(view:'list', model:[ assetInstanceList: results,assetInstanceTotal: Asset.count() ])
}
}
But it showing error.please guide me to solve this problem.
You probably want to bind your search to a command so that you have an actual bound Date object instead of a String parameter.
class SearchCommand {
LocalDate createdOn
...
}
You'll also need to fix your criteria.
def search = { SearchCommand command ->
...
def results = criteria {
or {
gt('createdOn', command.createdOn)
...
}
}

Resources