2016-11-21 4 views
0

지연 렌더링을 사용하려는 datatable이 있습니다. 문제가 무엇인지 확실하지 않습니다. 내 컨트롤러 메소드가 json 객체 배열을 반환합니다. 아래 코드를 참조하십시오.지연 렌더링 기능을 데이터 테이블에 사용하는 방법

** DataTable을 설치 **

페이지가로드 될 때 호출된다.

 var $dtTable = $("#tblPlayer"); 
     $dtTable.dataTable({ 
      bFilter: false, 
      pageLength: 10, 
      paging: true, 
      autoWidth: true, 
      columns: 
      [ 
       null, 
       { "orderDataType": "dom-text", type: "string" }, 
       { "orderDataType": "dom-text", type: "string" }, 
       { "orderDataType": "dom-text-numeric" }, 
       { "orderDataType": "dom-text-numeric" }, 
       { "orderDataType": "dom-text-numeric" }, 
       { "orderDataType": "dom-text-numeric" }, 
       { "orderDataType": "dom-text-numeric" }, 
       null, 
       null, 
       null 
      ], 
      "ajax": "Player/GetSetPlayers", 
      "deferRender": true 
     }); 

컨트롤러 방법

public object[] GetSetPlayers() 
    { 
     var players = GetPlayers(); 

     _players = new object[players.Count]; 

     for (var i = 0; i < players.Count; i++) 
     { 
      _players[i] = players[i]; 
     } 
     return _players; 
    } 

GetSetPlayers()는 JSON 오브젝트의 배열을 반환 아래 결과는 0과 1을 포함 어떤 인덱스의 exampel이다.

응답

[ 
    { 
     "product":25000, 
     "rank":1, 
     "dirty_money":25000, 
     "id":"b4b41b18edbb49b9ae80be5e768b6b80", 
     "name":"Dan", 
     "ban_status":0, 
     "edit":"<a href='/support/player_gamedata/b4b41b18edbb49b9ae80be5e768b6b80/game' class='btn'><i class='icon-folder-close'></i></a>", 
     "credit":30, 
     "clean_money":20000, 
     "ban":"<a href='/support/ban_player/by_id/b4b41b18edbb49b9ae80be5e768b6b80/' class='btn'><i class='icon-remove'></i></a>", 
     "supplies":25000 
    }, 
    { 
     "product":25000, 
     "rank":1, 
     "dirty_money":25000, 
     "id":"3cac6e366170458686021eaa77ac4d6d", 
     "name":"Dan", 
     "ban_status":0, 
     "edit":"<a href='/support/player_gamedata/3cac6e366170458686021eaa77ac4d6d/game' class='btn'><i class='icon-folder-close'></i></a>", 
     "credit":30, 
     "clean_money":20000, 
     "ban":"<a href='/support/ban_player/by_id/3cac6e366170458686021eaa77ac4d6d/' class='btn'><i class='icon-remove'></i></a>", 
     "supplies":25000 
    } 
] 

답변

0

난 당신이 반환 된 데이터로 채워해야 할 각 컬럼에 "데이터"속성을 지정해야합니다 생각합니다. 그렇지 않으면 json 개체의 어떤 특성이 각 열로 이동하는지 알지 못합니다. 데이터를 객체의 배열 대신 배열의 배열로 정의하면이 작업이 필요하지 않습니다. 또한

당신이 필요하지 않은 "DATASRC"옵션을 지정하지하지만이 설정되지 않은 경우, 나는 그것이 반환 된 JSON의 형식이 될 것으로 기대하고 믿고 : 그래서

{ 
    data: [ {...}, {...} ] 
} 

는 것 ajax 요청에 원시 응답을 추가 할 수 있다면 도움이 될 것입니다.

EDIT1 :

좋아, 그래서 두 번 확인하고 당신은 아무것도 수정할 필요없이이 문제를 해결하기 위해이 같은 작업을 수행 할 수 있도록 네 그것은 JSON 개체의 "데이터"속성에 할당 된 객체 배열을 원하는 않습니다 서버에. 아약스 옵션을 다음으로 변경하십시오.

"ajax": { 
    "url": "Player/GetSetPlayers", 
    "dataSrc": function (json) { 
     // We need override the built in dataSrc function with one that will 
     // just return the object array instead of looking for a "data" 
     // attribute on the "json" object. Note if you ever want to add 
     // serverside sorting/filtering/paging you will need to move your table 
     // data to an attribute within the JSON object. 
     return json; 
    } 
} 

위의 수정 사항을 모두 수행하면 좋을 것입니다.

+0

응답을 사용하여 질문을 업데이트했습니다. –

+0

@ lab labz 데이터가있는 각 열에 '데이터'속성을 설정하여이 기능을 실행 해 보셨습니까? 그것은 단지 "data : 'jsonAttrName',"입니다. – Adrian