我有一个 JQGrid treegrid,我需要将它从 ASP.NET MVC 项目转移到 WebForms 项目。
我决定在项目中使用 web 服务来返回网格的数据,因为我没有太多编写自定义处理程序的经验。
这是我现有的网格:
$("#TreeGrid").jqGrid({
url: '@Url.Action("GetSPTreeJSON", "Index")',
treeGrid: true,
datatype: "json",
mtype: "POST",
treeGridModel: 'adjacency',
postData: { currentUserGroup: currentUserGroup },
colNames: ["id", "partners"],
colModel: [
{ name: 'id', index: 'id', hidden: true, key: true },
{ name: 'partners', index: 'name', width: 500, sortable: false, classes: 'pointer' }
],
ExpandColumn: 'partners',
gridview: true
});
和现有的控制器动作:
public JsonResult GetSPTreeJSON(string nodeid, string n_level, Guid currentUserGroup)
{
List<TreeNode> list = clientService.GetTreeNodeList(nodeid, n_level, currentUserGroup);
var jsonData = new
{
page = 1,
total = 1,
records = 1,
rows = (
from TreeNode u in list
select new
{
cell = new object[] { u.Id.ToString(), u.name, u.level, u.ParentId, u.isLeaf, false }
}).ToList()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
示例 Json(用于根节点):
{
"page":1,
"total":1,
"records":1,
"rows":[
{
"cell":[
"5a1a9742-300f-11d3-941f-036008032006",
"root node",
0,
null,
false,
false
]
}
]
}
我的问题是:要让它在使用 json 数据和 web 服务的 asp.net webforms 项目中工作,究竟需要什么?
到目前为止我有什么半工作:
添加或更改了以下 jqGrid 选项:
url: 'JQGridData.asmx/GetSPTreeJSON', //now points to the webservice
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, //looks like it wont hit the webservice if this isn't included
serializeGridData: function (postData) { //looks like this is required because the webservice call will fail if not passed the correct parameters
if (postData.nodeid === undefined) postData.nodeid = null;
if (postData.n_level === undefined) postData.n_level = null;
return JSON.stringify(postData);
},
需要返回 JSON 的 WebService 骨架:
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetSPTreeJSON(string nodeid, string n_level, Guid currentUser)
{
List<TreeNode> list = clientService.GetTreeNodeList(nodeid, n_level, currentUserGroup);
var jsonData = new
{
page = 1,
total = 1,
records = 1,
rows = (
from TreeNode u in list
select new
{
cell = new object[] { u.Id.ToString(), u.name, u.level, u.ParentId, u.isLeaf, false }
}).ToList()
};
JObject json = JObject.Parse(jsonData);
return = json.ToString();
}
编辑:
按照 Olegs 的建议,我编辑了我的 webservice 方法,如下所示:
public class jqGridRecord
{
public Guid Id { get; set; }
public string Name { get; set; }
public int level { get; set; }
public Guid? parent { get; set; }
public bool isLeaf { get; set; }
public bool expanded { get; set; }
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class JQGridData : System.Web.Services.WebService
{
private readonly ClientService clientService = new ClientService();
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public JqGridData GetSPTreeJSON(string nodeid, string n_level, Guid currentUser)
{
List<TreeNode> list = clientService.GetTreeNodeList(nodeid, n_level, currentUser);
List<jqGridRecord> data = new List<jqGridRecord>();
foreach (var i in list)
{
jqGridRecord r = new jqGridRecord
{
Id = i.Id,
Name = i.name,
level = i.level,
parent = i.ParentId,
isLeaf = i.isLeaf,
expanded = false
};
data.Add(r);
}
int recordsCount = data.Count;
return new JqGridData()
{
total = 1,
page = 1,
records = recordsCount,
rows = data
};
}
}
public class JqGridData
{
public int total { get; set; }
public int page { get; set; }
public int records { get; set; }
public List<jqGridRecord> rows { get; set; }
}
并将以下内容添加到 jqGrid 选项中:
colModel: [
{ name: 'Id', hidden: true, key: true },
{ name: 'Name', width: 500, sortable: false, classes: 'pointer' }
],
jsonReader: { repeatitems: false, root: "d.rows", page: "d.page", total: "d.total", records: "d.records" },
现在为根返回以下 JSON,我可以在树中看到根节点,但无法单击 + 图标将其展开...看来 jsonReader 仍然存在某种问题?
{
"d":{
"__type":"SSDS.iValue.JqGridData",
"total":1,
"page":1,
"records":1,
"rows":[
{
"Id":"5a1a9742-3e0f-11d3-941f-006008032006",
"Name":"acsis Limited",
"level":0,
"parent":null,
"isLeaf":false,
"expanded":false
}
]
}
}