6

プロジェクトでタグを使用しています。grails.org でカスタム タグをブラウジングして、自分のライブラリ用の新しいタグを見つけました。

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

私は、StackOverflow コミュニティの人々が共有したいお気に入りのカスタム タグを持っているかどうか疑問に思っていました。

4

3 に答える 3

1

DecimalFormat クラス (および拡張による Grails の formatNumber タグ) は、特定のユース ケースでは少し不透明であり、適切なフォーマットを生成するための醜い前処理を行わずに、かなり基本的なフォーマットを行う合理的な方法をまだ見つけていません。ストリング。私は数ヶ月前に単純な数値書式タグをまとめました。これは基本的に書式文字列を構築し、数値自体に最小限の処理を行います。

それは私が望むほど一般的でもエレガントでもありません (当時必要だったのはこれだけです。非常に基本的なものですが、それでも GSP から醜い処理を排除しています)。些細な改善が必要です (つまり、単純な if-elseif slop の代わりにスケーリングの反復を行い、ユーザーがカスタム スケーリング マーカーを渡せるようにし、カスタム数値バリデーターをパラメーターとして使用できるようにするなど)。


// 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
    }
}
于 2009-09-01T02:25:52.133 に答える
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'))
      }
    }

  }
于 2009-08-31T19:49:50.073 に答える
1

「fmt:relDate」タグを使用すると、「3 日前」、「30 秒以内」などの相対的な日付をツールチップとしてリアルタイムで表示できます。

現在の実装は基本的に、私が好きな境界を持つ if/then ステートメントの巨大なチェーンです。二分探索ベースのアルゴリズムの方が (「より効率的」という意味で) 優れており、現在の実装には私の個人的な好みがエンコードされているため、タグを共有するのは気が進まない.

于 2008-10-20T17:51:03.917 に答える