【问题标题】:Dropdown list in grailsgrails中的下拉列表
【发布时间】:2013-06-05 04:24:55
【问题描述】:

在我的项目跟踪器程序中,我有一个表,其中的行对应于项目,列对应于项目的不同信息(名称、截止日期、状态等)。最后一列应该是“更多”列,当您按下它时,它应该显示项目附加属性的下拉列表。我如何在 Grails 中做到这一点? 下面是我的 list.gsp:

<calendar:resources lang="en"/>

<!doctype html>
<html>
    <head>
        <meta name="layout" content="layoutMain"/>
        <g:set var="entityName" value="${message(code: 'project.label', default: 'Project')}" />
        <title><g:message code="default.list.label" args="[entityName]" /></title>
    </head>
    <body>
        <div class="nav" role="navigation">
            <ul>
                <li><g:link class="create" action="create"><button>New Project</button></g:link></li>
            </ul>
        </div>
        <div id="list-project" class="content scaffold-list" role="main">
            <h1><g:message code="default.list.label" args="[entityName]" /></h1>
            <g:if test="${flash.message}">
            <div class="message" role="status">${flash.message}</div>
            </g:if>
            <table>
                <thead>
                    <tr>

                        <g:sortableColumn property="name" title="${message(code: 'project.name.label', default: 'Name')}" />

                        <g:sortableColumn property="dueDate" title="${message(code: 'project.dueDate.label', default: 'Due Date')}" />

                        <g:sortableColumn property="startDate" title="${message(code: 'project.startDate.label', default: 'Start Date')}" />
                        <g:sortableColumn property="status" title="${message(code: 'project.name.label', default: 'Status')}" />

                        <g:sortableColumn property="requirements" title="${message(code: 'project.name.label', default: 'Requirements')}" />

                        <g:sortableColumn property="design" title="${message(code: 'project.name.label', default: 'Design')}" />

                        <g:sortableColumn property="development" title="${message(code: 'project.name.label', default: 'Development')}" />

                        <g:sortableColumn property="qa" title="${message(code: 'project.name.label', default: 'QA')}" />

                        <g:sortableColumn property="ua" title="${message(code: 'project.name.label', default: 'UA')}" />

                        <g:sortableColumn property="delivery" title="${message(code: 'project.name.label', default: 'Delivery')}" />

                        <g:sortableColumn property="more" title="${message(code: 'project.name.label', default: 'More')}" />   

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

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

                        <td><g:formatDate date="${projectInstance.dueDate}" /></td>

                        <td><g:formatDate date="${projectInstance.startDate}" /></td>

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

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

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

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

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

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

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

                        <td><g:link action="show" id="${projectInstance.id}">${fieldValue(bean: projectInstance, field: "delivery.name")}</g:link></td>
                    </tr>
                </g:each>
                </tbody>
            </table>
            <div class="pagination">
                <g:paginate total="${projectInstanceTotal}" />
            </div>
        </div>
    </body>
</html>

【问题讨论】:

    标签: grails drop-down-menu html-table gsp


    【解决方案1】:

    如果你想使用标准的 Grails,你可以使用 g:select 标签在 Grails 中创建一个下拉菜单。一个更漂亮的解决方案可能是使用 jQuery(或类似的)来显示隐藏 HTML 块。

    如果您的项目域类似于:

    class Project {
    
        String name
    ...
        // more info
        String  attr1
        int     attr2
        boolean isAttr3
    
    }
    

    您可以向您的域添加一种便捷方法,以将字段聚合到一个列表(或可能是另一个对象)中,该列表可用于下拉框。例如

    // utility getter to aggregate the fields into an array 
    def getMoreInfo() {
        [attr1, attr2, isAttr3]
    }
    

    那么您可以在 GSP 中使用以下内容:

    <td><g:select name="more" from="${projectInstance.moreInfo}" /></td>
    

    【讨论】:

      【解决方案2】:

      点击更多列,您可以向某个操作发送 Ajax 请求,该操作可以为您带来两个数组的 JSON:

      {extraColumns:{c1,c2}, columnValues:{{v11,v12}, {v21,v22}}}
      

      在 Ajax 的响应中,您可以解析 JSON 并为标题创建附加列并为行创建附加列值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-17
        相关资源
        最近更新 更多