使用 Groovy 按 lastModified() 日期对 HTML 表进行排序
我有一个 groovy 脚本,它从 MYSQL 数据库中提取许多字段并将数据插入到 HTML 表中,同时我还找到了提取到数据库中的文件的 lastModified()
日期。
我发现了一个相当相似的主题:“按lastModified()对文件进行排序”,但是我的问题是我从未真正将数据放入地图或列表中,而是构建xml并使用sql.eachRow( query)
将正确的字段插入表中,然后在 xml 本身中查找 lastModified()
日期。
我无法在查询中进行排序,因为我必须从 groovy 脚本获取 lastModified()
日期。所以我有两个问题,是否可以使用我发布的代码中的设置按上次修改日期进行排序?如果是这样,我需要在哪里实际执行排序,以便在 HTML 表上正确排序?我应该说我对 Groovy 和 Java 非常陌生,在尝试弄清楚这些事情时我很糟糕,所以任何关于您在哪里找到这些信息的提示都会很棒。
类似的主题,但不完全是我正在寻找的主题:
- 如何对从 MySQL 调用的 HTML 表行进行排序 <-- 确实在看这个,但它是在 php
- http://groovy.329449.n5.nabble.com/ Sorting-files-by-lastModified-td362691.html <--这但试图排序一个表
- http://www.kryogenix.org/code/browser/sorttable/# Totalsrows <--这很好,但是在 groovy 中可能吗?
我的代码,我必须改变一些东西:
sql = Sql.newInstance("location of database", "username", "password", "driver")
writer = new StringWriter()
def xml = new MarkupBuilder(writer)
rptDate = new java.util.Date()
query =
"""
query stuff
"""
xml.html(){
xml.head(){
xml.title("Title")
xml.body(){
xml.h1("Title")
xml.table(border:1, cellpadding:5){
xml.tr(){
xml.th("ID")
xml.th("Date Added")
xml.th("Hospital Name")
xml.th("Total Daily Clients")
xml.th("Total Daily Pets")
xml.th("Last Upload Date")//lastModified() date
}//end headings
//insert data from query into each row of the table
sql.eachRow(query)
{row ->
xml.tr(align:'center'){
xml.td("${row.ID}")
xml.td("${row.DateAdded}")
xml.td("${row.HospitalName}")
xml.td("${row.TotalDailyClients}")
xml.td("${row.TotalDailyPets}")
//find lastModified() dates for incoming files and format them
mod = new File("/home/me/folderforfiles/${row.ID}.zip").lastModified()
fd = new Date(mod).format("EEE MMM dd hh:mm:ss a yyyy")
//insert into table
xml.td(fd)
}//end table data
}//end loop
}//end table
}//end body
}//end title
}//end html
println writer.toString()
I have a groovy script that pulls a number of fields from a MYSQL database and inserts the data into an HTML table, along with this I find the lastModified()
date of the files that are pulled into the database.
I found a fairly similar topic: "Sorting files by lastModified()", however my problem is that I never actually get the data into a map or list, instead I build the xml and call the query using sql.eachRow(query)
to insert the proper fields into the table and then find the lastModified()
date within the xml itself.
I can't do the sort in my query because I have to get the lastModified()
date from the groovy script. So I have two questions, is it possible to do a sort by last modified date using the set up in my code that I posted? And if so where would I need to actually perform the sort so that it is sorted correctly on the HTML table? I should say I am very new to Groovy and Java and am terrible when it comes to trying to figure out these kinds of things, so any hints as to where you found this information would be great.
Similar topics but ones that are not exactly what I am looking for:
- How to sort rows of HTML table that are called from MySQL <-- really looking at this but it is in php
- http://groovy.329449.n5.nabble.com/Sorting-files-by-lastModified-td362691.html <--this but trying to sort a table
- http://www.kryogenix.org/code/browser/sorttable/#totalsrows <--this would be nice but is it possible in groovy?
My code, I had to change some things:
sql = Sql.newInstance("location of database", "username", "password", "driver")
writer = new StringWriter()
def xml = new MarkupBuilder(writer)
rptDate = new java.util.Date()
query =
"""
query stuff
"""
xml.html(){
xml.head(){
xml.title("Title")
xml.body(){
xml.h1("Title")
xml.table(border:1, cellpadding:5){
xml.tr(){
xml.th("ID")
xml.th("Date Added")
xml.th("Hospital Name")
xml.th("Total Daily Clients")
xml.th("Total Daily Pets")
xml.th("Last Upload Date")//lastModified() date
}//end headings
//insert data from query into each row of the table
sql.eachRow(query)
{row ->
xml.tr(align:'center'){
xml.td("${row.ID}")
xml.td("${row.DateAdded}")
xml.td("${row.HospitalName}")
xml.td("${row.TotalDailyClients}")
xml.td("${row.TotalDailyPets}")
//find lastModified() dates for incoming files and format them
mod = new File("/home/me/folderforfiles/${row.ID}.zip").lastModified()
fd = new Date(mod).format("EEE MMM dd hh:mm:ss a yyyy")
//insert into table
xml.td(fd)
}//end table data
}//end loop
}//end table
}//end body
}//end title
}//end html
println writer.toString()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您首先将 SQL 结果加载到
List
中,那么您可以对此映射进行排序,并迭代它以生成 XML:我还没有尝试过,但它应该做您想做的事情...如果您遇到任何错误,请告诉我,我会解决它们...
当然,如果您有成千上万的行,则将所有行加载到内存中将不起作用...如果是这种情况,那么您应该将lastModified Date存储在数据库中,并使用sql查询对结果进行排序
If you load the SQL results into a
List
first, then you can sort this map, and iterate through it to generate your XML:I haven't tried that, but it should do what you want to do... Let me know if you get any errors, and I'll sort them out...
Of course, loading all the rows into memory will not work if you have thousands and thousands of rows... If that is the case, then you should store the lastModified Date in the database, and sort the results using the sql query