在 Grails 中将 joinTables 呈现为列表
我有两个域类:汽车和驾驶员。我想制作一个 gsp 视图,在列表中显示汽车和驾驶员信息,该列表看起来或多或少是默认的脚手架列表。例如:
Make Model Driver Age
------------------------
Chevy Nova Joe 20
Chevy Nova Mike 30
Chevy Nova Sally 40
这是域类:
class Car {
String make
String model
static constraints = { }
static hasMany = [ drivers : Driver ]
static mapping = {
drivers joinTable: [name: 'Car_Driver', column: 'DRIVER_ID', key: 'CAR_ID']
}
}
class Driver {
String name
int age
static constraints = { }
}
制造一辆汽车并给它一些驱动程序:
def car = new Car(make: 'Chevy', model: 'Nova')
def driver1 = new Driver(name: 'Joe', age: 20)
def driver2 = new Driver(name: 'Mike', age: 30)
def driver3 = new Driver(name: 'Sally', age: 40)
car.addToDrivers(driver1)
car.addToDrivers(driver2)
car.addToDrivers(driver3)
car.save()
我需要在 CarController 和/或 gsp 视图中做什么才能发生连接列表(并且仍然可以使用分页)?
I've got two domain classes: Car and Driver. I'd like to make a gsp view that displays the Car and Driver info in a list that looks more or less a default scaffolded list. For example:
Make Model Driver Age
------------------------
Chevy Nova Joe 20
Chevy Nova Mike 30
Chevy Nova Sally 40
Here's the domain classes:
class Car {
String make
String model
static constraints = { }
static hasMany = [ drivers : Driver ]
static mapping = {
drivers joinTable: [name: 'Car_Driver', column: 'DRIVER_ID', key: 'CAR_ID']
}
}
class Driver {
String name
int age
static constraints = { }
}
Make a car and give it a few drivers:
def car = new Car(make: 'Chevy', model: 'Nova')
def driver1 = new Driver(name: 'Joe', age: 20)
def driver2 = new Driver(name: 'Mike', age: 30)
def driver3 = new Driver(name: 'Sally', age: 40)
car.addToDrivers(driver1)
car.addToDrivers(driver2)
car.addToDrivers(driver3)
car.save()
What do I need to do in my CarController and/or gsp view to make a joined list happen (and still work with pagination)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
Driver
只能拥有一辆Car
,则您需要一个Driver
来引用Car
并仅渲染脚手架 <驱动程序
的code>list。要调整列表列,您必须
grails生成视图
。如果一个
Driver
可以有很多Cars
,并且您不想将Car_Driver
表拉入可见域模型(它没有自己的域含义) ),使用 SQL 查询结果作为cardriverInstancesList
来创建类似脚手架的list
操作。就像这里:Grails 中的 SQL/数据库视图。只需检查
result
是否为PagedResultList
。如果没有,您可以手动创建一个PagedResultList
,它可以轻松地由List
和totalCount
构造而成,您可以使用 SQL 找到它们。If a
Driver
can have only oneCar
, you need aDriver
to referenceCar
and just to render scaffoldinglist
for aDriver
.To tweak list columns, you'll have to
grails generate-views
.If a
Driver
can have manyCars
, and you don't want to pullCar_Driver
table into visible domain model (it has no own domain meaning), make a scaffolding-likelist
action using SQL query result as acardriverInstancesList
. Like here: SQL/Database Views in Grails.Just check that
result
is aPagedResultList
. If not, you can create aPagedResultList
by hand, it is easily constructed from aList
andtotalCount
, which you can find with SQL.