2008-10-20 15 views
6

मैं अपनी परियोजनाओं में टैग का उपयोग कर रहा हूं। मैं अपनी लाइब्रेरी के लिए कुछ नए टैग खोजने के लिए grails.org पर कस्टम टैग ब्राउज़ कर रहा था।पसंदीदा कस्टम Grails टैग है?

http://www.grails.org/Contribute+a+Tag

मैं सोच रहा था अगर StackOverflow समुदाय के लोगों पसंदीदा कस्टम टैग है कि वे साझा करने के लिए करना चाहते हैं।

उत्तर

1

मैं DecimalFormat वर्ग खोजने के (और Grails के formatNumber टैग विस्तार से) कुछ उपयोग के मामलों के लिए अपारदर्शी एक सा है, और मैं अभी भी कुछ बहुत करने के लिए एक उचित तरीका नहीं मिला है उचित स्वरूप स्ट्रिंग उत्पन्न करने के लिए कुछ बदसूरत प्री-प्रोसेसिंग के बिना इसके साथ मूल स्वरूपण। मैंने कई महीनों पहले एक साधारण संख्या स्वरूपण टैग को एक साथ फेंक दिया जो अनिवार्य रूप से एक प्रारूप स्ट्रिंग बनाता है और कुछ न्यूनतम प्रक्रिया को स्वयं ही करता है।

यह सामान्य या सुरुचिपूर्ण नहीं है जैसा कि मैं चाहता हूं (यह उस समय की आवश्यकता है - यह बहुत ही बुनियादी है, लेकिन यह अभी भी जीएसपी से कुछ बदसूरत प्रसंस्करण रखता है), लेकिन इसे पढ़ना आसान होना चाहिए, और यह स्पष्ट जहां इसे छोटा रूप से सुधार किया जा सकता है (यानी बेवकूफ अगर बदले के बदले स्केलिंग पुनरावर्तक बनाते हैं, तो उपयोगकर्ता को कस्टम स्केलिंग मार्करों में पास करने की इजाजत मिलती है, जो एक पैरामीटर के रूप में कस्टम नंबर सत्यापनकर्ता की इजाजत देता है)।


// Formats a number to 3 significant digits, appending appropriate scale marker 
// (k, m, b, t, etc.). Defining var allows you to use a string representation 
// of the formatted number anywhere you need it within the tag body, and 
// provides the scale as well (in case highlighting or other special formatting 
// based upon scale is desired). 
def formatNumberScaled = {attrs, body -> // number, prefix, suffix, invalid, var 
    Double number 
    String numberString 
    String scale 

    try { 
     number = attrs.'number'.toDouble() 
    } catch (Exception e) { 
     number = Double.NaN 
    } 

    if (number.isNaN() || number.isInfinite()) { 
     numberString = scale = attrs.'invalid' ?: "N/A" 
    } else { 
     Boolean negative = number < 0d 
     number = negative ? -number : number 

     if (number < 1000d) { 
      scale = '' 
     } else if (number < 1000000d) { 
      scale = 'k' 
      number /= 1000d 
     } else if (number < 1000000000d) { 
      scale = 'm' 
      number /= 1000000d 
     } else if (number < 1000000000000d) { 
      scale = 'b' 
      number /= 1000000000d 
     } else if (number < 1000000000000000d) { 
      scale = 't' 
      number /= 1000000000000d 
     } 

     String format 
     if (number < 10d) { 
      format = '#.00' 
     } else if (number < 100d) { 
      format = '##.0' 
     } else { 
      format = '###' 
     } 
     format = "'${attrs.'prefix' ?: ''}'${format}'${scale} ${attrs.'suffix' ?: ''}'" 

     numberString = g.formatNumber('number': negative ? -number : number, 'format': format) 
    } 

    // Now, either print the number or output the tag body with 
    // the appropriate variables set 
    if (attrs.'var') { 
     out << body((attrs.'var'): numberString, 'scale': scale) 
    } else { 
     out << numberString 
    } 
} 
+0

@ankimal - दूसरों पर टिप्पणी करने के लिए पर्याप्त प्रतिनिधि नहीं है - आप की कोशिश की है:
http://www.grails.org/Contribute+a+Tag#remotePaginate

जैसी है कुछ प्रकार की कमी, या आपका कस्टम टैग सिर्फ कुछ कार्य के लिए उपयुक्त है? –

1

मेरे पास एक "fmt: relDate" टैग है जो आपको "3 दिन पहले", "30 सेकंड पहले से कम", आदि को टूलटिप के रूप में वास्तविक समय के साथ ट्विटर जैसी सापेक्ष तिथियां देता है।

वर्तमान कार्यान्वयन मूल रूप से सीमाओं के साथ बयान के बाद/तो कथन की एक विशाल श्रृंखला है। एक बाइनरी-सर्च आधारित एल्गोरिदम बेहतर होगा ("अधिक कुशल" के अर्थ में), और वर्तमान कार्यान्वयन में मेरी निजी प्राथमिकताओं को एन्कोड किया गया है, इसलिए मैं टैग साझा करने में अनिच्छुक हूं।

1

मेरे पास एक रिमोट पेजिनेट टैब है, जो मुझे AJAX के माध्यम से परिणाम को कम करने में मदद करता है। यह डिफ़ॉल्ट टैब पर एक सुधार है और कस्टम तर्क लेता है।

कोड यह रहा:

class CustomRemotePaginateTagLib { 

    static namespace = 'myTagLib' 

    /** * Creates next/previous links to support pagination for the current controller * * <g:paginate total="$ { Account.count() } " />    */ 
    def remotePaginate = {attrs -> 
    def writer = out 
    if (attrs.total == null) throwTagError("Tag [remotePaginate] is missing required attribute [total]") 

    if (attrs.update == null) throwTagError("Tag [remotePaginate] is missing required attribute [update]") 

    def locale = RequestContextUtils.getLocale(request) 

    def total = attrs.total.toInteger() 

    def update = attrs.update 

    def action = (attrs.action ? attrs.action : (params.action ? params.action : "list")) 
    def controller = (attrs.controller ? attrs.controller : params.controller) 
    def offset = params.offset?.toInteger() 
    def max = params.max?.toInteger() 
    def maxsteps = (attrs.maxsteps ? attrs.maxsteps.toInteger() : 10) 

    if (!offset) offset = (attrs.offset ? attrs.offset.toInteger() : 0) 
    if (!max) max = (attrs.max ? attrs.max.toInteger() : 10) 

    def linkParams = [offset: offset - max, max: max] 
    if (params.sort) linkParams.sort = params.sort 
    if (params.order) linkParams.order = params.order 
    if (attrs.params) linkParams.putAll(attrs.params) 
    linkParams['action'] = action 
    linkParams['controller'] = controller 

    def linkTagAttrs = [url: "#"] 
    if (attrs.controller) { linkTagAttrs.controller = attrs.controller } 
    if (attrs.id != null) { linkTagAttrs.id = attrs.id } 

    // determine paging variables 
    def steps = maxsteps > 0 
    int currentstep = (offset/max) + 1 
    int firststep = 1 
    int laststep = Math.round(Math.ceil(total/max)) 

    // display previous link when not on firststep 
    if (currentstep > firststep) { 
     linkTagAttrs.class = 'prevLink' 
     def prevOffset = linkParams.offset 

     def params = attrs.params ?: [] 
     params.'offset' = prevOffset 

     linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params) 
     writer << link(linkTagAttrs.clone()) { 
     (attrs.prev ? attrs.prev : g.message(code: 'default.paginate.prev', default: 'Previous')) 
     } 
    } 

    // display steps when steps are enabled and laststep is not firststep 
    if (steps && laststep > firststep) { 
     linkTagAttrs.class = 'step' 

     // determine begin and endstep paging variables 
     int beginstep = currentstep - Math.round(maxsteps/2) + (maxsteps % 2) 
     int endstep = currentstep + Math.round(maxsteps/2) - 1 

     if (beginstep < firststep) { 
     beginstep = firststep 
     endstep = maxsteps 
     } 
     if (endstep > laststep) { 
     beginstep = laststep - maxsteps + 1 
     if (beginstep < firststep) { 
      beginstep = firststep 
     } 
     endstep = laststep 
     } 

     // display firststep link when beginstep is not firststep 
     if (beginstep > firststep) { 
     linkParams.offset = 0 

     def params = attrs.params ?: [] 
     params['offset'] = linkParams.offset 

     linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params) 
     writer << link(linkTagAttrs.clone()) { firststep.toString() } 
     writer << '<span class="step">..</span>' 
     } 

     // display paginate steps 
     (beginstep..endstep).each {i -> 
     if (currentstep == i) { 
      writer << "<span class=\"currentStep\">${i}</span>" 
     } else { 
      linkParams.offset = (i - 1) * max 

      def params = attrs.params ?: [] 
      params['offset'] = linkParams.offset 

      linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params) 
      writer << link(linkTagAttrs.clone()) { i.toString() } 
     } 
     } 

     // display laststep link when endstep is not laststep 
     if (endstep < laststep) { 
     writer << '<span class="step">..</span>' 
     linkParams.offset = (laststep - 1) * max 

     def params = attrs.params ?: [] 
     params['offset'] = linkParams.offset 

     linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params) 
     writer << link(linkTagAttrs.clone()) { laststep.toString() } 
     } 
    } 

    // display next link when not on laststep 
    if (currentstep < laststep) { 
     linkTagAttrs.class = 'nextLink' 
     linkParams.offset = offset + max 

     def params = attrs.params ?: [] 
     params['offset'] = linkParams.offset 

     linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params) 
     writer << link(linkTagAttrs.clone()) { 
     (attrs.next ? attrs.next : g.message(code: 'default.paginate.next', default: 'Next')) 
     } 
    } 

    } 
संबंधित मुद्दे