简体   繁体   English

播放框架:将数据显示为html表

[英]Play framework: Display data as html table

I am developing a small project which has a couple of tables. 我正在开发一个有几个表的小项目。 One of them is Person table which has id and name as attributes. 其中之一是Person表,该表具有id和name作为属性。 I am successfully getting the resultset from database (using MySql) and can convert them into json. 我成功地从数据库中获取了结果集(使用MySql)并将其转换为json。 However, i am unable to find a way to display the json as a html table. 但是,我无法找到一种将json显示为html表的方法。 Is there a way to pass the list of Persons to the html and just display it in a table ?? 有没有一种方法可以将“人”列表传递给html并仅将其显示在表格中?

Application.java 应用程序

public class Application extends Controller {

@Inject
FormFactory formFactory;

public Result index() {
    return ok(index.render());
}

@Transactional(readOnly = true)
public Result getPersons() {
    List<Person> persons = (List<Person>) JPA.em().createQuery("select p from Person p").getResultList();
    return ok(toJson(persons));
}

index.scala.html index.scala.html

 @()
@main("Welcome to Play") {
<script type='text/javascript' src='@routes.Assets.at("javascripts/index.js")'></script>

<ul id="persons"></ul>

//logic to display table
}

index.coffee 索引咖啡

$ ->
  $.get "/persons", (persons) ->
    $.each persons, (index, person) ->
      $("#persons").append $("<li>").text person.id + person.name 

This displays a list of person objects. 这将显示人员对象列表。 But i need to display a html table 但是我需要显示一个html表

However, i am unable to find a way to display the json as a html table. 但是,我无法找到一种将json显示为html表的方法。 Is there a way to pass the list of Persons to the html and just display it in a table ?? 有没有一种方法可以将“人”列表传递给html并仅将其显示在表格中?

It doesn't appear you have read the Play manual very well. 您似乎没有很好地阅读Play手册。 However, I would not send JSON to the template. 但是,我不会将JSON发送到模板。 Instead, I would pass the 相反,我会通过

List<Persons> 

(Java Object) to the scala template and use the Scala Templating language to display the table. (Java对象)添加到scala模板,并使用Scala模板语言显示表。 I think you would benefit from reading the template help page here. 我认为您将从这里阅读模板帮助页面受益

Pay special attention to the Iterating section for doing a for loop. 要进行for循环,请特别注意“迭代”部分。 This is how I would do it (untested): 这就是我要做的(未测试):

index.scala.html index.scala.html

@(persons: List[Person])
@main("Welcome to Play") {
<script type='text/javascript' src='@routes.Assets.at("javascripts/index.js")'></script>

<ul id="persons"></ul>

//logic to display table
<table>
   @for(person <- persons){
      <tr>
          <td>@person.getName()</td>
          <td>@person.getId()</td>
      </tr>
    }
</table>
}

Note that in a Scala Template, instead of using the syntax of: 请注意,在Scala模板中,不要使用以下语法:

List<Person>

(like in Java), you use (如Java),您可以使用

List[Person]  

And for Application Controller, change the relevant part to: 对于Application Controller,将相关部分更改为:

public Result getPersons() {
    List<Person> persons = (List<Person>) JPA.em().createQuery("select p    from Person p").getResultList();
return ok(index.render(persons);

This assumes you have two fields in class Person: name and id. 假设您在Person类中有两个字段:name和id。 It also assumes that you have "get" methods for them. 它还假定您具有“获取”方法。 If they are public fields, you can use @person.name and @person.id. 如果它们是公共字段,则可以使用@ person.name和@ person.id。 Change those variables to match what you have in your Person class. 更改这些变量以匹配您的Person类中的变量。

You shouldn't need index.coffee then. 那你就不需要index.coffee了。 You also won't need the tag in your index.scala.html (unless you need it for other reasons). 您也将不需要index.scala.html中的标记(除非您出于其他原因需要它)。

On the help page mentioned above, you might also check out the "Declaring reusable blocks" section to create a resuable block to display each Person object. 在上述帮助页面上,您还可以签出“声明可重用块”部分,以创建一个可重用块来显示每个Person对象。 However, I would get the simplest version working first and then attempt this. 但是,我将首先使用最简单的版本,然后再尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM