2017-07-05 44 views
-1

格式的電話號碼我一直在試圖格式的電話號碼在一個Grails應用程序通過使用下面的taglib在Groovy/Grails的

package rewards 

class MasksTagLib { 
    static defaultEncodeAs = [taglib:'html'] 
    //static encodeAsForTags = [tagName: [taglib:'html'], otherTagName:    
    [taglib:'none']]  
    def phone334 = { attrs -> 
    String phone = attrs.phone 
    def formatted = "("+phone.substring(0,3)+") "+pho ne.substring(3,6)+"-"+phone.substring(6) 
    out << formatted 
    } 
} 

此外,我已經使用了import語句使用taglib在我的意見,但是格式不改變顯示在index.gsp中的CustomerController 這適用於以下幾種觀點,但我不能讓它工作在別處的觀點profile.gsp

<%@ page import="rewards.Customer" %> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta name="layout" content="main"> 
    <g:set var="entityName" value="${message(code: 'customer.label',   default: 'Customer')}" /> 
    <title>Customer Profile</title> 
</head> 
<body> 
    <div id="edit-customer" class="content scaffold-edit" role="main"> 
     <h1>Customer Profile</h1> 
     <g:if test="${flash.message}"> 
     <div class="message" role="status">${flash.message}</div> 
     </g:if> 
     <g:hasErrors bean="${customerInstance}"> 
     <ul class="errors" role="alert"> 
      <g:eachError bean="${customerInstance}" var="error"> 
      <li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li> 
      </g:eachError> 
     </ul> 
     </g:hasErrors> 
     <g:form url="[resource:customerInstance, action:'updateProfile']" method="PUT" > 
      <g:hiddenField name="version" value="${customerInstance?.version}" /> 
      <fieldset class="buttons"> 
       <g:actionSubmit class="save" action="updateProfile" value="${message(code: 'default.button.update.label', default: 'Update')}" /> 
      </fieldset> 
      <fieldset class="form"> 
       <div class="fieldcontain ${hasErrors(bean: customerInstance, field: 'firstName', 'error')} "> 
        <label for="firstName"> 
         <g:message code="customer.firstName.label" default="First Name" /> 

        </label> 
        <g:textField name="firstName" value="${customerInstance?.firstName}"/> 
       </div> 

       <div class="fieldcontain ${hasErrors(bean: customerInstance, field: 'lastName', 'error')} "> 
        <label for="lastName"> 
         <g:message code="customer.lastName.label" default="Last Name" /> 

        </label> 
        <g:textField name="lastName" value="${customerInstance?.lastName}"/> 
       </div> 

       <div class="fieldcontain ${hasErrors(bean: customerInstance, field: 'phone', 'error')} required"> 
        <span id="phone-label" class="property-label"><g:message code="customer.phone.label" default="Phone" /></span> 
        <span class="property-value" aria-labelledby="phone-label"><g:phone334 phone="${customerInstance?.phone}"/></span> 
       </div> 

       <div class="fieldcontain ${hasErrors(bean: customerInstance, field: 'email', 'error')} "> 
        <label for="email"> 
         <g:message code="customer.email.label" default="Email" /> 

        </label> 
        <g:textField name="email" value="${customerInstance?.email}"/> 
       </div> 

       <div class="fieldcontain ${hasErrors(bean: customerInstance, field: 'totalPoints', 'error')} required"> 
        <span id="totalPoints-label" class="property-label"><g:message code="customer.totalPoints.label" default="Total Points" /></span> 
        <span class="property-value" aria-labelledby="totalPoints-label"><g:fieldValue bean="${customerInstance}" field="totalPoints"/></span> 

       </div> 
      </fieldset> 

     </g:form> 
    </div> 

    <div id="list-award" class="content scaffold-list" role="main"> 
     <g:if test="${flash.message}"> 
      <div class="message" role="status">${flash.message}</div> 
     </g:if> 
     <table> 
     <thead> 
       <tr> 

        <g:sortableColumn property="type" title="${message(code: 'award.type.label', default: 'Type')}" /> 

        <g:sortableColumn property="awardDate" title="${message(code: 'award.checkinDate.label', default: 'Award Date')}" /> 

        <th><g:message code="award.customer.label" default="Phone" /></th> 

        <g:sortableColumn property="points" title="${message(code: 'award.points.label', default: 'Points')}" /> 

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

        <td>${fieldValue(bean: checkinInstance, field: "type")}</td> 

        <td>${fieldValue(bean: checkinInstance, field: "awardDate")}</td> 

        <td><g:phone334 phone="${customerInstance?.phone}"/></td> 

        <td>${fieldValue(bean: checkinInstance, field: "points")}</td> 
       </tr> 
      </g:each> 
      </tbody> 
     </table> 

    </div> 
</body> 

我將不勝感激任何幫助。

這裏是控制器。它被稱爲CustomerController.groovy,它使用「索引」顯示列表。如何申請手機格式到此列表

package rewards 

class CustomerController { 
static scaffold = true 

def calculationsService 

def lookup() { 
    // def customerInstance = Customer.list(sort: "lastName", order:  
    "desc", max: 5, offset: 5) 
    // dynamic queries 
    // def customerInstance = Customer.findAllByLastName("Foster") 
    // def customerInstance = Customer.findAllByTotalPoints(5, [sort: "lastName", order: "desc"]) 
    // def customerInstance = Customer.findAllByPhone(params.id) // for one row return findBy if rows > 1 only first 
    // def customerInstance = Customer.findAllByLastNameLike("H%") // Case Sensitive 
    // def customerInstance = Customer.findAllByLastNameIlike("b%") // Case Insensitive 
    // def customerInstance = Customer.findAllByTotalPointsGreaterThanEquals(3, [sort: "totalPoints"]) 
    // def customerInstance = Customer.findAllByTotalPointsBetween(2, 4, [sort: "totalPoints"]) 
    def customerInstance = Customer.findAllByFirstNameIlikeAndTotalPointsGreaterThanEquals("b%", 3) 
    [customerInstanceList: customerInstance] 
} 

def customerLookup(Customer lookupInstance) { 
    // Query customer by Phone number - service 
    // If no result, - controller 
    // create a new customer - service 
    // create welcome message - service 
    // add award reward - service 
    // save customer - service 
    // send welcome to kiosk - controller 
    // if customer found - controller 
    // calculate total ponts - service 
    // create welcome message - service 
    // add award reward - service 
    // save customer - controller 
    // send welcome to kiosk - controller 
    def (customerInstance, welcomeMessage) = calculationsService.processCheckin(lookupInstance) 
    render(view: "checkin", model:[customerInstance: customerInstance, welcomeMessage: welcomeMessage]) 
} 
def index() { 
    params.max = 10 
    [customerInstanceList: Customer.list(params), customerInstanceCount: Customer.count()] 
} 

def create() { 
    [customerInstance: new Customer()] 
} 

def save(Customer customerInstance) { 
    customerInstance.save() 
    redirect(action: "show", id: customerInstance.id) 
} 

def show(Long id) { 
    def customerInstance = Customer.get(id) 
    customerInstance = calculationsService.getTotalPoints(customerInstance) 
    [customerInstance: customerInstance] 
} 

def edit(Long id) { 
    def customerInstance = Customer.get(id) 
    [customerInstance: customerInstance] 
} 

def update(Long id) { 
    def customerInstance = Customer.get(id) 
    customerInstance.properties = params 
    customerInstance.save() 
    redirect(action: "show", id: customerInstance.id) 
} 

def delete(Long id) { 
    def customerInstance = Customer.get(id) 
    customerInstance.delete() 
    redirect(action: "index") 
} 

def profile() { 
    def customerInstance = Customer.findByPhone(params.id) 
    [customerInstance: customerInstance] 
} 

def updateProfile(Customer customerInstance) { 
    customerInstance.save() 
    render(view: "profile", model:[customerInstance: customerInstance]) 
} 

def checkin() {} 

} 

Customer List Display

+0

我們可以看到它不起作用的gsp嗎?我想這是上面taglib中的一個拼寫錯誤,在pho ne這個詞中有一個空格? –

+0

「此外,我使用import語句在我的視圖中使用taglib」 - 您不需要import語句就可以在視圖中使用taglib。 –

+0

我可以告訴你控制器。它使用索引來顯示客戶列表。如何將該格式應用於顯示器 –

回答

0

如果您想自定義索引視圖,在這種情況下,最好的辦法是停止使用腳手架頁,是的。 (還有其他選擇,但是對於這種情況它們過於複雜,並且不能爲您提供對結果視圖的近乎控制。)

總的來說,長期來看,腳手架的內容。通過這個,我的意思是腳手架控制器,或腳手架的意見,但不是另一邊。長期來看,其中一個或另一個可能會發生變化,您的網頁將會崩潰。

+0

當我看着你的代碼時,還有一些其他的註釋:看看作爲你現在如何渲染錯誤的替代方法,因爲你可能會發現它更清晰。您可以刪除taglib導入。您的控制器行爲中的一些行爲似乎仍然是標準的腳手架行爲,因此如果您也將視圖單獨留下,那麼您可能只需將它們取出並讓腳手架完成工作,但我知道的細節通常會得到取出張貼在這裏,以便可能不是「真實的」。祝你一切順利! – Daniel