【问题标题】:How to keep query results after deleting an item?删除项目后如何保留查询结果?
【发布时间】:2014-06-19 16:01:05
【问题描述】:

在我的 Grails 应用程序中,我有多个页面,其中列出了表内的一组数据对象。在这些页面中,我提供了一个搜索功能,执行该功能时将调整表格以仅显示与查询匹配的数据对象。但是,如果用户决定删除其中一个数据对象,应用程序会将它们带回显示所有内容的默认表。我希望查询结果在执行删除后保持不变。

我将在这篇文章中使用我的“技能评估”页面作为示例。

这是领域类中的相关代码

SkillEval.groovy

class SkillEval {

    static hasMany = [lines: SkillEvalL, courses: CourseOffering, choiceLabels: ChoiceLabel]

    String name
    String formVersion


static def search(params) {
    def criteria = SkillEval.createCriteria()

    def results = criteria.list(params) {
        or {
            ilike("name", params.search+'%')
        }
    }

    return results
}
}

gsp视图文件的相关部分

list.gsp

    <g:form> 
        <div class="search">
            <label for="searchField">Search:</label> <input type="text"
                id="searchField" name="search" value="${params.search}" /> <input
                type="submit" value="Search" />
        </div>
        <br>

        <table id="mainTable">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Version</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <g:each var="eval" in="${skillEvalList}">
                    <tr>
                        <td>
                            <strong>
                                ${eval.name}
                            </strong>
                        </td>
                        <td>
                            ${eval.formVersion}
                        </td>
                        <td>        
                            <g:actionSubmit value="Delete" controller="skillEval" action="delete" onclick="setId(${eval.id});return confirm('Are you sure?');" class="delete" />
                        </td>
                    </tr>
                </g:each>
            </tbody>
        </table>
        <g:if test="${(skillEvalCount/maxCount) > 1}">
            <div class="pagination">
                <g:paginate action="list" total="${skillEvalCount}" />
            </div>
        </g:if>

        <input id="evalId" type="hidden" name="id" value="" />
    </g:form>
</div>
<r:script>
    function setId(id)
    {
        $('#evalId').val(id);
    }
</r:script>

Controller类中的相关代码

SkillEvalController.groovy

def delete(Long id) {

    def skillEval = SkillEval.get(id)

    if (skillEval) {
        def allInstances = SkillEvalI.findAllByForm(skillEval)

        allInstances.each { evalInstance ->
            evalInstance.lines.clear()
            if(!evalInstance.delete()) {
                println "Failed to delete skill eval instance"
            }
            else {
                println "Instance successfully deleted."
            }   
        }

        try {
            skillEval.delete(flush: true)

        }
        catch (DataIntegrityViolationException e) {

        }
    } 

    redirect(action: "list")
}


删除其中一项查询后,如何让视图保留查询结果?

【问题讨论】:

    标签: grails groovy gsp


    【解决方案1】:

    当您将redirect 传递给list 操作时,您可以提供参数——只需在delete 操作中捕获参数(如果有),然后在重定向调用末尾将它们传递给list控制器动作。

    (更新:忘记了 actionSubmit 不接受参数作为属性,所以从this SO answerthe Grails docs 拼凑了这个解决方案)

    例子:

    // View (list.gsp)
    <g:actionSubmit action="deleteWithParams" value="Delete" ... />
    
    
    // Controller
    def list() {
        def deleteWithParams = { forward(action:'delete', params:[search: params?.search]) }
        render...
    }
    
    def delete(Long id) {
        // Deleting the skillEval ...
        redirect(action: "list", params: [search: params?.search])
    }
    

    【讨论】:

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