zoukankan      html  css  js  c++  java
  • 一步一步学grails:8 物料表

    资源下载

    1、  首先,我们需要一个让用户录入“材料费”的地方。在list页面增加一个按钮“材料费”:

    <span class="menuButton"><g:link class="doc" action="index" controller="materialCost">材料费</g:link></span>

    然后用户可以通过这个链接录入材料费了:

    2、  在“生产计划”列表页面,我们增加一个“物料表”链接,使用户能编辑物料表:

    <g:if test="${productionNoInstance.findPlan()}">

    ……

    <g:link controller="planning" action="materialList" id="${productionNoInstance.findPlan().id}">物料表</g:link>

    ……

     </g:if>

    3、  打开PlanningController.groovy,新增一个action,叫做materialList

    def materialList = {//显示物料表

    def planningInstance = Planning.get( params.id )//获得生产计划实例

    if(!planningInstance) {//若获得的实例为空,显示错误,返回list页面

          flash.message = "Planning not found with id ${params.id}"

          redirect(action:list)

    }

    else { return [ planningInstance : planningInstance ] }//返回实例

    }

    4、  grails-app/views/planning 下新建一个页面materialList.gsp:

    ……

        <body>

    ……

    <h1>物料表</h1>

    <span style="color:#ff0066">[产品编号]</span><g:link action="show" id="${planningInstance.id}">${planningInstance.productNo}</g:link>

    &nbsp;&nbsp;&nbsp;&nbsp;

    <span style="color:#ff0066">[计划编号]</span>${planningInstance.id}<br/><br/>

    ……

    <thead>

    <tr>

                               <th width="60">Id</th>

                               <th width="150">物料代码</th>

                                  <th width="200">物料类型</th> 

                                  <th width="80">单价</th>

                                  <th width="80">数量</th>

                                  <th/>

                            </tr>

    ……

    <tbody>

    <g:each in="${planningInstance.materials}" status="i" var="m">

                            <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">

                            <td>${m?.id}</td>

                            <td>${m?.fee?.code}</td>

                            <td>${m?.fee?.name}</td>

                            <td>${m?.fee?.price}</td>

                            <td>${m?.amount}</td>

                            <td>

    <g:form  controller="material"  action="delete">

    <input type="hidden" name="planningId" value="${planningInstance?.id}" />

    <input type="hidden" name="id" value="${m?.id}"/>

    <span class="button"><g:submitButton name="delete" class="edit" value="删除"/></span>

    </g:form>

    </td>

    </tr>

    </g:each>

    </tbody>

    </table>

    </div>

    <div class="buttons" >

                    <g:form  controller="material"  action="create">

                        <input type="hidden" name="planningId" value="${planningInstance?.id}" />

                        <span class="button"><g:submitButton name="submit" class="edit" value="添加"/></span>

    </g:form>

    </div>

    ……

    注意其中的两个button,一个是删除按钮,一个是添加按钮。分别对应的是material控制器中的两个action

    def delete = {

    def materialInstance = Material.get( params.id )

    def planning=Planning.get(params.planningId)

    if(materialInstance) {

    try {

                    materialInstance.delete()

                    if(planning){

                      planning.delMaterial(materialInstance)

                      planning.save()

                    }

                    flash.message = "/u6210/u529F/u5C06/u7269/u6599/u4ECE/u7269/u6599/u8868/u4E2D/u5220/u9664/uFF01"

                    redirect(controller:'planning',action:'materialList',id:params.planningId)

    }         catch(org.springframework.dao.DataIntegrityViolationException e) {

        flash.message = "Material ${params.id} could not be deleted"

        redirect(action:show,id:params.id)

        }

    }

    else {

    flash.message = "Material not found with id ${params.id}"

    redirect(action:list)

            }

    }

    def create = {

            def materialInstance = new Material()

            def planning=Planning.get(params.planningId)

            materialInstance.properties = params

            return ['materialInstance':materialInstance,'planningInstance':planning]

    }

    同时还需要修改save闭包,在保存时调用planning 实例的addMaterial方法把新建的Material实例添加到物料表中:

    def save = {

           def planning=Planning.get(params.planningId)

            def materialInstance = new Material(params)

            if(!materialInstance.hasErrors() && materialInstance.save()) {

            planning.addMaterial(materialInstance)

             planning.save()

                flash.message = "/u6210/u529F/u5C06/u7269/u6599/u52A0/u5165/u7269/u6599/u8868/uFF01"

                redirect(action:show,id:materialInstance.id,params:[planningId:planning.id])

            }

            else {

                render(view:'create',model:[materialInstance:materialInstance])

            }

        }

    这样当我们点击前面的“物料表”链接时呈现如下页面:

    我们通过“添加”按钮给生产计划中的物料表添加物料或通过“删除”按钮把物料从物料表中删除。当我们点击上面的兰色的产品编号链接时,显示生产计划细节:

    注意,下端“材料表”栏,显示出了物料表中包含的物料,但以物料id的显示方式显然不能让人满意,我们来稍微改变一下。

     

    5、  当然,我们还需要给Planning 类增加相应的addMaterialdelMaterial方法:

    void addMaterial(Material m){

        if(materials==null)

            materials=[]

        materials.add(m)

    }

    void delMaterial(Material m){

        if(materials!=null)

            materials.remove(m)

    }

    6、  打开Material.groovy,增加toString方法:

    String toString(){

           "${fee?.code} ${fee?.name} ${amount} ${fee?.unit}"

        }

    现在,材料表的显示则直观多了:

    7、  修改页面grails-app/planning/show.gsp,在其中增加编辑物料表的链接:

    <g:link action="materialList" id="${planningInstance?.id}">物料表</g:link>:

                          

    这样,通过链接,可以编辑物料表中内容。

    8、   

  • 相关阅读:
    synchronized优化手段:锁膨胀、锁消除、锁粗化和自适应自旋锁...
    synchronized 优化手段之锁膨胀机制!
    synchronized 加锁 this 和 class 的区别!
    SpringBoot中时间格式化的5种方法!
    阿里云ddns shell 脚本
    adb 备份apk
    paddlex_gui_win10(飞浆)
    cuda 版本对照
    PaddleHub
    yum 查找库对应的包
  • 原文地址:https://www.cnblogs.com/encounter/p/2188550.html
Copyright © 2011-2022 走看看