2014-04-01 117 views
1

我是JQGrid的新手。jqgrid顯示響應地圖

我得到了一個服務器端POJO的JSON表示,我試圖在JQGrid中呈現它。

方案-1 - 從響應

{ 

     "parameterWiseResult": null, 

     "results": null, 

     "resultsCollection": 

     [ 

      { 

       "_id": 

       { 

        "SpId": 515, 

        "Channel_Id": 0, 

        "ActivityId": "Hk1Uzn0oqSUbMc56SAbFZw", 

        "SchedularId": 2127, 

        "UniqueID": 9 

       }, 

       "Sentiment": 7.870000000000002, 

       "id": 1 

      }, 

      { 

       "_id": 

       { 

        "SpId": 515, 

        "Channel_Id": 0, 

        "ActivityId": "GiMkGZqChJsTvJ2LKrdFhA", 

        "SchedularId": 2127, 

        "UniqueID": 11 

       }, 

       "Sentiment": 2.06, 

       "id": 2 

      } 

     ], 

     "result": null, 

     "errorMessage": null, 

     "paginationParameters": 

     { 

      "rangeKey": 0, 

      "noOfPages": 0, 

      "totalNoOfRecords": 61, 

      "page": 61 

     } 

    } 

下面顯示陣列的是,我使用填充於上述響應電網的示例代碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 

<HTML> 

<HEAD> 

    <TITLE> New Document </TITLE> 

    <META NAME="Generator" CONTENT="EditPlus"> 

    <META NAME="Author" CONTENT=""> 

    <META NAME="Keywords" CONTENT=""> 

    <META NAME="Description" CONTENT=""> 

</HEAD> 



<BODY> 

    <title>TestGrid</title> 

<script src="scripts/jquery-1.9.1.js" type="text/javascript"></script> 

<script src="scripts/jquery-ui.js" type="text/javascript"></script> 

<script src="scripts/grid.locale-en.js" type="text/javascript"></script> 

<script src="scripts/jquery.jqGrid.min.js" type="text/javascript"></script> 

<script src="scripts/custom/grid.js" type="text/javascript"></script> 

<link rel="stylesheet" href="css/common/ui.css" /> 

<link rel="stylesheet" href="css/jquery-ui.css" /> 

<script type="text/javascript"> 

$(document).ready(function() { 



     var tee = {"spId" : 515, "channels" : [2,7], "queryParameters" : {"Sentiment" : "Positive"} , "channelWisePaginationParameters" : {"7" : {"noOfPages" : 50, "rangeKey" : 8},"2" : {"noOfPages" : 1, "rangeKey" : 4}}}; 



     jQuery("#testGrid").jqGrid({ 



       jsonReader : { 

        root:"resultsCollection", 

          page : "paginationParameters.noOfPages", //current page of the query 

          total : "paginationParameters.page", //total pages for the query 

          records : "paginationParameters.totalNoOfRecords",//total number of records for the query 

          rows: "results", 

          repeatitems : false, 

          id : "id" //the unique id of the row 

        }, 



       url:'http://localhost:8080/SapphireDataAnalysis/ChannelDetails/ProcessedDataForSentimentWithId', 

        mtype: 'POST', 

        postData : JSON.stringify(tee) , 

        ajaxGridOptions: { contentType: 'application/json'}, 

         ajaxEditOptions: { contentType: 'application/json', dataType: 'json' }, 

       datatype: "json", 

       colNames:['Sentiment'], 

       colModel:[ 

       {name:'Sentiment',label:'Sentiment', width:55} 

       ], 

       rowNum:10, 

       rowList:[10,20,30], 

       pager: '#testGridPager', 

       viewrecords: true, 

     gridview: true, 

       height: '100%' , 

       loadComplete: function (data) { 

         var myData = JSON.stringify(data); 

         alert(myData); 

        } 

       }); 

}); 

</script> 

</head> 

<body> 

<h1>Test grid </h1> 

<table id="testGrid"> 

</table> 

<div id="testGridPager"></div> 

</BODY> 

</HTML> 

方案-2- - 陣列圖

{ 

     "parameterWiseResult": 

     { 

      "Youtube": 

      [ 

       { 

        "_id": 

        { 

         "SpId": 515, 

         "Channel_Id": 0, 

         "ActivityId": "Hk1Uzn0oqSUbMc56SAbFZw", 

         "SchedularId": 2127, 

         "UniqueID": 9 

        }, 

        "Sentiment": 7.870000000000002, 

          "id": 1 

       }, 

       { 

        "_id": 

        { 

         "SpId": 515, 

         "Channel_Id": 0, 

         "ActivityId": "GiMkGZqChJsTvJ2LKrdFhA", 

         "SchedularId": 2127, 

         "UniqueID": 11 

        }, 

        "Sentiment": 2.06, 

        "id": 2 

       } 

      ], 

      "Twitter": 

      [ 

       { 

        "_id": 

        { 

         "SpId": 515, 

         "Channel_Id": 0, 

         "ActivityId": "616070755", 

         "SchedularId": 2127, 

         "UniqueID": 5 

        }, 

        "Sentiment": 0.215, 

        "id": 1 

       }, 

       { 

        "_id": 

        { 

         "SpId": 515, 

         "Channel_Id": 0, 

         "ActivityId": "1335772050", 

         "SchedularId": 2127, 

         "UniqueID": 6 

        }, 

        "Sentiment": 0.07, 

          "id": 2 

       } 

      ] 

     }, 

     "results": null, 

     "resultsCollection": null, 

     "result": null, 

     "errorMessage": null, 

     "paginationParameters": 

     { 

      "rangeKey": 0, 

      "noOfPages": 0, 

      "totalNoOfRecords": 225, 

      "page": 0 

     } 

    } 

我與做方案-1,但無法弄清楚如何在jqGrid的渲染場景的2 - 我想是這樣的,以顯示:

**Channel**   **Sentiment** 
Youtube    7.870000000000002 
Youtube    2.06 
Twitter    0.215 
Twitter    0.07 

回答

0

我會建議你使用beforeProcessing回調這種情況。例如,你可以做到以下幾點:

beforeProcessing: function (data) { 
    var prop, input = data.parameterWiseResult, i, l, arr, item; 
    data.rows = []; 
    for (prop in input) { 
     if (input.hasOwnProperty(prop) && $.isArray(input[prop])) { 
      arr = input[prop]; 
      l = arr.length; 
      for (i = 0; i < l; i++) { 
       item = arr[i]; 
       data.rows.push({ 
        id: prop + "_" + item.id, 
        Channel: prop, 
        Sentiment: item.Sentiment 
       }); 
      } 
     } 
    } 
} 

The demo顯示

enter image description here