Suppose I have two domain objects, Document and Author
Class Document {
Author author
String title
}
Class Author {
String lastName
String firstName
String toString() {
return lastName + ", " + firstName
}
}
The view list.gsp looks something like this:
<g:sortableColumn property="title" title=... />
<g:sortableCOlumn property="author" title=... />
....
<td>${fieldValue(bean: documentInstance, field: "author"}></td>
<td>${fieldValue(bean: documentInstance, field: "title"}></td>
The displayed values in the table work as intended - the table row will show the author as (lastName, firstName) next to documentInstance.title.
However, clicking the Author column header to sort causes "documents" to be sorted by author.id .
What is the most expedient way to get sorting by author.toString() or "author.lastName, author.firstName" instead of sorting by author.id?
I'd prefer to avoid falling back to .withCriteria{} if possible - I have four different columns that need this functionality, and it seems like that would get messy.
You could use a derived property to create a virtual column to sort on:
Class Author {
String lastName
String firstName
String sortingName
static mapping {
// modify the SQL formula to use your DB's concatenation operator
sortingName formula: "`LAST_NAME` || ',' || `FIRST_NAME`)" // Standard SQL
}
String toString() { sortingName }
}
Then set your column to sortingName:
<g:sortableColumn property="author.sortingName" title=... />
(I'm kind of guessing here, but I think this should work.)
I'm just a Grails beginner so maybe my answer is not optimal, but this was the easiest way for me:
Instead of using Document.list(params) I used Document.findAll(). Is worth mentioning that in my application I do needed some kind of filter in my lists, so findAll() was the best approach. Anyways, here is how I would do it:
Document.findAll( "from Document as d order by d." + params.sort + ' ' + params.order, params ) //supports pagination
And in the View:
<g:sortableCOlumn property="author.lastName" title=... />
Related
I'm just inquisitive if this is possible. If so, please help me.
Everything is already set and valid like the:
-Model.info:
public class info{
public string information{get;set;}
}
-ViewBag.Infos:
ViewBag.Infos = new SelectList(new []{"Option1","Option2"});
-I am not finding or encountering an error.
So it goes like this:
#foreach(var nom in Model.info)
{
#Html.DropDownList("Infos",new{#Value = "nom.information"})
}
Is it possible to put the foreach value of nom into each dropdownlist's value?
First error is that you are iterating over Model.info that is an string (is an iteration over every char, the type of nom would be char).
By the other hand, you must define a DropDownList with the ViewBag elements, something like that:
#Html.DropDownList("info", ViewBag.info, null, Model.info)
I've been battling this for two days and am at a loss. I'm attempting to create node relationships and am severely failing.
Here is my code for creating and running the relationship.
var query = graphClient.Cypher
.Match("(apt_1:AttackPatterns)", "(apt_2:AttackPatterns)")
.Where((AttackPatterns apt_1) => apt_1.Id == Convert.ToInt64(apt.ID))
.AndWhere((AttackPatterns apt_2) => apt_2.Id == Convert.ToInt64(rt.Relationship_Target_ID))
.CreateUnique("(apt_1)-[:" + rtrn.ToString() + "]->(apt_2)");
query.ExecuteWithoutResults();
Here is the AttackPatterns class.
public class AttackPatterns
{
public long Id { get; set; }
public string Name { get; set; }
}
During runtime, the value for query equates, in one iteration, to the following:
MATCH (apt_1:AttackPatterns), (apt_2:AttackPatterns)\r\nWHERE (apt_1.Id = \"1\")\r\nAND (apt_2.Id = \"122\")\r\nCREATE UNIQUE (apt_1)-[:ChildOf]->(apt_2)
I notice the "\r\n" characters. I also notice quotes around 1 and 122. When I paste this into the Neo4j web interface replacing "\r\n" with actual new lines and remove the "\" escape character before the quotes, it fails. If I remove the quotes around the 1 and 122, it successfully creates the relationship.
I'm really not sure what I'm doing wrong and would appreciate any assistance!
I've made some assumptions about types, as I think I know what's going on - basically - the Convert.ToInt64 isn't called before the query is made, you have to do it outside of the query generation:
//Given:
var apt = new { ID = "1" };
//Convert outside query
var ap1Id = Convert.ToInt64(apt.ID);
//Use in query
var query = gc.Cypher
.Match("(apt_1:AttackPatterns)", "(apt_2:AttackPatterns)")
.Where((AttackPatterns apt_1) => apt_1.Id == ap1Id) // <-- using locally created var
/* etc */
The \r\n is just for formatting when you look at the DebugQueryText property, they're not sent across, the only problem is the " around the numbers.
Please feel free to add this as a bug to the github project and I'll have a look into it, ideally it would be executed beforehand, but it's possibly like this for a reason.
I have to create method for table in Dynamics AX which concatenate two fields from one table in one field. For example Name+Surname displays in one field. How can I do it?
Create display method like this:
public display PersonName fullName()
{
return this.FirstName + ' ' + this.LastName;
}
See also edit method.
To concatenate two fields (or more) you can use StrFmt() method.
Example:
str fullName;
fullName = strFmt("%1 %2", this.Name, this.Surname);
I am using Grails 2.2.4 and having one Domain contains value as map and I want to find domain object using key of map. Please help me to resolve this issue.
Student.groovy
package com.grails
import java.util.Map;
class Student {
String firstName
String lastName
Map address
static constraints = {
}
}
When My application are run I can see that Grails application create tables in database are as follow:
1) first table
student
id
version
first_name
last_name
indexes
2) second table
student_address
address
addres_idx
addres_elt
When I save Domain as:
def std = new Student()
std.firstName = 'Piyush'
std.lastName = 'Chaudhari'
std.address = [city:'Surat',state:'Gujarat',pincode:'38001']
std.save(flash:true)
values are insert in database as follow:
student table
ID VERSION FIRST_NAME LAST_NAME
1 0 Piyush Chaudhari
student_address table
ADDRESS ADDRESS_IDX ADDRESS_ELT
1 city Surat
1 state Gujarat
1 pincode 38001
Now, I want data or row using GORM like Student.findBy_____ or Student.findAllBy______
where 'city' = surat
Any one can help me to resolved this issue?
You can use:
Student.findBy<FieldName1>And<FieldName2> (<FieldNameParameter1>, <FieldNameParameter2>)
Or Either:`
Student.list().find { it.address.city == 'Surat' }
Student.list().findAll { it.address.city == 'Surat' }
`
I don't think that you can search things like this using maps.
Maybe you can do this:
def students = Student.list()
def result = students.each { student -> student.address.city == 'Surat' }
println("Resultado" + result)
But this is a very bad way to do this kind of things
Define an address class, and then add an address field to the student class (this will change how your tables are mapped in the database):
class Student {
String firstName
String lastName
Address address
static constraints = {
}
}
class Address {
String city
String state
String pincode
}
Address should be another entity in your domain, not a map of values. Remember that Grails GROM is an ORM, so you should design your domain using a OOP model in order to take advantage of the dynamic finders and criterias for doing queries.
With those changes in place, you can now use a simple criteria:
def students = Student.withCriteria{
'address'{
eq('city', 'surat')
}
}
More information about criterias in the grails docs:
http://grails.org/doc/latest/ref/Domain%20Classes/withCriteria.html
http://grails.org/doc/latest/guide/single.html#criteria
If you want to use Dynamic finders, you will have to get all the address with city = 'surat' and then use a findByAddressInList(...). But i think that in this case, criterias is a better approach
I'm playing around with Martini, and for some reason I can't get the contrib binding package to work.
My struct isn't having the values bound to. I've reduced the code down to it's simplest form, but it still doesn't work.
Can anyone see what I'm doing wrong?
package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/binding"
"net/http"
)
var html string = `<form method="POST" enctype="application/x-www-form-urlencoded"><input name="un" type="text" /><input type="submit" value="Some button" /></form>`
type FormViewModel struct {
Username string `form: "un"`
}
func main() {
m := martini.Classic()
m.Get("/", func(w http.ResponseWriter) {
w.Header().Add("content-type", "text/html")
w.Write([]byte(html))
})
m.Post("/", binding.Form(FormViewModel{}), func(vm FormViewModel) string {
return "You entered: " + vm.Username
})
m.Run()
}
It is just a parsing issue in the definition of the tag associated to the field of the structure.
You need to remove the blank character after form:
If you write the structure as follows:
type FormViewModel struct {
Username string `form:"un"` // No blank after form:
}
... it should work better.
The Go language specification says:
By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ' '), quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using U+0022 '"' characters and Go string literal syntax.
Apparently, the parser implemented in the reflect package does not tolerate a space after the colon.