当前位置: 首页 > 工具软件 > GrailsUI > 使用案例 >

基于grails richUi的评分效果的开发

长孙智刚
2023-12-01
首先需要安装richuUi插件
创建一个domain:News
class News { 

String title
String content
Date time

static hasMany = [rates:Rate]

static constraints = {
title (blank:false)
content (blank:false,type:'text')
}

}


再创建一个domain:Rate
class Rate { 
News news
String rating = "0"

static belongsTo = [news:News]

static constraints = {
}

}


其中News和Rate的关系是一对多的关系。

然后修改news的show.gsp页面:
首先在head标签中增加<resource:rating />
然后增加
              <h5>新闻内容评分:</h5> 
<g:render template="/rating/rate" model=['id':id,'rating':0] />
<g:set var="a" value="${0}" />
<g:set var="b" value="${0}" />
<g:set var="c" value="${0}" />
<g:set var="d" value="${0}" />
<g:set var="e" value="${0}" />
<g:each in="${newsInstance.rates}">
<g:if test="${it.rating == '1'}">
<g:set var="a" value="${a+1}" />
</g:if>
<g:elseif test="${it.rating == '2'}">
<g:set var="b" value="${b+1}" />
</g:elseif>
<g:elseif test="${it.rating == '3'}">
<g:set var="c" value="${c+1}" />
</g:elseif>
<g:elseif test="${it.rating == '4'}">
<g:set var="d" value="${d+1}" />
</g:elseif>
<g:elseif test="${it.rating == '5'}">
<g:set var="e" value="${e+1}" />
</g:elseif>
</g:each>
评 1分的个数:${a}<br>
评 2分的个数:${b}<br>
评 3分的个数:${c}<br>
评 4分的个数:${d}<br>
评 5分的个数:${e}<br>


其中,_rate.gsp模板的代码如下:
<% 
def id= newsInstance?.id
%>
<%@ page contentType="text/html;charset=UTF-8" %>
<richui:rating dynamic="true" id="${id}" units="5" rating="${rating}" controller="rating" action="rate" />


这时,我们用到了RatingController,其代码如下:

class RatingController { 

def rate = {
def rating = params.rating
def id = params.id
def news = News.get(new Long(id))
def rate = new Rate(rating:rating)
news.addToRates(rate)
news.save()
render(template: "rate", model: [id:id ,rating: rating])
}

}


好了,最终的结果:

[img]http://dl.iteye.com/upload/attachment/220131/e9e6b8fc-2e7c-300a-934d-19ebdcc0dac8.jpg[/img]
 类似资料: