Grails按用户ID查看和过滤事件

时间:2014-05-20 15:30:28

标签: grails view filtering

我有一个Domain类:

课程活动{     static belongsTo = [赞助商,旧]

String evento
Date data = new Date()
String local
Sponsor sponsor
Old old

static constraints = {
    data(blank: false)
    local(blank: false)
    evento(blank: false)
}

}

我自动生成视图和控制器(生成视图),现在当我登录时,在我的视图中,个人化我有各种事件......我如何按用户ID(登录)过滤结果?

我的页面是:

...
<div id="list-events" class="content scaffold-list" role="main">
        <h1>Agenda</h1>
        <br>
        <table>
        <thead>
                <tr>

                    <g:sortableColumn property="data" controller ="events" title="${message(code: 'events.data.label', default: 'Evento') } " />

                    <g:sortableColumn property="local" controller ="events" title="${message(code: 'events.local.label', default: 'Local')}" />

                    <g:sortableColumn property="evento" controller ="events" title="${message(code: 'events.evento.label', default: 'Data')}" />

                </tr>
            </thead>
            <tbody>
            <g:each in="${eventsInstanceList}" status="i" var="eventsInstance">
                <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">

                    <td><g:link action="show" id="${eventsInstance.id}">${fieldValue(bean: eventsInstance, field: "evento")}</g:link></td>

                    <td>${fieldValue(bean: eventsInstance, field: "local")}</td>

                    <td>${fieldValue(bean: eventsInstance, field: "data")}</td>

                </tr>
            </g:each>
            </tbody>
        </table>
        <div class="pagination">
            <g:paginate total="${eventsInstanceCount ?: 0}" />
        </div>
    </div>
</div>...

并在_form中:

<div class="fieldcontain ${hasErrors(bean: eventsInstance.old, field: 'data', 'error')} required">
<label for="data">
    <g:message code="events.data.label" default="Data" />
    <span class="required-indicator">*</span>
</label>
<g:datePicker name="data" precision="day"  value="${eventsInstance?.data}"  />

                      *                                 *                                 *                                 *          

我的控制员:

class EventsController {

static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond Events.list(params), model:[eventsInstanceCount: Events.count()]
}

def show(Events eventsInstance) {
    respond eventsInstance
}

def create() {
    respond new Events(params)
}

@Transactional
def save(Events eventsInstance) {
    if (eventsInstance == null) {
        notFound()
        return
    }

    if (eventsInstance.hasErrors()) {
        respond eventsInstance.errors, view:'create'
        return
    }

    eventsInstance.save flush:true

    request.withFormat {
        form {
            flash.message = message(code: 'default.created.message', args: [message(code: 'eventsInstance.label', default: 'Events'), eventsInstance.id])
            redirect eventsInstance
        }
        '*' { respond eventsInstance, [status: CREATED] }
    }
}

def edit(Events eventsInstance) {
    respond eventsInstance
}

@Transactional
def update(Events eventsInstance) {
    if (eventsInstance == null) {
        notFound()
        return
    }

    if (eventsInstance.hasErrors()) {
        respond eventsInstance.errors, view:'edit'
        return
    }

    eventsInstance.save flush:true

    request.withFormat {
        form {
            flash.message = message(code: 'default.updated.message', args: [message(code: 'Events.label', default: 'Events'), eventsInstance.id])
            redirect eventsInstance
        }
        '*'{ respond eventsInstance, [status: OK] }
    }
}

@Transactional
def delete(Events eventsInstance) {

    if (eventsInstance == null) {
        notFound()
        return
    }

    eventsInstance.delete flush:true

    request.withFormat {
        form {
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'Events.label', default: 'Events'), eventsInstance.id])
            redirect action:"index", method:"GET"
        }
        '*'{ render status: NO_CONTENT }
    }
}

protected void notFound() {
    request.withFormat {
        form {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'eventsInstance.label', default: 'Events'), params.id])
            redirect action: "index", method: "GET"
        }
        '*'{ render status: NOT_FOUND }
    }
}

}

如何过滤与特定登录相关联的每个事件?

由于

1 个答案:

答案 0 :(得分:1)

您需要修改控制器。这是您需要编辑的行:

respond Events.list(params), model:[eventsInstanceCount: Events.count()] 

将其更改为

def myEvents = Events.findAllBySponsor(sponsor, params)
def allMyEventsCount = Events.countBySponsor(sponsor)
respond myEvents, model:[eventsInstanceCount: allMyEventsCount] 

赞助商是您要过滤的用户。 (我假设已登录的用户是活动赞助商)

请注意,如果您为指定的排序参数指定了多个具有相同值的事件,则结果可能与刷新或翻阅结果的顺序不同。防止这种情况的一种方法是使用多个排序条件,其中一个标准始终位于唯一值属性(例如数据库标识)上。我通常用Criteria实现这一点。我可以推荐你http://grails.org/doc/latest/guide/GORM.html#querying