【问题标题】:JQGrid 4.4.0: 'date' formatter srcformat/newformat now tries to format date when source date's format does not match srcformatJQGrid 4.4.0:“日期”格式化程序 srcformat/newformat 现在尝试在源日期的格式与 srcformat 不匹配时格式化日期
【发布时间】:2012-07-09 19:18:05
【问题描述】:

我有一个包含日期列的 JQGrid。我使用日期格式化程序将日期格式化为m/d/Y 格式。以前,如果源日期的格式与我传入的formatoptions 中的srcformat 不匹配,它就不会格式化日期。无论源格式如何,JQGrid v4.4.0 现在都尝试格式化日期,并提出了很远的日期:-)。

我填充到此列中的日期可能已经是正确的格式 (m/d/Y),也可能是我在 srcformat (Y-m-dTH:i:s) 中定义的格式。

在 JQGrid 4.4.0 中是否有办法不尝试解析与 srcformat 不匹配的日期?


列的我的 colModel def:

{name:"date", index:"date", label:"Date", width:85, jsonmap:"date", hidden: false, formatter:'date', sorttype: 'date', formatoptions:{srcformat:'Y-m-dTH:i:s', newformat:'m/d/Y'}, searchrules: { date: true } }        

【问题讨论】:

    标签: jquery jqgrid formatter date-formatting


    【解决方案1】:

    我解决了我自己的问题:)

    我创建了一个自定义格式化程序并使用 Datejs JQuery 库来帮助解析日期。

    如果日期是 Y-m-dTH:i:s 格式,基本上格式化程序只会将日期格式化为 m/d/Y 格式;否则,它假定它已经是 m/d/Y 格式并保持不变。

    /**
     * This function formats the date column for the summary grid.
     * 
     * The grid could be populated with dates that are in m/d/Y format or in Y-m-dTH:i:s format; need
     * to account for this; want the dates to end up being in m/d/Y format always.
     * 
     * @param cellvalue     is the value to be formatted
     * @param options       an object containing the following element
     *                      options : { rowId: rid, colModel: cm} where rowId - is the id of the row colModel is the object of the properties for this column getted from colModel array of jqGrid
     * @param rowObject     is a row data represented in the format determined from datatype option;
     *                      the rowObject is array, provided according to the rules from jsonReader
     * @return              the new formatted cell value html
     */
    function summaryGridDateFormatter(cellvalue, options, rowObject) {
    
        // parseExact just returns 'null' if the date you are trying to 
        // format is not in the exact format specified
        var parsedDate = Date.parseExact(cellvalue, "yyyy-MM-ddTHH:mm:ss"); 
    
        // if parsed date is null, just used the passed cell value; otherwise, 
        // transform the date to desired format
        var formattedDate = parsedDate ? parsedDate.toString("MM/dd/yyyy") : cellvalue;
    
        return formattedDate;
    }
    

    【讨论】:

      【解决方案2】:

      @icats:谢谢!它真的很有帮助 - 我开始变得非常沮丧......

      实际上我不得不稍微修改一下你的函数。 我从数据库中获得了一个 timestamp 字段,该字段通过 JSON 作为 毫秒 的值返回(即,实际值类似于:1343314489564)。因此,我添加了对 parsedDate 的第二次检查,如下所示:

      /**
       * This function formats the date column for the  grid.
       *
       * The grid could be populated with dates that are in m/d/Y format or in Y-m-dTH:i:s format; need
       * to account for this; want the dates to end up being in m/d/Y format always.
       *
       * @param cellvalue     is the value to be formatted
       * @param options       an object containing the following element
       *                      options : { rowId: rid, colModel: cm} where rowId - is the id of the row colModel is the object of the properties for this column getted from colModel array of jqGrid
       * @param rowObject     is a row data represented in the format determined from datatype option;
       *                      the rowObject is array, provided according to the rules from jsonReader
       * @return              the new formatted cell value html
       */
      function dateFormatter(cellvalue, options, rowObject) {
      
          // parseExact just returns 'null' if the date you are trying to
          // format is not in the exact format specified
          var parsedDate = Date.parseExact(cellvalue, "yyyy-MM-ddTHH:mm:ss");
          if(parsedDate == null )
              parsedDate = new Date(cellvalue);
      
          // if parsed date is null, just used the passed cell value; otherwise,
          // transform the date to desired format
          var formattedDate = parsedDate ? parsedDate.toString("yyyy-MM-dd HH:mm:ss") : cellvalue;
      
          return formattedDate;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-24
        • 1970-01-01
        • 2011-09-22
        • 2012-08-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多