2017-10-22 37 views
1

我試圖使用jQuery DataTables。在我使用引導表之前。 在服務器上,我使用彈簧啓動,帶分頁的彈簧數據。使用返回的服務器參數來顯示數據

<table id="example" class="display" cellspacing="0" width="100%"> 
     <thead> 
      <tr> 
       <th>memberId</th> 
       <th>name</th> 
       <th>phone1</th> 
       <th>phone2</th> 
       <th>email</th> 
      </tr> 
     </thead> 
    </table> 
$('#example').DataTable({ 
      "processing": "true", 
      "serverSide": "true", 
      "ajax": { 
       "url": "/rest/members", 
       "data": function (d) { 
        return { 
         search: d.search.value, 
         page: d.start, 
         size: d.length 
        } 

       }, 
       dataFilter: function (data) { 
        var json = jQuery.parseJSON(data); 
        json.recordsTotal = json.totalElements; 
        json.data = json.content; 
        return JSON.stringify(json); // return JSON string 
       } 

      } 
     }); 

與服務器返回的值,我試圖顯示的數據。事實上,沒有任何顯示

{ 
    "content":[ 
     { 
     "memberId":1, 
     "name":"bob", 
     "phone1": "450", 
     "phone2": "1", 
     "email": "test" 
     }, 
     { 
     "memberId":2, 
     "name":"robert" 
     "phone1": "2323", 
     "phone2": "2", 
     "email": "test" 
     } 
    ], 
    "last":true, 
    "totalElements":27, 
    "totalPages":1, 
    "sort":null, 
    "numberOfElements":27, 
    "first":true, 
    "size":100, 
    "number":0 
} 

編輯

數據表警告:表ID =例如 - 請求的未知參數「0」 0行,列0。有關此錯誤的詳細信息,請參閱http://datatables.net/tn/4

enter image description here

回答

1

的幾個問題

  1. 樣品JSON是無效的,你已經尾隨逗號

  2. 的AJAX數據的回調被稱爲dataSrc,不dataFilter

  3. 你並不需要解析data,JSON已經是公認的dataType

  4. 返回data作爲字符串,其中一個數組,預計將引發一個錯誤

  5. 的工作回調可以看看這個

dataSrc: function(data) { 
     data.recordsTotal = data.totalElements; 
     data.data = data.content; 
     return data.data 
    } 

演示基於JSON的上方 - >http://jsfiddle.net/sx3py6fg/

+0

看到編輯,我做了修飾 –

+0

@roberttrudel,現在缺少一個逗號:)見更新,已經做了一個小提琴 - 真的沒什麼需要補充的。 – davidkonrad

相關問題