2011-11-03 100 views
0

我有一個jqGrid的問題。 jqGrid版本4.2.0 - jQuery網格 網格顯示加載數據不是樹模式,只有網格模式。爲什麼? 請幫幫我! Можнопорусски。 加入標題jqGrid不是樹模式爲什麼?

<link href="../jqGrid/css/ui.jqgrid.css" rel="stylesheet" type="text/css" /> 
    <script src="jqGrid/js/i18n/grid.locale-ru.js" type="text/javascript"></script> 
    <script src="jqGrid/js/jquery.jqGrid.min.js" type="text/javascript"></script> 

和我從web服務獲取數據的函數。從服務器服務

function getDataSC(pData) { 
    $.ajax({ 
     type: 'POST', 
     contentType: "application/json; charset=utf-8", 
     url: '<%= ResolveClientUrl("~/FetchData.asmx/bindSCJson") %>', 
     dataType: "json", 
     success: function(data, textStatus) { 
     if (textStatus == "success") 
      ReceivedClientDataSC(JSON.parse(getMain(data)).rows); 
    }, 
    error: function(data, textStatus) { 
     alert('An error has occured retrieving data!'); 
    } 
}); 
} 
function ReceivedClientDataSC(data) { 
    var thegrid = $("#gridSC"); 
    thegrid.clearGridData(); 
    for (var i = 0; i < data.length; i++) 
     thegrid.addRowData(i + 1, data[i]); 
} 
function getMain(dObj) { 
    if (dObj.hasOwnProperty('d')) 
     return dObj.d; 
    else 
     return dObj; 
} 

var lastSel; 
$("#gridSC").jqGrid({ 
datatype: function(pdata) { getDataSC(pdata); }, 

treeGrid: true, 
treeGridModel : 'adjacency', 
ExpandColumn: 'Name', 
ExpandColClick: true , 
mtype: 'POST', 
treeIcons: {plus:'ui-icon-circle-plus',minus:'ui-icon-circle-minus',leaf:'ui-  
icon-person'}, 

height: "100%", 
width: 900, 
    colNames: ['Id', 'Name', 'CountryName', 'Town', 'Adress', 'Phone', 'Email', 
'Url'], 
    colModel: [ 
        { name: 'Id', index:'id', width:10, hidden:true,key:true}, 
        { name: 'Name', index: 'Name', width: 80}, 
        { name: 'CountryName',index:'CountryName', width:80}, 
        { name: 'Town', index: 'Town', width: 20 }, 
        { name: 'Adress', index: 'Adress', width: 90 }, 
        { name: 'Phone', index: 'Phone', width: 20}, 
        { name: 'Email', index: 'Email', width: 20 }, 
        { name: 'Url', index: 'Url', width: 30} 
        ], 
     pager: $('#pjmapSC') 
     //rowNum:20, 
     //viewrecords: true, 
     //gridview: true, 
     //rowList:[10,20,30,100], 
     //sortname: 'Id', 
     //sortorder: 'asc' 

    }) 

    } 

JSON(加載到網格):

{"d":"{\"total\":1,\"page\":1,\"records\":2,\"rows\":[{\"err\":null,\"Id\":1,\"Parent 
\":0,\"Name\":\"Сервисные центры\",\"CountryCode\":103,\"Town\":\"Киев\",\"Adress 
\":\"Красных партизан 1\",\"Phone\":\"123-321\",\"Email\":\"[email protected] 
\",\"CountryName\":\"United Arab Emirates\",\"Url\":\"www.service1\",\"isLeaf\":false, 
\"Expanded\":true,\"Level\":1},{\"err\":null,\"Id\":2,\"Parent\":1,\"Name 
\":\"Сервисный центр 1_1\",\"CountryCode\":103,\"Town\":\"Киев\",\"Adress\":\"Артема 
\",\"Phone\":\"123-321\",\"Email\":\"[email protected]\",\"CountryName\":\"United Arab 
Emirates\",\"Url\":\"www.service2\",\"isLeaf\":true,\"Expanded\":true,\"Level\":2}], 
\"userData\":null}"} 

幫助請!謝謝。

編輯:

感謝奧列格,但我盡我的服務器字符串變成

{"d":{"__type":"_admin.JqGridData","total":3,"page":1,"records":26,"rows":  [{"id":1,"cell":["1","Сервисные центры","United Arab Emirates","Киев","Красных партизан 1","123-321","[email protected]","www.service1","0",null,"False","False"]},{"id":2,"cell":["2","Сервисный центр 1_1","United Arab Emirates","Киев","Артема","123-321","[email protected]","www.service2","1","1","True","False"]},{"id":4,"cell":["4","Сервисный центр 1_2","United Arab Emirates","Донецк","Артема","123-321","[email protected]","www.service2","1","1","True","False"]},{"id":5,"cell":["5","Сервисный центр 1_3","United Arab Emirates","Одесса","Артема","123-321","[email protected]","www.service3","1","1","True","False"]}, 

$("#gridSC").jqGrid({ 
    url: '<%= ResolveClientUrl("~/FetchData.asmx/bindSC") %>', 
    datatype: 'json', 
    mtype: 'POST', 
    treeGridModel: 'adjacency', 
    ExpandColumn: 'Name', 
    ExpandColClick: true, 
    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, 
    serializeGridData: function (postData) { 
    if (postData.searchField === undefined) postData.searchField = null; 
    if (postData.searchString === undefined) postData.searchString = null; 
    if (postData.searchOper === undefined) postData.searchOper = null; 
    //if (postData.filters === undefined) postData.filters = null; 
    return JSON.stringify(postData); 
    }, 
jsonReader: { 
    root: function (obj) { return obj.d.rows; }, 
    page: function (obj) { return obj.d.page; }, 
    total: function (obj) { return obj.d.total; }, 
    records: function (obj) { return obj.d.records; } 
    }, 
    colNames: ['Id', 'Name', 'CountryName', 'Town', 'Adress', 'Phone', 'Email', 'Url'], 
    colModel: [ 
       { name: 'Id', index: 'id', width: 10, hidden: true, key: true }, 
       { name: 'Name', index: 'Name', width: 80 }, 
       { name: 'CountryName', index: 'CountryName', width: 80 }, 
       { name: 'Town', index: 'Town', width: 20 }, 
       { name: 'Adress', index: 'Adress', width: 90 }, 
       { name: 'Phone', index: 'Phone', width: 20 }, 
       { name: 'Email', index: 'Email', width: 20 }, 
       { name: 'Url', index: 'Url', width: 30 } 
       ], 
    rowNum: 10, 
    rowList: [10, 20, 300], 
    sortname: 'Name', 
    sortorder: "asc", 
    pager: "#pjmapSC", 
    viewrecords: true, 
    gridview: true, 
    rownumbers: true, 
    height: "100%", 
    caption: '' 
    }) 

和測試 treeReader:{ level_field: 「水平」, parent_id_field:「parent」, leaf_field:「isLeaf」, expanded_field:「擴展」 }, 網格(非樹網格)沒有任何變化。在服務器側我的方法

 int startIndex = (page - 1) * rows; 
     int endIndex = (startIndex + rows < recordsCount) ? 
         startIndex + rows : recordsCount; 
     List<TableRow> gridRows = new List<TableRow>(rows); 

     for (int i = startIndex; i < endIndex; i++) 
     { 
      gridRows.Add(new TableRow() 
       { 
       id = lsc[i].Id, 
       cell = new List<string>(11) { 
        lsc[i].Id.ToString(), 
        lsc[i].Name, 
        lsc[i].CountryName, 
        lsc[i].Town, 
        lsc[i].Adress, 
        lsc[i].Phone, 
        lsc[i].Email, 
        lsc[i].Url, 
        lsc[i].level.ToString(), 
        lsc[i].parent, 
        lsc[i].isLeaf.ToString(), 
        lsc[i].expanded.ToString() 

如果我改變此 //string.Format("Name:{0}」,LSC [I]請將.Name) 我的電網負荷錯誤地數據

幫助請。


感謝奧列格,但我盡我的服務器字符串變成

{ 「d」:{ 「__類型」: 「_ admin.JqGridData」, 「總」:3, 「頁」:1, 「records」:26,「rows」:[{「id」:1,「cell」:[「1」,「Сервисныецентры」,「阿拉伯聯合酋長國」,「Киев」,「Красныхпартизан1」,「123 -321" , 「[email protected]」, 「www.service1」, 「0」,NULL, 「假」, 「假」]},{ 「ID」:2, 「小區」:[ 「2」, 「Сервисныйцентр1_1」,「阿拉伯聯合酋長國」,「Киев」,「Артема」,「123-321」,「[email protected]」,「www.service2」,「1」,「1」,「True 「,」False「]},{」id「:4,」cell「:[」4「,」Сервисныйцентр1_2「,」阿拉伯聯合酋長國「,」Донецк「,」Артема「,」123-321「, 「[email protected]」, 「www.service2」, 「1」, 「1」, 「真」, 「假」]},{ 「id」:5,「cell」:[「5」,「Сервисныйцентр1_3」,「阿拉伯聯合酋長國」,「Одесса」,「Артема」,「123-321」,「[email protected]」,「 www.service3" , 「1」, 「1」, 「真」, 「假」]},

$("#gridSC").jqGrid({ 
    url: '<%= ResolveClientUrl("~/FetchData.asmx/bindSC") %>', 
    datatype: 'json', 
    mtype: 'POST', 
    treeGridModel: 'adjacency', 
    ExpandColumn: 'Name', 
    ExpandColClick: true, 
    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, 
    serializeGridData: function (postData) { 
    if (postData.searchField === undefined) postData.searchField = null; 
    if (postData.searchString === undefined) postData.searchString = null; 
    if (postData.searchOper === undefined) postData.searchOper = null; 
    //if (postData.filters === undefined) postData.filters = null; 
    return JSON.stringify(postData); 
    }, 
jsonReader: { 
    root: function (obj) { return obj.d.rows; }, 
    page: function (obj) { return obj.d.page; }, 
    total: function (obj) { return obj.d.total; }, 
    records: function (obj) { return obj.d.records; } 
    }, 
    colNames: ['Id', 'Name', 'CountryName', 'Town', 'Adress', 'Phone', 'Email', 'Url'], 
    colModel: [ 
       { name: 'Id', index: 'id', width: 10, hidden: true, key: true }, 
       { name: 'Name', index: 'Name', width: 80 }, 
       { name: 'CountryName', index: 'CountryName', width: 80 }, 
       { name: 'Town', index: 'Town', width: 20 }, 
       { name: 'Adress', index: 'Adress', width: 90 }, 
       { name: 'Phone', index: 'Phone', width: 20 }, 
       { name: 'Email', index: 'Email', width: 20 }, 
       { name: 'Url', index: 'Url', width: 30 } 
       ], 
    rowNum: 10, 
    rowList: [10, 20, 300], 
    sortname: 'Name', 
    sortorder: "asc", 
    pager: "#pjmapSC", 
    viewrecords: true, 
    gridview: true, 
    rownumbers: true, 
    height: "100%", 
    caption: '' 
    }) 

和測試 treeReader:{ level_field: 「電平」, parent_id_field :「parent」, leaf_field:「isLeaf」, expanded_field:「expanded」 }, 網格(非樹網格)沒有任何變化。在服務器端我的方法

 int startIndex = (page - 1) * rows; 
     int endIndex = (startIndex + rows < recordsCount) ? 
         startIndex + rows : recordsCount; 
     List<TableRow> gridRows = new List<TableRow>(rows); 

     for (int i = startIndex; i < endIndex; i++) 
     { 
      gridRows.Add(new TableRow() 
       { 
       id = lsc[i].Id, 
       cell = new List<string>(11) { 
        lsc[i].Id.ToString(), 
        lsc[i].Name, 
        lsc[i].CountryName, 
        lsc[i].Town, 
        lsc[i].Adress, 
        lsc[i].Phone, 
        lsc[i].Email, 
        lsc[i].Url, 
        lsc[i].level.ToString(), 
        lsc[i].parent, 
        lsc[i].isLeaf.ToString(), 
        lsc[i].expanded.ToString() 

如果我改變這個 //string.Format("Name:{0}」,LSC [1]。名稱) 我的網格加載不正確數據

請幫忙。

回答

0

你主要的錯誤是在你使用樹形網格的列名的錯誤情況下。它應該是level,parent,expanded而不是Level,Parent,Expanded,您在JSON輸入中使用它。只有isLeaf您正確使用。因此,您應該糾正其使用Web服務或添加參數treeReader描述,您使用的名稱列:

treeReader = { 
    level_field: "Level", 
    parent_id_field: "Parent", 
    leaf_field: "isLeaf", 
    expanded_field: "Expanded" 
} 

此外,我會建議你,讓你使用datatype: 'json'代替datatype的功能更改代碼。