簡體   English   中英

如何在2個以上域中使用Grails可搜索插件

[英]How to use Grails Searchable plugins in more than 2 domains

我是Grails的新手,從過去的幾天開始學習grails。 我試圖通過在我的演示grails應用程序中使用可搜索插件來添加搜索功能。 我在用戶搜索中成功添加了可搜索的插件 ,用戶可以在其中搜索其他用戶並關注他們。 我是這樣

grails install-plugin searchable

域Person.groovy-

package org.grails.twitter

  class Person {

transient springSecurityService

String realName
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static hasMany = [followed:Person, status:Status]
static searchable = [only: 'realName']

static constraints = {
    username blank: false, unique: true
    password blank: false
}

static mapping = {
    password column: '`password`'
}

Set<Authority> getAuthorities() {
    PersonAuthority.findAllByPerson(this).collect { it.authority } as Set
}

def beforeInsert() {
    encodePassword()
}

def beforeUpdate() {
    if (isDirty('password')) {
        encodePassword()
    }
}

protected void encodePassword() {
    password = springSecurityService.encodePassword(password)
}
   }

查看/可搜索/ index.gsp-

<html>
 <head>
  <meta name="layout" content="main" />
  <title>What Are You Doing?</title>
  <g:javascript library="jquery" plugin="jquery" />
  </head>
  <body>
<h1>Search For People To Follow</h1>
<div class="searchForm">
    <g:form controller="searchable">
        <g:textField name="q" value=""/>
    </g:form>
</div>

    <h1>What Are You Doing?</h1>
    <div class="updateStatusForm">
    <g:formRemote onSuccess="document.getElementById('messageArea').value='';" url="[action: 'updateStatus']" update="messageLists" name="updateStatusForm">
        <g:textArea name="message" value="" id="messageArea" /><br/>
        <g:submitButton name="Update Status" />
    </g:formRemote>
     </div>
    <div id="messageLists">
    <g:render template="messages" collection="${messages}" var="message"/>
</div>
   </body>
   </html>

工作正常。 現在我的問題開始了。 現在,我想在我的帖子域中添加此可搜索項,用戶也可以在其中搜索帖子項。 我正在這樣...

域Post.groovy-

package groovypublish

class Post {

static hasMany = [comments:Comment]

String title
String teaser
String content
Date lastUpdated
Boolean published = false
SortedSet comments

static searchable = [only: 'title']

static constraints = {
    title(nullable:false, blank:false, length:1..50)
    teaser(length:0..100)
    content(nullable:false, blank:false)
    lastUpdated(nullable:true)
    published(nullable:false)
}
}

這是表單視圖

查看/發布/list.gsp-

  ------ some code -----
 <g:form controller="searchable" class="navbar-search pull-left">
   <g:textField name="q" value="" class="search-query" placeholder="Search Posts"/>
 </g:form>

 ------ some code ------

現在,當我嘗試按帖子標題搜索帖子時,它顯示錯誤。 它會覆蓋可搜索的操作。 如何解決這個問題呢 ?

您可以使用searchable實現自己的搜索方法,從搜索表單中調用控制器函數,然后執行搜索:

假設您有兩種搜索形式:

<g:form controller="postsController" action="postAction" class="navbar-search pull-left">
   <g:textField name="q" value="" class="search-query" placeholder="Search Posts"/>
 </g:form>

 <g:form controller="searchable">
        <g:textField name="q" value=""/>
    </g:form>

然后在PostCOntroller中,您可以使用postAction方法來執行搜索:

def postAction (Integer max) {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        params.sort = "id"
        params.order = "desc"

        if(params?.q){


            def result = Post .search(params.q , order:"desc" )

            return [searchResults: result.results, searchResultsCount: result.total, popup : params.popup?.toBoolean()]
        }else{
            [searchResults: Post .list(params), searchResultsCount: Post .count(), popup : params.popup?.toBoolean()]
        }

同樣,您可以為其他搜索使用不同的功能,如果您使用遠程表單,則需要在搜索頁面上具有兩個不同的div,然后可以在其中呈現結果頁面。

假設您有:

<g:formRemote name="postSearchForm" update="postSearchResultsDiv" url="[controller: 'post', action:'postAction' , params: [popup: false]]">
                        <label for="searchText">Search Post:</label>
                        <input name="q" type="text" id="searchText" class="input-medium search-query"/>
                        <input id="searchButton" type="submit" class="btn-info" value="Search"/>
                    </g:formRemote>

<div id="postSearchResultsDiv">--Your search result for the form will display here--</div>

該遠程表單將在控制器中調用postAction方法,您可以在控制器的view文件夾上具有postAction.gsp頁面,並在那里打印結果。

並且在您的搜索頁面上, postSearchResultsDiv將具有搜索結果(postAction GSP頁面輸出)

我解決了自己的問題...我已經這樣做了..

PostController ---

import org.compass.core.engine.SearchEngineQueryParseException
class PostController 
{
   def searchableService

   def searchpost = {
    if (!params.q?.trim()) {
        return [:]
    }
    try {
        return [searchResult: searchableService.search(params.q, params)]
    } catch (SearchEngineQueryParseException ex) {
        return [parseException: true]
    }
    render(view:'searchpost')
   }

  .......
}   

搜索表格-

<g:form controller="post" action="searchpost" class="navbar-search pull-left">
   <g:textField name="q" value="" class="search-query" placeholder="Search Posts"/>
 </g:form>

searchpost.gsp //用於顯示結果

<html>
   <head>
    <r:require modules="bootstrap"/>
    <meta name="layout" content="main"/>        
   </head>
   <body>
     <g:render template="/layouts/header" />    
     <div class="well"> 
        <g:each var="post" in="${searchResult?.results}">
          <div>
           <h2>${post.title}</h2>
           <p>${post.teaser}</p>
           <p>Last Updated: ${post.lastUpdated}</p>
           <g:link controller="post" action="view" id="${post.id}" class="btn btn-success">
         View this post
          </g:link>
          <g:if test="${post.author == currentLoggedInUser }">
         <g:link controller="post" action="edit" id="${post.id}" class="btn btn-danger">
             Edit this post
         </g:link>
         <g:actionSubmit action="delete" value="${message(code: 'default.button.delete.label', default: 'Delete')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');" class="btn btn-inverse" />
        </g:if>
        <g:form>
     <g:hiddenField name="id" value="${post?.id}" />            
        </g:form>
   </div>

   </g:each>
  </div>        
 </body>
 </html>

它有效:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM