c# 部门List数据怎么使用 jqgrid 展示树形数据
时间: 2024-03-05 21:51:00 浏览: 108
jqgrid资料
以下是使用 jqGrid 展示树形数据的 C# 代码示例:
1. 创建一个 C# 类型来表示部门信息
```csharp
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public int? ParentId { get; set; }
public string Manager { get; set; }
}
```
2. 创建一个 List 对象来存储所有部门信息
```csharp
List<Department> departments = new List<Department>();
```
3. 将部门信息添加到 List 对象中
```csharp
departments.Add(new Department { Id = 1, Name = "总经理办公室", ParentId = null, Manager = "李总" });
departments.Add(new Department { Id = 2, Name = "人事部", ParentId = 1, Manager = "张经理" });
departments.Add(new Department { Id = 3, Name = "财务部", ParentId = 1, Manager = "王经理" });
departments.Add(new Department { Id = 4, Name = "市场部", ParentId = 1, Manager = "赵经理" });
departments.Add(new Department { Id = 5, Name = "销售部", ParentId = 4, Manager = "刘经理" });
```
4. 将部门列表转换为 jqGrid 树形格式的 JSON 数据
```csharp
public JsonResult GetTreeData()
{
var treeData = from d in departments
select new
{
id = d.Id,
parent = d.ParentId.HasValue ? d.ParentId.Value.ToString() : "#",
text = d.Name,
data = d.Manager
};
return Json(treeData, JsonRequestBehavior.AllowGet);
}
```
5. 在前端页面中使用 jqGrid 展示树形数据
```javascript
$(function () {
$("#treeGrid").jqGrid({
url: "/Department/GetTreeData",
datatype: "json",
mtype: "GET",
colModel: [
{ label: '部门名称', name: 'text', width: 200 },
{ label: '部门经理', name: 'data', width: 200 }
],
treeGrid: true,
treeGridModel: 'adjacency',
ExpandColumn: 'text',
ExpandColClick: true,
rowNum: 10000,
height: 'auto',
caption: '部门列表'
});
});
```
以上代码仅供参考,实际实现时可能需要根据具体情况进行修改。注意,在序列化时需要引入 System.Web.Script.Serialization 命名空间,使用 jqGrid 时需要引入相关的 JavaScript 和 CSS 文件。
阅读全文