如何使用grails中的ajax-jquery创建表

时间:2016-04-04 04:02:15

标签: grails

如何使用grails中的 ajax-jquery 创建表格。 请帮帮我

我只想在grails中使用 ajax-jquery 创建表格。

这是我的代码

Project.groovy

package com.moog

class Project {

String project
Workpackage workpackage
String activity
static hasMany=[workpackage:Workpackage]


static constraints = {
    project  blank:false,unique:true
     workpackage  blank:false,unique:true
     workpackage nullable:true
     activity  blank:false,unique:true
     activity nullable:true
     project nullable:true
        }
static mapping={


}
}

workpackage.groovy

package com.moog

class Workpackage {

static belongsTo=[project:Project]

String workpackage

static constraints = {

    project nullable:true

    workpackage blank:false,unique:true

    workpackage nullable:true

}
}

project.controller

package com.moog

import org.springframework.dao.DataIntegrityViolationException

class ProjectController {

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

def index() {
    redirect(action: "list", params: params)
}


def list(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    [projectInstanceList: Project.list(params), projectInstanceTotal: Project.count()]

}

def create() {
    [projectInstance: new Project(params)]

        }

def save() {
    def projectInstance = new Project(params)
    if (!projectInstance.save(flush: true)) {
        render(view: "create", model: [projectInstance: projectInstance])
        return
    }

    flash.message = message(code: 'default.created.message', args: [message(code: 'project.label', default: 'Project'), projectInstance.id])
    redirect(action: "show", id: projectInstance.id)
}

def show(Long id) {
    def projectInstance = Project.get(id)
    if (!projectInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'project.label', default: 'Project'), id])
        redirect(action: "list")
        return
    }

    [projectInstance: projectInstance]
}

def edit(Long id) {
    def projectInstance = Project.get(id)
    if (!projectInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'project.label', default: 'Project'), id])
        redirect(action: "list")
        return
    }

    [projectInstance: projectInstance]
}

def update(Long id, Long version) {
    def projectInstance = Project.get(id)
    if (!projectInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'project.label', default: 'Project'), id])
        redirect(action: "list")
        return
    }

    if (version != null) {
        if (projectInstance.version > version) {
            projectInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                      [message(code: 'project.label', default: 'Project')] as Object[],
                      "Another user has updated this Project while you were editing")
            render(view: "edit", model: [projectInstance: projectInstance])
            return
        }
    }

    projectInstance.properties = params

    if (!projectInstance.save(flush: true)) {
        render(view: "edit", model: [projectInstance: projectInstance])
        return
    }

    flash.message = message(code: 'default.updated.message', args: [message(code: 'project.label', default: 'Project'), projectInstance.id])
    redirect(action: "show", id: projectInstance.id)
}

def delete(Long id) {
    def projectInstance = Project.get(id)
    if (!projectInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'project.label', default: 'Project'), id])
        redirect(action: "list")
        return
    }

    try {
        projectInstance.delete(flush: true)
        flash.message = message(code: 'default.deleted.message', args: [message(code: 'project.label', default: 'Project'), id])
        redirect(action: "list")
    }
    catch (DataIntegrityViolationException e) {
        flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'project.label', default: 'Project'), id])
        redirect(action: "show", id: id)
    }
}
}

workpackage.controller

package com.moog

import org.springframework.dao.DataIntegrityViolationException

class WorkpackageController {

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

def index() {
    redirect(action: "list", params: params)
}

def list(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    [workpackageInstanceList: Workpackage.list(params), workpackageInstanceTotal: Workpackage.count()]
}

def create() {
    [workpackageInstance: new Workpackage(params)]
}

def save() {
    def workpackageInstance = new Workpackage(params)
    if (!workpackageInstance.save(flush: true)) {
        render(view: "create", model: [workpackageInstance: workpackageInstance])
        return
    }

    flash.message = message(code: 'default.created.message', args: [message(code: 'workpackage.label', default: 'Workpackage'), workpackageInstance.id])
    redirect(action: "show", id: workpackageInstance.id)
}

def show(Long id) {
    def workpackageInstance = Workpackage.get(id)
    if (!workpackageInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'workpackage.label', default: 'Workpackage'), id])
        redirect(action: "list")
        return
    }

    [workpackageInstance: workpackageInstance]
}

def edit(Long id) {
    def workpackageInstance = Workpackage.get(id)
    if (!workpackageInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'workpackage.label', default: 'Workpackage'), id])
        redirect(action: "list")
        return
    }

    [workpackageInstance: workpackageInstance]
}

def update(Long id, Long version) {
    def workpackageInstance = Workpackage.get(id)
    if (!workpackageInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'workpackage.label', default: 'Workpackage'), id])
        redirect(action: "list")
        return
    }

    if (version != null) {
        if (workpackageInstance.version > version) {
            workpackageInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                      [message(code: 'workpackage.label', default: 'Workpackage')] as Object[],
                      "Another user has updated this Workpackage while you were editing")
            render(view: "edit", model: [workpackageInstance: workpackageInstance])
            return
        }
    }

    workpackageInstance.properties = params

    if (!workpackageInstance.save(flush: true)) {
        render(view: "edit", model: [workpackageInstance: workpackageInstance])
        return
    }

    flash.message = message(code: 'default.updated.message', args: [message(code: 'workpackage.label', default: 'Workpackage'), workpackageInstance.id])
    redirect(action: "show", id: workpackageInstance.id)
}

def delete(Long id) {
    def workpackageInstance = Workpackage.get(id)
    if (!workpackageInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'workpackage.label', default: 'Workpackage'), id])
        redirect(action: "list")
        return
    }

    try {
        workpackageInstance.delete(flush: true)
        //flash.message = message(code: 'default.deleted.message', args: [message(code: ' workpackage.label', default: 'Workpackage'), id])
        flash.message = message(code: 'default.deleted.message', args: [message(code: 'project.label', default: 'Workpackage'), id])
        redirect(action: "list")
    }
    catch (DataIntegrityViolationException e) {
        flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'workpackage.label', default: 'Workpackage'), id])
        redirect(action: "show", id: id)
    }
}
}

0 个答案:

没有答案