更新 jqGrid 行时格式丢失

发布于 2024-12-08 01:28:40 字数 1354 浏览 0 评论 0原文

我显示一个 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 之间存在脱节> 我在显示网格和数据时指定我稍后更新。我需要重新指定此信息吗?我该怎么做?

I display a jqGrid table of recipes and provide a master-details type view for the user. When the user selects a recipe from the grid, it displays the details of that recipe in a div below the grid. Then, I provide an in-place editing capability inside that div. When the user saves the edits, I redisplay the details to the recipe. That all works well enough. Now, the selected grid row may have data which doesn't match what the details show after the update, so I do something like this to update the grid:

$.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);
   }
});

My controller action looks like so:

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);
}

So, the update works almost perfectly with the exception that the RecipeDate entry ends up getting displayed like so:

/Date(1317182400000)/

rather than the formatted date:

10/03/2011

that I specified in the colModel when I return the grid rows:

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

There's a disconnect here between the colModel that I specified when the grid is displayed and the data I'm updating later. Do I need to re-specify this information? How do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶茶白久 2024-12-15 01:28:40

我需要重新指定此信息吗?

是的。

我该怎么做?

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

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();

Do I need to re-specify this information?

Yes.

How do I do that?

You could perform this formatting in the anonymous object your are returning from your controller action:

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();
三月梨花 2024-12-15 01:28:40

GitHub 上找到了答案

添加 $.jgrid.formatter.date.reformatAfterEdit = true;在我调用 setRowData 之前,目前看来是一个很好的解决方法。

我添加了这段代码,我的日期现在按照我的预期格式化了。

Found an answer on GitHub.

Adding $.jgrid.formatter.date.reformatAfterEdit = true; before I call setRowData seems to be a good work around for now.

I added this code and my dates now come out formatted as I expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文