2012-05-31 3 views
0

jqGrid (4.3.2)에 행을 표시하려고했지만 아직 성공하지 못했습니다. 내 웹 서비스 응답은 다음과 같습니다 jqGrid가 행을 표시하지 않습니다.

{"d": 
{"__type":"JSONResponse:pe", 
"ActionType":null, 
"JSONData":" 
      {\"total\":1, 
      \"page\":1, 
      \"records\":5, 
      \"rows\":[ 
         {\"invid\":1,\"cell\":[{\"invid\":1,\"amount\":500,\"tax\":65,\"total\":565,\"note\":\"myNote 0\"}]}, 
         {\"invid\":2,\"cell\":[{\"invid\":2,\"amount\":510,\"tax\":75,\"total\":585,\"note\":\"myNote 1\"}]}, 
         {\"invid\":3,\"cell\":[{\"invid\":3,\"amount\":520,\"tax\":85,\"total\":605,\"note\":\"myNote 2\"}]}, 
         {\"invid\":4,\"cell\":[{\"invid\":4,\"amount\":530,\"tax\":95,\"total\":625,\"note\":\"myNote 3\"}]}, 
         {\"invid\":5,\"cell\":[{\"invid\":5,\"amount\":540,\"tax\":105,\"total\":645,\"note\":\"myNote 4\"}]} 
         ] 
      }", 
"Status":false 
} 

} <이 - (위의 코드에서이 중괄호를 가정)

이 같은

내 그리드 구성 모양 : -

<script type="text/javascript"> 
    $(document).ready(function() { 

     gridConfig(); 

    }); 

    function gridConfig() { 
     var request = JSON.stringify(requestData()); 
     var jsonResponse; 

     $("#list").jqGrid({ 
      url: '/ScriptServices/JsonService.svc/Process', 
      ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, 
      datatype: 'json', 
      postData: { request: function() { return request; } }, 
      mtype: 'GET', 
      colNames: ['Inv No', 'Amount', 'Tax', 'Total', 'Notes'], 
      colModel: [ 
        { name: 'invid', index: 'invid', width: 55 }, 
        { name: 'amount', index: 'amount', width: 80, align: 'right' }, 
        { name: 'tax', index: 'tax', width: 80, align: 'right' }, 
        { name: 'total', index: 'total', width: 80, align: 'right' }, 
        { name: 'note', index: 'note', width: 150, sortable: false } 
        ], 
      pager: $('#pager'), 
      rowNum: 10, 
      rowList: [10, 20, 30], 
      sortname: 'invid', 
      sortorder: 'desc', 
      viewrecords: true, 
      jsonReader: { 
       root: "d.JSONData.rows", 
       page: "d.JSONData.page", 
       total: "d.JSONData.total", 
       records: "d.JSONData.records", 
       cell: "cell", 
       id:0 
      }, 
      caption: 'My first grid' 
     }); 

    }; 

    function requestData() { 
     var request; 
     request = { 
      ActionType: 'GET_GRID_DATA', 
      RequestJSONData: '{ }' 
     }; 
     return request; 
    } 

</script> 

나는를 찾을 수 없습니다입니다 발행물. 다른 형식이 필요합니다. ..

갱신 1 제발 도움말 :

이제 내 데이터 올렉 제안 후 적절한 JSON 형식으로오고있다,하지만 때문에 내 JSON 데이터에 __type 추가 필드의 그 올바르게 렌더링하지.

수정 된 JSON 데이터는 다음과 같습니다 -

{ 
"d": 
    {"__type":"JSONResponse:pe", 
    "ActionType":"GRID_RESP", 
    "JSONData": 
       {"__type":"JQGridJSONReader:pe", 
       "total":1, 
       "page":1, 
       "records":5, 
       "rows":[ 
         {"__type":"InvoiceGrid:pe", 
         "invid":1, 
         "cell":[{"__type":"JqGridCell:pe","invid":1,"amount":500,"tax":65,"total":565,"note":"myNote 0"}]}, 
         .... and More Rows 
         ] 
       }, 
    "Status":true 
} 

} <이 -이 link을 확인하고 기본 생성자의 제안을 구현 한

(위의 코드에서이 중괄호를 가정), protected internal 할 수 없습니다 나의 경우에는. 이 같은

내 서비스 구성보기 :

<system.serviceModel> 
    <services> 
     <service name="JsonService" behaviorConfiguration="JsonServiceAspNetAjaxBehavior"> 
      <endpoint address="" behaviorConfiguration="JsonServiceEndPointBehavior" 
       binding="webHttpBinding" contract="JsonService" /> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="JsonServiceEndPointBehavior"> 
       <enableWebScript/> 
       <dataContractSerializer ignoreExtensionDataObject="true" /> 
      </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
      <behavior name="JsonServiceAspNetAjaxBehavior"> 
       <serviceMetadata httpGetEnabled="true"/> 
       <serviceDebug includeExceptionDetailInFaults="true" /> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
     multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 

내가 오류 " 'QueryStringConverter'로 변환 할 수있는 유형이 있어야합니다 UriTemplate 쿼리 값을 변수"를 얻을 <webHttp/>endpointBehaviors에 내가 적용하는 경우

. 이 같은

내 WebService에 모양 : -

[ServiceContract(Namespace = "http://www.abhishek.com/portal/json")] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class JsonService 
{ 
    [OperationContract] 
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
    [GenerateScriptType(typeof(JsonService), ScriptTypeId = "JsonService1")] 
    public PresentationEntities.JSONResponse Process(PresentationEntities.JSONRequest request) 
    { 
     JSONResponse response; 

     try 
     { 
      if (request == null) 
      { 
       throw new ArgumentNullException("request", string.Format(ExceptionHelper.ARG_NULL_EXP_MSG, "JSONManager")); 
      } 
      else 
      { 
       JQGridTransalator gridtransalator = new JQGridTransalator(); 
       response = gridtransalator.JQGridToJSONResponse(); // fill response.JSONData with JQGridJSONReader 
      } 
     } 
     catch (Exception) 
     { 
      throw; 
     } 

     return response; 
    }} 

는 다음과 같이 JQGridJSONReader : -

[DataContract(Name = "JQGridJSONReader", Namespace = "pe")] 
public class JQGridJSONReader 
{ 
    public JQGridJSONReader() 
    { 

    } 

    [DataMember(Name = "total", Order = 1)] 
    public int Total { get; set; } 

    [DataMember(Name = "page", Order = 2)] 
    public int Page { get; set; } 

    [DataMember(Name = "records", Order = 3)] 
    public int Records { get; set; } 

    [DataMember(Name = "rows", Order = 4)] 
    public List<InvoiceGrid> Rows { get; set; } 
} 

어떤 제안은 JSON 데이터에서 __type를 제거합니다.

갱신 2 :

좀 더 명확하게

[DataContract(Name = "InvoiceGrid", Namespace = "pe")] 
public class InvoiceGrid 
{ 
    [DataMember(Name = "cell", Order = 2)] 
    public List<JqGridCell> Cell { get; set; } 
} 

[DataContract(Name = "JqGridCell", Namespace = "pe")] 
public class JqGridCell 
{ 
    [DataMember(Name = "invid", Order = 1)] 
    public int Invid { get; set; } 

    [DataMember(Name = "invdate", Order = 2)] 
    public DateTime InvDate { get; set; } 

    [DataMember(Name = "amount", Order = 3)] 
    public double Amount { get; set; } 

    [DataMember(Name = "tax", Order = 4)] 
    public double Tax { get; set; } 

    [DataMember(Name = "total", Order = 5)] 
    public double Total { get; set; } 

    [DataMember(Name = "note", Order = 6)] 
    public string Note { get; set; } 
} 

답변

1

귀하의 오류 내 질문에 대답 할 수 있도록 코드를 업데이트하면 JSON 두 번에 객체의 데이터 d.JSONData와 객체를 직렬화한다는 것입니다. 아마도 사용 객체 대신 수동으로 JSON 직렬화를 사용했고 .NET에서 데이터 자체를 직렬화 할 수있었습니다.

당신이 d.JSONData문자열 대신 객체가 rows 특성을 갖는 때문에 d.JSONData.rows 같은 데이터를 액세스 할 수 없습니다 볼 수있는 방법.

+0

나는 내 json 데이터를 바로 잡았다. __type은 jsonReader 형식에 영향을주는 json 데이터와 함께 사용하는 추가 필드입니다. ** Update1 ** – abhishekrvce

+0

오류가 발생했습니다. "{"__ type ":"JqGridCell : pe ","invid ": 1,"amount ": 500,"tax ": 65,"total ": 565,"note ":"myNote 0 "}]},'. 곧 나는 대답을 게시 할 것이다. @nleg에게 .net 객체를 json 객체로 변환하는 방법에 대한 힌트를 주셔서 감사합니다. – abhishekrvce

+0

예 수동으로 JSON 직렬화를 사용하고있었습니다. 그것을 바로 잡았습니다. 앞으로는 응답을 보낼 때 ASP.NET이 이미 나와 함께하기 때문에 앞으로는 그렇게하지 않을 것입니다. :) – abhishekrvce

0

첫 번째 오류가 수정되었습니다. - @Oleg에서 제안한대로 JSONData (문자열 형식 임)를 json 개체로 변환했습니다. 이는 JSON 직렬화가 데이터를 보내기 전에 사용 되었기 때문입니다.수정

두 번째 실수 : - : (복합 형 즉 GridData 인) List<GridData>가와 GridData는 셀 내용이 있었다

내 세포 유형은 다음과 같이이었다. 그래서 나는 이것을 얻고있다

"cell":[{"__type":"JqGridCell:pe","invid":1,"amount":500,"tax":65,"total":565,"n‌​ote":"myNote 0"}]},이 셀에는 첫 번째 행으로 복잡한 유형이있다. 상상력은 "cell":[{ComplexType}] 그래서 첫 번째 셀에는 "[Object object]"가 표시되고 나머지 셀은 비어 있습니다.

이렇게 수정 된 셀 유형은 List<string>으로 그리드 데이터를 추가 한 다음 List<InvoiceGrid>에 셀을 추가합니다. 나는 그 오류를 생각

 JQGridJSONReader input = new JQGridJSONReader(); 
     input.Page = 1; 
     input.Total = 1; 
     input.Records = 5; 

     input.Rows = new List<InvoiceGrid>(); 


     for (int i = 0; i < 5; i++) 
     { 
      InvoiceGrid invGrid = new InvoiceGrid(); 
      invGrid.Cell = new List<string>(); 
      JqGridCell gridrows = new JqGridCell(); 
      gridrows.Invid = 1 + i; 
      gridrows.Amount = 500.00 + (i * 10); 
      gridrows.Tax = 65.00 + (i * 10); 
      gridrows.Total = gridrows.Amount + gridrows.Tax; 
      gridrows.Note = "myNote " + i.ToString(); 
      invGrid.Cell.Add(gridrows.Invid.ToString()); 
      invGrid.Cell.Add(gridrows.InvDate.ToString()); 
      invGrid.Cell.Add(gridrows.Amount.ToString()); 
      invGrid.Cell.Add(gridrows.Tax.ToString()); 
      invGrid.Cell.Add(gridrows.Total.ToString()); 
      invGrid.Cell.Add(gridrows.Note.ToString()); 
      input.Rows.Add(invGrid); 
     } 

때문에의 __type로 추가 된 JSON 데이터입니다. 하지만 jqGrid는이를 무시합니다.

내 json 데이터가 이제 이와 같이 보입니다. 참고 : - 이번에는 invid를 무시했지만 포함될 수 있습니다.

{"d": 
{"__type":"JSONResponse:pe", 
"ActionType":"GRID_RESP", 
"JSONData": 
      {"__type":"JQGridJSONReader:pe", 
      "total":1, 
      "page":1, 
      "records":5, 
      "rows":[ 
        {"__type":"InvoiceGrid:pe", 
        "cell":["1","500","65","565","myNote 0"]}, 
        {"__type":"InvoiceGrid:pe", 
        "cell":["2","510","75","585","myNote 1"]}, 
        {"__type":"InvoiceGrid:pe", 
        "cell":["3","520","85","605","myNote 2"]}, 
        {"__type":"InvoiceGrid:pe", 
        "cell":["4","530","95","625","myNote 3"]}, 
        {"__type":"InvoiceGrid:pe", 
        "cell":["5","540","105","645","myNote 4"]} 
        ] 
      }, 
"Status":true 
}} 

고맙습니다. @Oleg.

관련 문제