【问题标题】:Free jqGrid restoring date with custom formatter after cancelling inline edit取消内联编辑后使用自定义格式化程序免费 jqGrid 恢复日期
【发布时间】:2018-08-08 09:40:05
【问题描述】:

取消内联编辑后,日期列返回为未定义,而不是恢复原始值。列定义如下(日期以1970-01-01 格式出现):

{name:'Release<br>Date',index:'Street_Date', sorttype:"date", width:70,
    formatter: function (cellvalue, options, rowObject) {
        return cellvalue === ('1970-01-01') ? "" : $.fn.fmatter.call(this, "date", cellvalue, options, rowObject);
    },
    formatoptions: {newformat:'d M y'},
    editable:true,
    editoptions: {
        size:9, 
        dataInit: function(el, options) { 
                $(el).datepicker({ 
                    dateFormat: "d M y",
                    defaultDate: '01 Jan 70',
                    onSelect: function(dateText, inst) {
                    }
                });
        } 
    },
    searchoptions: {
        sopt: ['eq','ne','ge','le'],
        dataInit: function (elem) { 
            $(elem).datepicker({ showButtonPanel: true, dateFormat: 'yy-mm-dd' }) 
            } 
        }
},

内联编辑设置如下:

ondblClickRow: function (rowid) {
    var savedRows = $grid.jqGrid("getGridParam", "savedRow");

    if (savedRows.length > 0 && savedRows[0].id !== rowid) {
        // cancel editing 
        $grid.jqGrid("restoreRow", savedRows[0].id);
    }
    if (savedRows.length === 0) {
        $grid.jqGrid("editRow", rowid, editOptions);
    }
}

加载Grid后,日期显示为07 Aug 18,双击进入内联编辑,日期仍为07 Aug 18。通过单击离开或单击取消按钮取消编辑后,日期变为NaN undefined N。刷新后,它恢复正常。

取消编辑后如何保留正确的日期? 网格在formatter: date 下表现正确

免费的 jqGrid v jqGrid 4.13.5

【问题讨论】:

  • 使用哪个版本的 jqGrid - Guriddo jqGrid、free-jqGrid 或 jqGrid ver
  • @Tony Tomov,对不起,它是 jqGrid 4.13.5

标签: date formatter free-jqgrid inline-editing


【解决方案1】:

也许free-jqGrid的作者会帮助更好,但我建议你在调用格式化程序时添加额外的参数(action='edit')。代码如下:

formatter: function (cellvalue, options, rowObject) {
    return cellvalue === ('1970-01-01') ? "" : $.fn.fmatter.call(this, "date", cellvalue, options, rowObject, "edit");
},

注意$.fn.fmatter.call中的最后一个参数

更新

这在我的测试中有效。 由于您使用自定义日期格式化程序,因此需要将 savedRows 中的值取消格式化才能正确保存。在默认格式化程序 = 日期的情况下,这是自动完成的。

下面是可以使用的代码,假设你知道colModel中字段的索引:

ondblClickRow: function (rowid) {
    var savedRows = $grid.jqGrid("getGridParam", "savedRow");

    if (savedRows.length > 0 && savedRows[0].id !== rowid) {
        // cancel editing 
        savedRows[0].Release_Date = $.unformat.date.call($grid[0], savedRows[0].Release_Date,  $grid[0].p.colModel[1]);
        $grid.jqGrid("restoreRow", savedRows[0].id);
    }
    if (savedRows.length === 0) {
        $grid.jqGrid("editRow", rowid, editOptions);
    }
}

【讨论】:

  • 很遗憾,这没有任何影响,只是想对你和奥列格说个人谢谢
  • 是的,我在这里看到了您的逻辑,或者我可以复制并恢复原始值,但是,ondblClickRow 不会被调用,当您只需单击网格中的任何位置(即远离当前行编辑模式)或点击Cancel按钮...
  • 正如我在更新中解释的那样 - restoreRow 调用 setRowData 来设置值以防万一没有变化 - 即当取消按钮、esc 键等时 setRowData 在格式化程序日期自动转换为原始的情况下日期以保持混乱,但您使用自定义日期格式化程序。为了克服这个问题,需要在 savedRow 数组中的每个 editRow 之后使用自定义代码重新格式化日期。我刚刚举了一个例子。要完全解决问题,需要在每个 editRow 之后重新格式化 savedRow 中的日期字段。
  • 抱歉耽搁了,感谢您的帮助。我离开了一段时间......进一步的实验表明,这个问题只发生在formatoptions: {newformat:'d M y'}, 上,当我将它更改为formatoptions: {newformat:'Y-m-d'}, 时,它完全按预期工作。你知道为什么吗?是因为空格吗?没有任何formatoptions,默认日期格式是美式7/27/2018,也可以正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 2016-06-20
  • 1970-01-01
  • 2018-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多