更新 jqGrid 行时格式丢失
我显示一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。
您可以在从控制器操作返回的匿名对象中执行此格式化:
Yes.
You could perform this formatting in the anonymous object your are returning from your controller action:
在GitHub 上找到了答案。
我添加了这段代码,我的日期现在按照我的预期格式化了。
Found an answer on GitHub.
I added this code and my dates now come out formatted as I expect.