更新jqGrid行时格式丢失

Formatting lost when updating jqGrid row

本文关键字:格式 jqGrid 更新      更新时间:2023-09-26

显示食谱的jqGrid表,并为用户提供主-详细信息类型视图。当用户从网格中选择一个食谱时,它会在网格下方的div中显示该食谱的详细信息。然后,我在该div中提供就地编辑功能。当用户保存编辑时,我将详细信息重新显示到配方中。这一切都运行得很好。现在,所选的网格行可能具有与更新后的详细信息显示的数据不匹配的数据,因此我执行以下操作来更新网格:

$.ajax({
   type: "GET",
   data: "id=" recipeId,
   url:  '@Url.Action("GetGridDataForRecipe", "Recipe")',
   dataType: "json",
   success: function (result) {
      var myGrid = $("#recipeGrid");
      var selRowId = myGrid.jqGrid('getGridParam', 'selrow');
      myGrid.jqGrid('setRowData', selRowId, result);
   }
});

我的控制器动作看起来像这样:

public JsonResult GetGridDataForRecipe(int id)
{
   // ...
   var recipeData = context.recipes.Where(m => m.RecipeId == id).Select(row => new
   {
      RecipeId = row.RecipeId,
      RecipeName = row.RecipeName,
      RecipeDate = row.RecipeDate,
   }).First();
   return Json(recipeData, JsonRequestBehavior.AllowGet);
}

因此,更新工作几乎完美,除了RecipeDate条目最终显示如下:

/Date(1317182400000)/

而不是格式化日期:

10/03/2011

我在colModel中指定的,当我返回网格行:

{ name: 'RecipeDate', index: 'RecipeDate', width: 120, align: 'left', sorttype: 'date',
   formatter: 'date', formatoptions: { newformat: 'm/d/Y'},
...

我在显示网格时指定的colModel与我稍后更新的数据之间存在脱节。我需要重新指定这些信息吗?我怎么做呢?

我需要重新指定这个信息吗?

是的。

我该怎么做?

你可以在从控制器动作返回的匿名对象中执行此格式化:

var recipeData = context.recipes.Where(m => m.RecipeId == id).Select(row => new
{
    RecipeId = row.RecipeId,
    RecipeName = row.RecipeName,
    RecipeDate = row.RecipeDate.ToString("MM/dd/yyyy"),
}).First();

在GitHub上找到答案。

添加.jgrid.formatter.date美元。reformatAfterEdit = true;在我调用setRowData之前,现在似乎是一个很好的工作。

我添加了这段代码,我的日期现在显示为我期望的格式。