如何将jqGrid(4.x)配置为TreeView

时间:2012-02-07 18:39:21

标签: jqgrid

我已经使用了演示代码并尝试修改它以满足我的需求,但遗憾的是我无法获得所需的结果。

树网格正在加载和“绑定”但是在扩展和折叠期间层次结构似乎存在问题(我宁愿它在默认情况下保持扩展),看起来它正在对数据进行另一个ajax请求它不应该这样做。

第一次获取数据的调用将检索所需的所有功能。

DTO:

[Serializable]
public class Company
{
    public string CompanyId { get; set; }

    public string CompnayName { get; set; }

    public bool expanded { get; set; }

    public bool isLeaf { get; set; }

    public string level { get; set; }

    public string parent { get; set; }

    public static Company FromDataReader(OracleDataReader r)
    {
        return new Company
        {
            CompanyId = r["companyid"] is DBNull ? null : r["companyid"].ToString(),
            CompnayName = r["companyname"] is DBNull ? null : r["companyname"].ToString(),
            level = r["company_level"] is DBNull ? null : r["company_level"].ToString(),
            parent = r["parent_company_id"] is DBNull ? null : r["parent_company_id"].ToString(),
            isLeaf = Convert.ToInt64(r["child_count"]) == 0 ? true : false,
            expanded = true
        };
    }

}

控制器:

[HandleJsonException]
public JsonResult CompanyService()
{
    var profiler = MiniProfiler.Current;

    using (profiler.Step("Companies"))
    {
        var q = Repository.GetCompanies(SimpleSessionPersister.User.Id).ToList();

        var result = (from c in q
                      select new string[]
                             {
                                 Convert.ToString(c.CompanyId),
                                 c.CompnayName,
                                 c.level,
                                 c.parent,
                                 Convert.ToString(c.isLeaf),
                                 Convert.ToString(c.expanded)
                             }).ToArray();

        var jsonData = new
        {
            rows = result
        };

        return Json(jsonData, JsonRequestBehavior.AllowGet);
    }


}

网格初始化:

var companyGrid;

$(document).ready(function () {
var col_names = ['CompanyId', 'Company'];
var col_model = [
                    { name: 'CompanyId', index: 'CompanyId', width: 1, hidden: true, key: true },
                    { name: 'Company', index: 'Company', width: 300, }
                ];

companyGrid = $('#treegridCompanies');

companyGrid.jqGrid({
    url: '/xxx/yyy/zzz/',
    datatype: 'json',
    ajaxGridOptions: { contentType: "application/json" },
    jsonReader: {
        id: "CompanyId",
        cell: "",
        root: function (obj) { return obj.rows; },
        page: function () { return 1; },
        total: function () { return 1; },
        records: function (obj) { return obj.rows.length; },
        repeatitems: true
    },
    colNames: col_names,
    colModel: col_model,
    height: '100%',
    rowNum: 10000,
    sortable: false,
    treeGrid: true,
    ExpandColumn: 'Company'
});

网格Json数据:

{"rows":[["754568001","BrownStone Root","1","1736451491","False","True"],["2974914503","American Hotel Register - Administration","2","754568001","True","True"],["2976495739","Andre Dev Catalogs","2","754568001","True","True"],["2129814647","DARCY CO.","2","754568001","True","True"],["1736764899","EDITEST","2","754568001","False","True"],["1736764905","BrownStone - Kirsten Luisi","2","754568001","False","True"],["2977223369","TEST NEW BrownStone 2","2","754568001","True","True"],["2977223203","TESTING NEW BrownStone COMPANY","2","754568001","True","True"],["2161775149","B2BApp -  JONES","3","1736764905","False","True"],["2975929355","B2BApp - Bill Hirsch","3","1736764905","True","True"],["2975929365","B2BApp DEMO - Bill Hirsch","3","1736764905","True","True"],["1736764898","BrownStone - TBD - Dir of Accounting","3","1736764899","False","True"],["1736764903","BrownStone - Tom Packer","3","1736764905","False","True"],["2051802334","B2BApp - Alan Champagne","4","1736764903","True","True"],["2059110298","B2BApp - Jack Noriega","4","1736764903","True","True"],["2051800719","B2BApp - Steve Werner","4","1736764903","True","True"],["2129810951","KIRSTEN DEMO","4","2161775149","False","True"],["1736764897","BrownStone - Matt Fanning","4","1736764898","False","True"],["1737913153","BrownStone - Ramon Boatwright","4","1736764903","True","True"],["1736764996","BrownStone Hotels - Administration","4","1736764903","False","True"],["1766184823","ASSET MANAGED","5","1736764897","True","True"],["2977073604","CHECKBOOK GL PERIOD TEST","5","2129810951","True","True"],["2006901783","D&C Project Managers","5","1736764897","True","True"],["2976570081","DEMO CHECKBOOK","5","2129810951","False","True"],["2220489862","DEMO REPORTS","5","2129810951","True","True"],["2078446743","DISABLED BrownStone ACCOUNTS","5","1736764897","True","True"],["1736764902","BrownStone - Bob Alter","5","1736764996","False","True"],["1736764695","BrownStone Corporate Office","5","1736764897","True","True"],["2005626695","BrownStone HOTELS","5","1736764897","False","True"],["1736765065","BrownStone HOTELS","5","1736764897","False","True"],["2010587742","BrownStone HOTELS","5","1736764897","False","True"],["1736765062","BrownStone HOTELS","5","1736764897","False","True"],["1766200934","BrownStone HOTELS","5","1736764897","False","True"],["1736765059","BrownStone HOTELS","5","1736764897","False","True"],["1736765060","BrownStone HOTELS","5","1736764897","False","True"],["1736022491","BrownStone RVPS","5","1736764897","False","True"],["2977230825","TESTDEFAULTREGION1","5","2129810951","True","True"],["1735853749","COURTYARD  OLD TOWN SAN DIEGO","6","1736765060","True","True"],["1735854977","COURTYARD AIRPORT FRESNO","6","1736765062","True","True"],["1735879751","COURTYARD BY MARRIOTT OXNARD VENTURA","6","1736765062","True","True"],["1735854022","COURTYARD LAX","6","1736765062","True","True"],["1735853966","Courtyard Marriott -  Riverside","6","1736765060","True","True"],["1735952557","COURTYARD MARRIOTT LYNNWOOD","6","1736765062","True","True"],["1735879861","COURTYARD SANTA FE","6","1736765062","True","True"],["1766207732","Crowne Plaza Englewood","6","1736765059","True","True"],["1766210117","Crowne Plaza Ft. Magruder","6","1736765059","False","True"],["2131080915","DOUBLETREE HOTEL NASHVILLE","6","1736765060","True","True"],["1766212404","DOUBLETREE MINNEAPOLIS","6","1736765059","True","True"],["1766210705","EMBASSY SUITES CHICAGO","6","1736765059","True","True"],["1735853689","EMBASSY SUITES LAX","6","1736765062","True","True"],["1735952640","HAMPTON INN DENVER SE","6","2010587742","True","True"],["1735952441","HAMPTON INN PUEBLO","6","2010587742","True","True"],["1735880135","HAWTHORN SUITES KENT","6","1736765062","True","True"],["1735879490","HAWTHORN SUITES SACRAMENTO","6","1766200934","True","True"],["1766204777","HILTON DEL MAR","6","1736765060","True","True"],["1735953002","HILTON GARDEN INN LAKE OSWEGO","6","1766200934","True","True"],["1766205907","HILTON HUNTINGTON","6","1736765059","True","True"],["1735855012","HOLIDAY INN BOISE","6","1736765060","True","True"],["1735952366","HOLIDAY INN CRAIG","6","1736765065","True","True"],["1735854051","HOLIDAY INN EXPRESS OLD TOWN","6","1736765060","True","True"],["1735854128","HOLIDAY INN HOLLYWOOD","6","1736765062","True","True"],["1735854687","HOLIDAY INN PRICE","6","1736765060","True","True"],["1735854586","HOLIDAY INN PROVO","6","1736765060","True","True"],["1735854730","HOLIDAY INN ROCHESTER","6","1736765059","True","True"],["1735853861","Holiday Inn San Diego Downtown","6","1736765060","True","True"],["1735853545","HOLIDAY INN SAN DIEGO MISSION VALLEY STADIUM","6","1736765060","True","True"],["1735952275","HOLIDAY INN SELECT RENTON","6","1736765062","True","True"],["2058220502","HYATT NEWPORTER","6","2005626695","True","True"],["1735854840","Kahler Hotel - Rochester","6","1736765059","True","True"],["1740354058","Kahler Inn & Suites","6","1736765059","True","True"],["2977230876","KIRSTEN DEMO","6","2976570081","True","True"],["1735854224","Marriott - Ogden - DO NOT SEND PO\u0027s TO THIS ACCOUNT","6","1736765060","True","True"],["1735854556","Marriott - Park City","6","1736765060","True","True"],["1735854251","Marriott - Provo","6","1736765060","True","True"],["1735854354","Marriott - Salt Lake City","6","1736765060","True","True"],["1766213001","MARRIOTT GREENSPOINT","6","1736765060","True","True"],["1735879564","MARRIOTT NAPA VALLEY","6","1736765060","True","True"],["1766219965","MARRIOTT PHILADELPHIA WEST","6","2005626695","True","True"],["1735854205","MARRIOTT PORTLAND","6","1766200934","True","True"],["1735854270","MARRIOTT PUEBLO","6","1736765065","True","True"],["1735854104","MARRIOTT RIVERSIDE","6","1736765060","True","True"],["1735854900","MARRIOTT ROCHESTER","6","1736765059","True","True"],["1766220488","MARRIOTT TROY","6","2005626695","True","True"],["1766224945","MARRIOTT TYSONS CORNER","6","2005626695","True","True"],["2263401804","MIREPOIX RESTAURANT","6","1736765065","True","True"],["2977223544","NEWMANAGEMENTCOMPANY9","6","2976570081","True","True"],["2977231310","NEWSHUSTERMANAGEMENT2","6","2976570081","True","True"],["2977064515","NO CB Fiscal test 2","6","2976570081","True","True"],["2977064482","NO CB Fiscal Years test","6","2976570081","True","True"],["2977064560","NO FISCAL 3","6","2976570081","False","True"],["2977223526","NO FISCAL COMPANY 9","6","2976570081","True","True"],["1736772841","Ogden Eccles Conference Center","6","1736765060","True","True"],["1775491350","ONTARIO AIRPORT MARRIOTT","6","1736765060","True","True"],["1736765061","PJ SNOWDEN","6","1736022491","True","True"],["2085767632","RESIDENCE INN BEVERLY HILLS","6","1736765062","True","True"],["2010453862","RESIDENCE INN MANHATTAN BEACH","6","1736765062","True","True"],["1735854993","RESIDENCE INN OXNARD","6","1736765062","True","True"],["2134841291","RESIDENCE INN ROCHESTER","6","1736765059","True","True"],["1735879595","RESIDENCE INN SACRAMENTO","6","1766200934","True","True"],["1736020746","RESIDENCE INN SAN DIEGO","6","2010587742","True","True"],["1735854665","SHERATON CITY CENTRE SALT LAKE CITY","6","1736765060","True","True"],["2733122057","SHERATON HOTEL CERRITOS","6","1766200934","True","True"],["2010609773","SUE GREGORY","6","1736022491","True","True"],["1736764901","BrownStone - Mark Mance","6","1736764902","False","True"],["2529863176","TEST","6","2976570081","True","True"],["2977223362","TESTING77","6","2976570081","True","True"],["1736020559","TEXTILE CARE SERVICES ROCHESTER","6","1736765059","True","True"],["1736020575","TEXTILE CARE SERVICES SALT LAKE CITY","6","1736765060","True","True"],["1766225201","VALLEY RIVER INN","6","2005626695","True","True"],["1766211379","WYNDHAM GREENSPOINT","6","1736765060","True","True"],["2977064674","NO FISCAL 4","7","2977064560","False","True"],["1736764900","BrownStone - Evan Studer","7","1736764901","True","True"],["2977223101","TESTING 777","7","2977064560","True","True"],["2977223196","TESTING11","7","2977064560","True","True"],["2977223169","TESTING7","7","2977064560","True","True"],["2977231370","TESTNEWCROWN","7","1766210117","True","True"],["2977222940","TESTING3","8","2977064674","False","True"],["2977222949","TESTING4","8","2977064674","True","True"],["2977222965","TESTING6","8","2977064674","True","True"]]}

谢谢你, 斯蒂芬

代码审核后

[Serializable]
public class Company
{
    public string CompanyId { get; set; }

    public string CompanyName { get; set; }

    public bool isLeaf { get; set; }

    public string level { get; set; }

    public string parent { get; set; }

    public static Company FromDataReader(OracleDataReader r)
    {
        return new Company
        {
            CompanyId = r["companyid"] is DBNull ? null : r["companyid"].ToString(),
            CompanyName = r["companyname"] is DBNull ? null : r["companyname"].ToString(),
            level = r["company_level"] is DBNull ? null : r["company_level"].ToString(),
            parent = r["parent_company_id"] is DBNull ? null : r["parent_company_id"].ToString(),
            isLeaf = Convert.ToInt64(r["child_count"]) == 0 ? true : false
        };
    }

}
companyGrid.jqGrid({
 url: '/xxx/yyy/zzz/',
 datatype: 'json',
 ajaxGridOptions: { contentType: "application/json" },
 jsonReader: {
     id: "CompanyId",
     cell: "",
     root: function (obj) { return obj.rows; },
     page: function () { return 1; },
     total: function () { return 1; },
     records: function (obj) { return obj.rows.length; },
     repeatitems: true
 },
 beforeProcessing: function (data) {
    var rows = data.rows, i, l = rows.length;
    for (i = 0; i < l; i++) {
        rows[i][5] = true; // set expanded column
        rows[i][6] = true; // set loaded column
    }
 },
 colNames:  ['CompanyId', 'Company'],
 colModel: [
                { name: 'CompanyId', index: 'CompanyId', width: 1, hidden: true, key: true },
                { name: 'Company', index: 'Company', width: 300, }
            ],
 height: '200px',
 autowidth: true,
 rowNum: 10000,
 sortable: false,
 treeGrid: true,
 treeGridModel: 'adjacency',
 ExpandColumn: 'Company'
});
{"rows":[["754568001","BrownStone Root","1","1736451491","False","True"],["1736764899","EDITEST","2","754568001","False","True"],["1736764898","BrownStone - TBD - Dir of Accounting","3","1736764899","False","True"],["1736764897","BrownStone - Matt Fanning","4","1736764898","False","True"],["1736022491","BrownStone RVPS","5","1736764897","False","True"],["1736765061","PJ SNOWDEN","6","1736022491","True","True"],["2010609773","SUE GREGORY","6","1736022491","True","True"],["1736764695","BrownStone Corporate Office","5","1736764897","True","True"],["1736765059","BrownStone HOTELS","5","1736764897","False","True"],["1735854730","HOLIDAY INN ROCHESTER","6","1736765059","True","True"],["1735854840","Kahler Hotel - Rochester","6","1736765059","True","True"],["1735854900","MARRIOTT ROCHESTER","6","1736765059","True","True"],["1736020559","TEXTILE CARE SERVICES ROCHESTER","6","1736765059","True","True"],["1740354058","Kahler Inn & Suites","6","1736765059","True","True"],["1766205907","HILTON HUNTINGTON","6","1736765059","True","True"],["1766207732","Crowne Plaza Englewood","6","1736765059","True","True"],["1766210117","Crowne Plaza Ft. Magruder","6","1736765059","False","True"],["2977231370","TESTNEWCROWN","7","1766210117","True","True"],["1766210705","EMBASSY SUITES CHICAGO","6","1736765059","True","True"],["1766212404","DOUBLETREE MINNEAPOLIS","6","1736765059","True","True"],["2134841291","RESIDENCE INN ROCHESTER","6","1736765059","True","True"],["1736765060","BrownStone HOTELS","5","1736764897","False","True"],["1735853545","HOLIDAY INN SAN DIEGO MISSION VALLEY STADIUM","6","1736765060","True","True"],["1735853749","COURTYARD  OLD TOWN SAN DIEGO","6","1736765060","True","True"],["1735853861","Holiday Inn San Diego Downtown","6","1736765060","True","True"],["1735853966","Courtyard Marriott -  Riverside","6","1736765060","True","True"],["1735854051","HOLIDAY INN EXPRESS OLD TOWN","6","1736765060","True","True"],["1735854104","MARRIOTT RIVERSIDE","6","1736765060","True","True"],["1735854224","Marriott - Ogden - DO NOT SEND PO\u0027s TO THIS ACCOUNT","6","1736765060","True","True"],["1735854251","Marriott - Provo","6","1736765060","True","True"],["1735854354","Marriott - Salt Lake City","6","1736765060","True","True"],["1735854556","Marriott - Park City","6","1736765060","True","True"],["1735854586","HOLIDAY INN PROVO","6","1736765060","True","True"],["1735854665","SHERATON CITY CENTRE SALT LAKE CITY","6","1736765060","True","True"],["1735854687","HOLIDAY INN PRICE","6","1736765060","True","True"],["1735855012","HOLIDAY INN BOISE","6","1736765060","True","True"],["1735879564","MARRIOTT NAPA VALLEY","6","1736765060","True","True"],["1736020575","TEXTILE CARE SERVICES SALT LAKE CITY","6","1736765060","True","True"],["1736772841","Ogden Eccles Conference Center","6","1736765060","True","True"],["1766204777","HILTON DEL MAR","6","1736765060","True","True"],["1766211379","WYNDHAM GREENSPOINT","6","1736765060","True","True"],["1766213001","MARRIOTT GREENSPOINT","6","1736765060","True","True"],["1775491350","ONTARIO AIRPORT MARRIOTT","6","1736765060","True","True"],["2131080915","DOUBLETREE HOTEL NASHVILLE","6","1736765060","True","True"],["1736765062","BrownStone HOTELS","5","1736764897","False","True"],["1735853689","EMBASSY SUITES LAX","6","1736765062","True","True"],["1735854022","COURTYARD LAX","6","1736765062","True","True"],["1735854128","HOLIDAY INN HOLLYWOOD","6","1736765062","True","True"],["1735854977","COURTYARD AIRPORT FRESNO","6","1736765062","True","True"],["1735854993","RESIDENCE INN OXNARD","6","1736765062","True","True"],["1735879751","COURTYARD BY MARRIOTT OXNARD VENTURA","6","1736765062","True","True"],["1735879861","COURTYARD SANTA FE","6","1736765062","True","True"],["1735880135","HAWTHORN SUITES KENT","6","1736765062","True","True"],["1735952275","HOLIDAY INN SELECT RENTON","6","1736765062","True","True"],["1735952557","COURTYARD MARRIOTT LYNNWOOD","6","1736765062","True","True"],["2010453862","RESIDENCE INN MANHATTAN BEACH","6","1736765062","True","True"],["2085767632","RESIDENCE INN BEVERLY HILLS","6","1736765062","True","True"],["1736765065","BrownStone HOTELS","5","1736764897","False","True"],["1735854270","MARRIOTT PUEBLO","6","1736765065","True","True"],["1735952366","HOLIDAY INN CRAIG","6","1736765065","True","True"],["2263401804","MIREPOIX RESTAURANT","6","1736765065","True","True"],["1766184823","ASSET MANAGED","5","1736764897","True","True"],["1766200934","BrownStone HOTELS","5","1736764897","False","True"],["1735854205","MARRIOTT PORTLAND","6","1766200934","True","True"],["1735879490","HAWTHORN SUITES SACRAMENTO","6","1766200934","True","True"],["1735879595","RESIDENCE INN SACRAMENTO","6","1766200934","True","True"],["1735953002","HILTON GARDEN INN LAKE OSWEGO","6","1766200934","True","True"],["2733122057","SHERATON HOTEL CERRITOS","6","1766200934","True","True"],["2005626695","BrownStone HOTELS","5","1736764897","False","True"],["1766219965","MARRIOTT PHILADELPHIA WEST","6","2005626695","True","True"],["1766220488","MARRIOTT TROY","6","2005626695","True","True"],["1766224945","MARRIOTT TYSONS CORNER","6","2005626695","True","True"],["1766225201","VALLEY RIVER INN","6","2005626695","True","True"],["2058220502","HYATT NEWPORTER","6","2005626695","True","True"],["2006901783","D&C Project Managers","5","1736764897","True","True"],["2010587742","BrownStone HOTELS","5","1736764897","False","True"],["1735952441","HAMPTON INN PUEBLO","6","2010587742","True","True"],["1735952640","HAMPTON INN DENVER SE","6","2010587742","True","True"],["1736020746","RESIDENCE INN SAN DIEGO","6","2010587742","True","True"],["2078446743","DISABLED BrownStone ACCOUNTS","5","1736764897","True","True"],["1736764905","BrownStone - Kirsten Luisi","2","754568001","False","True"],["1736764903","BrownStone - Tom Packer","3","1736764905","False","True"],["1736764996","BrownStone Hotels - Administration","4","1736764903","False","True"],["1736764902","BrownStone - Bob Alter","5","1736764996","False","True"],["1736764901","BrownStone - Mark Mance","6","1736764902","False","True"],["1736764900","BrownStone - Evan Studer","7","1736764901","True","True"],["1737913153","BrownStone - Ramon Boatwright","4","1736764903","True","True"],["2051800719","B2BApp - Steve Werner","4","1736764903","True","True"],["2051802334","B2BApp - Alan Champagne","4","1736764903","True","True"],["2059110298","B2BApp - Jack Noriega","4","1736764903","True","True"],["2161775149","B2BApp -  JONES","3","1736764905","False","True"],["2129810951","KIRSTEN DEMO","4","2161775149","False","True"],["2220489862","DEMO REPORTS","5","2129810951","True","True"],["2976570081","DEMO CHECKBOOK","5","2129810951","False","True"],["2529863176","TEST","6","2976570081","True","True"],["2977064482","NO CB Fiscal Years test","6","2976570081","True","True"],["2977064515","NO CB Fiscal test 2","6","2976570081","True","True"],["2977064560","NO FISCAL 3","6","2976570081","False","True"],["2977064674","NO FISCAL 4","7","2977064560","False","True"],["2977222940","TESTING3","8","2977064674","False","True"],["2977222949","TESTING4","8","2977064674","True","True"],["2977222965","TESTING6","8","2977064674","True","True"],["2977223101","TESTING 777","7","2977064560","True","True"],["2977223169","TESTING7","7","2977064560","True","True"],["2977223196","TESTING11","7","2977064560","True","True"],["2977223362","TESTING77","6","2976570081","True","True"],["2977223526","NO FISCAL COMPANY 9","6","2976570081","True","True"],["2977223544","NEWMANAGEMENTCOMPANY9","6","2976570081","True","True"],["2977230876","KIRSTEN DEMO","6","2976570081","True","True"],["2977231310","NEWSHUSTERMANAGEMENT2","6","2976570081","True","True"],["2977073604","CHECKBOOK GL PERIOD TEST","5","2129810951","True","True"],["2977230825","TESTDEFAULTREGION1","5","2129810951","True","True"],["2975929355","B2BApp - Bill Hirsch","3","1736764905","True","True"],["2975929365","B2BApp DEMO - Bill Hirsch","3","1736764905","True","True"],["2129814647","DARCY CO.","2","754568001","True","True"],["2974914503","American Hotel Register - Administration","2","754568001","True","True"],["2976495739","Andre Dev Catalogs","2","754568001","True","True"],["2977223203","TESTING NEW BrownStone COMPANY","2","754568001","True","True"],["2977223369","TEST NEW BrownStone 2","2","754568001","True","True"]]}

1 个答案:

答案 0 :(得分:1)

您在服务器部分遇到一些问题,这些问题将被转发到JSON。

第一个简单的问题是Company类。您输入了错误并使用了CompnayName而不是CompanyName。结果是代码的第一个修订版的数据无法读取CompanyName。更改为string[]repeatitems: true后,系统会读取公司名称。

下一个问题是您忘记包含jqGrid的treeGridModel: 'adjacency'选项,输入数据将以错误的方式解释。

JSON数据中的下一个问题是您应该另外将网格的loaded列设置为true。隐藏的loaded列是expanded之后的下一列。如果您想要始终加载所有数据,可以在beforeProcessing

中解决问题
beforeProcessing: function (data) {
    var rows = data.rows, i, l = rows.length;
    for (i = 0; i < l; i++) {
        rows[i][6] = true; // set loaded column
    }
}

以同样的方式,您可以将expanded列设置为true,例如(rows[i][5] = true),您希望减少传输数据的大小。

我在JSON数据中可以看到的最后一个问题是节点顺序。您应该了解Tree Grid的工作非常简单。它会将所有行以完全相同的顺序放置在网格中,以便发布数据。可以隐藏孩子,但所有孩子必须直接在其父节点之后。因此,您应该更改服务器响应中放置的数据的顺序。