【问题标题】:Date display in handsontable手持式日期显示
【发布时间】:2013-06-28 21:00:40
【问题描述】:

这是我所拥有的(它的相关部分)。

Handsontable 选项:

data: [[new Date(2013,5,26,17,00,00),24.7025,null,29.018950276,19.7746530531,null,null,null,null,null,null,55,110,165,220], ...

columns: [{type:'date', dateFormat: 'mm/dd/yy'},{type: 'numeric',format: '0,0.00'}, ...

结果如可用日期单元格所示:

Wed Jun 26 2013 17:00:00 GMT+0200 (Romance Daylight Time)

我想将它显示为“dd-MM-yyyy HH:mm”,但我还没有找到这样做的方法……显示似乎总是坚持默认的日期格式,如上图所示.

【问题讨论】:

  • 你不能用to_date函数吗??
  • 在我的情况下(出于与其他对象兼容性的其他原因)源必须保留在本机 javascript 日期中,在本例中为 new Date(2013,5,26,17,00,00)。我在这里唯一的余地是强制 Handsontable 通过其选项 columns 将此原生 javascript 日期格式化为格式化字符串。
  • 您能否展示您完整的可动手操作的初始化代码,或许还有更多背景知识?

标签: javascript handsontable


【解决方案1】:

在这种情况下,您可以使用自定义渲染器。

我已经创建了 jquery.handsontable.exts.js,我正在使用 handsontable.js 加载它。

此示例脚本使用 renderFormat 或 dateFormat 属性正确呈现日期。

    /**
    * Handsontable date renderer
    * @param {Object} instance Handsontable instance
    * @param {Element} TD Table cell where to render
    * @param {Number} row
    * @param {Number} col
    * @param {String|Number} prop Row object property name
    * @param value Value to render (remember to escape unsafe HTML before inserting to DOM!)
    * @param {Object} cellProperties Cell properites (shared by cell renderer and editor)
    */
    Handsontable.DateRenderer = function (instance, TD, row, col, prop, value, cellProperties) {
        if (value && typeof value == "object" && value.constructor == Date) {
            // Date!
            var format = cellProperties.renderFormat || cellProperties.dateFormat || "";
            value = $.datepicker.formatDate(format, value); //value.format(format);
        }
        if (cellProperties.readOnly)
            Handsontable.TextRenderer(instance, TD, row, col, prop, value, cellProperties);
        else
            Handsontable.AutocompleteRenderer(instance, TD, row, col, prop, value, cellProperties);
    };

    Handsontable.DateCell.renderer = Handsontable.DateRenderer;
    Handsontable.cellLookup.renderer.date = Handsontable.DateRenderer;

【讨论】:

    【解决方案2】:

    4 年后,我在Handsontable documentation 中找到了答案。我想我还是会分享...

    hot = new Handsontable(container, {
        data: getDataFromSomewhere(),
        colHeaders: ['SomeText', 'SomeDate'],
        columns: [
          {
            // 1st cell is simple text, no special options here
          },
          {
            type: 'date',
            dateFormat: 'MM/DD/YYYY',
            correctFormat: true,
            defaultDate: '01/01/1900'
          }
        ]
    });
    

    这样做的缺点是您需要使用三个额外的库:

    • /dist/moment/moment.js
    • /dist/pikaday/pikaday.js
    • /dist/pikaday/css/pikaday.css

    它会自动添加一个daypicker(这不是我想要的……我也需要HH:mm)。

    【讨论】:

      【解决方案3】:
          columns: [
              { data: 'Changed', renderer: dateTimeRenderer, readOnly: true },
          ],
      
      function dateTimeRenderer(instance, td, row, col, prop, value, cellProperties) {
          value = typeof value === "undefined" ? '' : new Date(value).toLocaleString();
          return Handsontable.renderers.TextRenderer(instance, td, row, col, prop, value, cellProperties);
      }
      

      【讨论】:

        猜你喜欢
        • 2016-01-19
        • 2016-07-07
        • 1970-01-01
        • 1970-01-01
        • 2020-11-06
        • 2014-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多