2017-07-05 48 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