2016-07-31 3 views
1

여기에 해당합니다.데이터 테이블 안에있는 json 형식의 컨트롤러에서 데이터 가져 오기

  • 내 테이블에서 json 데이터를 바인딩하기 위해 datatable js에서 ajax 호출을 사용하고 있습니다.
  • 지금 데이터 바인딩을 위해 json 파일을 직접 사용하고 있습니다.
  • 이제 json 값을 반환하는 내 컨트롤러 내에 메서드를 작성한 내 db에서 데이터에 액세스하려고합니다.
  • 아약스에서 json 파일 을 호출하는 것처럼이 메서드를 호출 할 수 없습니다. 친절하게 해결책을 제안하십시오. 여기

    //Controller Name AppDetail 
        public string getData(string ddlid) 
        { 
         DataTable ddl = new DataTable(); 
         string query = string.Empty; 
         if (ddlid == "O1") 
         { 
          query = "SELECT for O1"; 
         } 
         else if (ddlid == "O2") 
         { 
          query = "SELECT for O2"; 
         } 
         con.Open(); 
         MySqlDataAdapter da = new MySqlDataAdapter(query, con); 
         da.Fill(ddl); 
         con.Close(); 
         System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
         return jSearializer.Serialize(ddl);    
        } 
    

    그리고

    { 
        "data": [ 
         { 
          "name": "Aladdin" 
         } 
        ] 
    } 
    

    이 친절하게 도와 JSON 데이터 샘플을 수 있습니다 :

    다음은 코드 샘플

    var table = $('#example').DataTable({ 
         "ajax": "/content/data/dataList.json", //here I want the url of my method. 
         "bDestroy": true, 
         "iDisplayLength": 15, 
         "columns": [ 
          { 
           "class": 'details-control', 
           "orderable": false, 
           //"data": null, 
           "defaultContent": '' 
          }, 
          { "data": "name" }, 
         ], 
         "order": [[1, 'asc']], 
         "fnDrawCallback": function (oSettings) { 
          runAllCharts(); 
         } 
        }); 
    

그리고 내 메소드 ID에게 있습니다.

답변

0

컨트롤러 당신이 서버 측 처리 방법은 첫째 아약스 방법 및 사용을 사용하여 모든 데이터를 얻을 사용하지 않는 경우 DataTables

1

을의 (https://datatables.net/reference/option/data) 소스 DataTables 전에 호출하고 데이터를 통해 데이터를 삽입 할 수 있습니다 작동하는 경우 그 데이터 테이블의 데이터. 아래 코드를 보면 ... 아이디어를 얻는 데 도움이 될 수 있습니다.

$.ajax({ 
      url: 'api/AppDetail/getData', 
      method: 'get', 
      data :{ddlid:'01'},   // this is input parameter for your function 
      dataType: 'json', 
      contentType: 'text/json', 
      success: function(res){ 
       var table=$('#example').dataTable({ 
        data: res, 
        columns:[ 
          {'data':'name'} 
         ], 
        bDestroy : true, 
       iDisplayLength : 15, 
       }); 
      } 
     }); 
0

컨트롤러에서 u가 원하는 데이터를 가져 오면이 데이터를 부분보기로 반환 할 수 있습니다. 부분보기는 Razor 구문이나 기타 사항을 나타내는 HTML 표가 아닙니다. 그런 다음이 부분보기를 반환하기 위해 아약스를 호출합니다. 성공하면 데이터 표 플러그인을 적용 할 수 있습니다.

<div id=MyTable></div> 
$.ajax({ 
     type: 'GET', 
     url: ControllerName/ActionName=partialView Which makes table. 
     success: function (data) { 
      debugger; 
      $('#MyTable').html(data); //Puting result of ajax call to the div which containg Id, 
      $('#PartilView_Table').DataTable({ // Applying DataTable Plugin table inside partialView        
       "bProcessing": true, 
       "bDeferRender": true, 
       "scrollX": true, 
       "stateSave": true, 
       "bAutoWidth": true, 
       "bSort": false, 
       "columnDefs": [ 
        { 

        } 
       ] 
       }); 
     }, 
     }); 

희망이 당신을 도울 것입니다 ..

관련 문제