格式化Groovy / Grails中的电话号码

时间:2017-07-05 03:12:10

标签: grails

我一直在尝试使用以下taglib格式化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,但格式不会改变customerController的index.gsp中的显示 这适用于以下视图,但我无法在其他地方工作,视图是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

1 个答案:

答案 0 :(得分:0)

如果你想自定义你的索引视图,在这种情况下你最好的选择是停止使用scaffolded页面,是的。 (有其他选择,但它们在这种情况下过于复杂,并且几乎没有为你提供对结果视图的控制。)

一般来说,长期拥有半支架内容并不是一个好主意。通过这个,我的意思是脚手架控制器,或脚手架视图,但不是另一边。从长远来看,其中一种可能会发生变化,您的网页也会中断。